使用Spring JavaMail发送邮件

一直以来都是用javax.mail来写email发送,看了spring的javamail之后才知道javax.mail臃肿繁杂。

下面总结一下spring javamail的应用:


首先导入使用jar包:activation.jar,commons-logging-1.0.4.jar,mail.jar,spring.jar

 

1、使用xml配置javamail:
在classpath底下新建application-mail.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.       xmlns:aop="http://www.springframework.org/schema/aop"  
5.       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
6.       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  
7.    <!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->  
8.    <bean id="mailSender"  
9.  class="org.springframework.mail.javamail.JavaMailSenderImpl">  
10.        <property name="host">  
11.            <value>smtp.163.com</value>  
12.        </property>  
13.        <property name="javaMailProperties">  
14.            <props>  
15.                <prop key="mail.smtp.auth">true</prop>  
16.                <prop key="mail.smtp.timeout">25000</prop>  
17.            </props>  
18.        </property>  
19.        <property name="username">  
20.            <value>[email protected]</value>  
21.        </property>  
22.        <property name="password">  
23.            <value>xxxxxx</value>  
24.        </property>  
25.    </bean>  
26.</beans>  

 

2、发送Email类

1.public class SendMail {   
2. public ApplicationContext ctx = null;   
3. public SendMail() {   
4.  //获取上下文   
5.  ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");   
6. }   
7. public void send() {   
8.  //获取JavaMailSender bean   
9.  JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");   
10.  SimpleMailMessage mail = new SimpleMailMessage(); //<SPAN style="COLOR: #ff0000">注意SimpleMailMessage只能用来发送text格式的邮件</SPAN>   
11.  
12.  try {   
13.   mail.setTo("[email protected]");//接受者   
14.   mail.setFrom("[email protected]");//发送者,这里还可以另起Email别名,不用和xml里的username一致   
15.   mail.setSubject("spring mail test!");//主题   
16.   mail.setText("springMail的简单发送测试");//邮件内容   
17.   sender.send(mail);   
18.  } catch (Exception e) {   
19.   e.printStackTrace();   
20.  }   
21. }  

 

 发送html格式的Email:

1.public class SendMail {   
2. public ApplicationContext ctx = null;   
3. public SendMail() {   
4.  //获取上下文   
5.  ctx = new ClassPathXmlApplicationContext("applicationContext.xml");   
6. }   
7. public void send() {   
8.  //获取JavaMailSender bean   
9.  JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");   
10.  JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();   
11.  MimeMessage mailMessage = senderImpl.createMimeMessage();   
12.  //设置utf-8或GBK编码,否则邮件会有乱码   
13.  MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");   
14.  try {   
15.   messageHelper.setTo(email.getEmail());//接受者      
16.   messageHelper.setFrom("[email protected]");//发送者   
17.   messageHelper.setSubject("测试邮件");//主题   
18.   //邮件内容,注意加参数true,表示启用html格式   
19.   messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);   
20.   sender.send(mailMessage);   
21.  } catch (Exception e) {   
22.   e.printStackTrace();   
23.  }   
24. }  

 发送html格式并带有附件的Email:

1.public class SendMail {   
2. public ApplicationContext ctx = null;   
3. public SendMail() {   
4.  //获取上下文   
5.  ctx = new ClassPathXmlApplicationContext("applicationContext.xml");   
6. }   
7. public void send() {   
8.  //获取JavaMailSender bean   
9.  JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");   
10.  JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();   
11.  MimeMessage mailMessage = senderImpl.createMimeMessage();   
12.  //设置utf-8或GBK编码,否则邮件会有乱码   
13.  MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");   
14.  try {   
15.   messageHelper.setTo(email.getEmail());//接受者      
16.   messageHelper.setFrom("[email protected]");//发送者   
17.   messageHelper.setSubject("测试邮件");//主题   
18.   //邮件内容,注意加参数true   
19.   messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);   
20.   //附件内容   
21.   messageHelper.addInline("a", new File("E:/xiezi.jpg"));   
22.   messageHelper.addInline("b", new File("E:/logo.png"));    
23.   File file=new File("E:/测试中文文件.rar");     
24.   // 这里的方法调用和插入图片是不同的,使用MimeUtility.encodeWord()来解决附件名称的中文问题   
25.   messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);    
26.   sender.send(mailMessage);   
27.  } catch (Exception e) {   
28.   e.printStackTrace();   
29.  }   
30. }  

 

你可能感兴趣的:(spring,AOP,xml,bean,qq)