必須添加的包commons-logging.jar,mail.jar,spring.jar
mailMessage.xml配置文件********************************
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p">
<!-- 加載Properties文件 -->
<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
<!-- 申明SimpleMailMessage對象 -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<!-- 設置發送方,hz是發送者的名字 -->
<value><![CDATA[hz<${mail.from}>]]></value>
</property>
<!-- 設置接收方 -->
<property name="to" value="${mail.to}" />
<!-- 查看SimpleMailMessage源碼還可以注入標題,內容等 -->
</bean>
<!-- 申明JavaMailSenderImpl對象 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="defaultEncoding" value="UTF-8" />
<property name="host" value="${mail.host}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<!-- 設置認證開關 -->
<prop key="mail.smtp.auth">true</prop>
<!-- 啓動調試開關 -->
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p">
<!-- 加載Properties文件 -->
<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
<!-- 申明SimpleMailMessage對象 -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<!-- 設置發送方,hz是發送者的名字 -->
<value><![CDATA[hz<${mail.from}>]]></value>
</property>
<!-- 設置接收方 -->
<property name="to" value="${mail.to}" />
<!-- 查看SimpleMailMessage源碼還可以注入標題,內容等 -->
</bean>
<!-- 申明JavaMailSenderImpl對象 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="defaultEncoding" value="UTF-8" />
<property name="host" value="${mail.host}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<!-- 設置認證開關 -->
<prop key="mail.smtp.auth">true</prop>
<!-- 啓動調試開關 -->
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
</beans>
mail.properties************************
mail.host=smtp.163.com//走163發送協議
mail.username=xxxxxxxx//163郵箱名
mail.username=xxxxxxxx//163郵箱名
mail.password=xxxxxxxx//163郵箱密碼
測試文件***********************
package mail;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class SpringSimpleMailTest {
public static void main(String[] args) {
ApplicationContext actx = new ClassPathXmlApplicationContext(
"mail/mailMessage.xml");
MailSender ms = (MailSender) actx.getBean("mailSender");
SimpleMailMessage smm = (SimpleMailMessage) actx.getBean("mailMessage");
// 主題,內容
smm.setSubject("你好");
smm.setText("這個是一個通過Spring框架來發送郵件的小程序");
ms.send(smm);
}
}
public static void main(String[] args) {
ApplicationContext actx = new ClassPathXmlApplicationContext(
"mail/mailMessage.xml");
MailSender ms = (MailSender) actx.getBean("mailSender");
SimpleMailMessage smm = (SimpleMailMessage) actx.getBean("mailMessage");
// 主題,內容
smm.setSubject("你好");
smm.setText("這個是一個通過Spring框架來發送郵件的小程序");
ms.send(smm);
}
}