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

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):

 1 import  org.springframework.context.ConfigurableApplicationContext;
 2 import  org.springframework.context.support.ClassPathXmlApplicationContext;
 3 import  org.springframework.mail.MailSender;
 4 import  org.springframework.mail.SimpleMailMessage;
 5
 6 public   class  SendMail  {
 7
 8    public static void main(String[] args) {
 9        SimpleMailMessage message = new SimpleMailMessage();
10
11        message.setFrom("[email protected]");  //髮送者郵件地阯
12        message.setTo("[email protected]");            //接收者郵件地阯
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}

你可能感兴趣的:(Spring实现发送邮件功能(简易篇))