Spring实现发送邮件功能(简易篇)

1.Spring配制文件(bean.xml):

 1<?xml version="1.0" encoding="UTF-8"?>
 2<beans xmlns="http://www.springframework.org/schema/beans"
 3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 5
 6    <bean id="mailSender"
 7        class="org.springframework.mail.javamail.JavaMailSenderImpl">
 8        <property name="host">
 9            <!-- 公司的郵件服務器 -->
10            <value>mail.xxx.com</value>
11        </property>
12    </bean>
13    
14</beans>


2.测试类(SendMail.java):

 1import org.springframework.context.ConfigurableApplicationContext;
 2import org.springframework.context.support.ClassPathXmlApplicationContext;
 3import org.springframework.mail.MailSender;
 4import org.springframework.mail.SimpleMailMessage;
 5
 6public class SendMail {
 7
 8    public static void main(String[] args) {
 9        SimpleMailMessage message = new SimpleMailMessage();
10
11        message.setFrom("zzk@gmail.com");  //髮送者郵件地阯
12        message.setTo("jakin@xxx.com");            //接收者郵件地阯
13        message.setSubject("Test SendMail");    //郵件主題
14        message.setText("Success");                //郵件內容
15
16        ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
17        ((MailSender) applicationContext.getBean("mailSender")).send(message);
18    }

19}

 

posted @ 2008-01-08 17:53  Jakin.zhou  阅读(350)  评论(0编辑  收藏  举报