spring发邮件

1 mail.host=smtp.163.com
2 mail.username=1881375%%
3 mail.password=aaa%%%
4 mail.from=1881375¥¥¥@163.com

上面是mail.properties 的配置文件

 

applicationContext.xml

下面是applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <p:beans 
 3     xmlns:p="http://www.springframework.org/schema/beans" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xmlns:context="http://www.springframework.org/schema/context" 
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="
 9     http://www.springframework.org/schema/beans 
10     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
11     http://www.springframework.org/schema/context 
12     http://www.springframework.org/schema/context/spring-context-4.2.xsd 
13     http://www.springframework.org/schema/aop 
14     http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
15     http://www.springframework.org/schema/tx 
16     http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
17     ">
18     
19 
20     
21  <!-- 加载Properties文件 -->  
22     <p:bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
23         <p:property name="locations">  
24             <p:list>  
25                 <p:value>classpath:mail.properties</p:value>  
26             </p:list>  
27         </p:property>  
28     </p:bean>  
29     <p:bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">  
30         <p:property name="from">  
31             <p:value>${mail.from}</p:value>  
32         </p:property>  
33         <!-- 查看SimpleMailMessage源码还可以注入标题,内容等 -->  
34     </p:bean>  
35     <!-- 申明JavaMailSenderImpl对象 -->  
36 
37       <p:bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
38         <p:property name="defaultEncoding" value="UTF-8" />  
39         <p:property name="host" value="${mail.host}" />  
40         <p:property name="username" value="${mail.username}" />  
41         <p:property name="password" value="${mail.password}" />  
42         <p:property name="javaMailProperties">  
43             <p:props>  
44                 <!-- 设置认证开关 -->  
45                 <p:prop key="mail.smtp.auth">true</p:prop>  
46                 <!-- 启动调试开关 -->  
47                 <p:prop key="mail.debug">true</p:prop>  
48                 <!-- 设置发送延时 -->
49                 <p:prop key="mail.smtp.timeout">0</p:prop>
50             </p:props>  
51         </p:property>  
52     </p:bean>  
53  
54     
55 </p:beans>

 

测试代码

 1 package com.carlos;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 import org.springframework.mail.MailSender;
 6 import org.springframework.mail.SimpleMailMessage;
 7 import org.springframework.messaging.MessagingException;
 8 
 9 public class SingleMailSend {
10 
11     
12     
13      
14     public static void main(String args[]) throws MessagingException {
15         ApplicationContext actx = new ClassPathXmlApplicationContext(
16                 "applicationContext.xml");
17         SimpleMailMessage mailMessage = (SimpleMailMessage) actx.getBean("mailMessage");
18         MailSender sender = (MailSender) actx.getBean("mailSender");
19         mailMessage.setSubject("你好");
20         mailMessage.setText("这个是一个通过Spring框架来发送邮件的小程序");
21         mailMessage.setTo("473/996/@qq.com");
22         sender.send(mailMessage);
23         
24     }
25 }

 

 

jar spring挺多jar包,还有就是mail jar包

目录结构

 

posted @ 2016-07-26 00:15  树下野鹿  阅读(161)  评论(0编辑  收藏  举报