论坛首页 入门技术论坛

使用Spring JavaMail发送邮件总结

浏览 13459 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-13   最后修改:2010-01-30

一直以来都是用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,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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">
    <!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
    <bean id="mailSender"
  class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host">
            <value>stmp.163.com</value>
        </property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.timeout">25000</prop>
            </props>
        </property>
        <property name="username">
            <value>xxxx@163.com</value>
        </property>
        <property name="password">
            <value>xxxxxx</value>
        </property>
    </bean>
</beans>

  或者把以上的Beans配置到applicaiont.xml里面也可以。

 

2、发送Email类:

public class SendMail {
 public ApplicationContext ctx = null;
 public SendMail() {
  //获取上下文
  ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
 }
 public void send() {
  //获取JavaMailSender bean
  JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
  SimpleMailMessage mail = new SimpleMailMessage(); //注意SimpleMailMessage只能用来发送text格式的邮件


  try {
   mail.setTo("xxx@qq.com");//接受者
   mail.setFrom("xxxx@163.com");//发送者,这里还可以另起Email别名,不用和xml里的username一致
   mail.setSubject("spring mail test!");//主题
   mail.setText("springMail的简单发送测试");//邮件内容
   sender.send(mail);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 

 发送html格式的Email:

 

public class SendMail {
 public ApplicationContext ctx = null;
 public SendMail() {
  //获取上下文
  ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 }
 public void send() {
  //获取JavaMailSender bean
  JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
  JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
  MimeMessage mailMessage = senderImpl.createMimeMessage();
  //设置utf-8或GBK编码,否则邮件会有乱码
  MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
  try {
   messageHelper.setTo(email.getEmail());//接受者   
   messageHelper.setFrom("xxx@163.com");//发送者
   messageHelper.setSubject("测试邮件");//主题
   //邮件内容,注意加参数true,表示启用html格式
   messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
   sender.send(mailMessage);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 

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

public class SendMail {
 public ApplicationContext ctx = null;
 public SendMail() {
  //获取上下文
  ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 }
 public void send() {
  //获取JavaMailSender bean
  JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
  JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
  MimeMessage mailMessage = senderImpl.createMimeMessage();
  //设置utf-8或GBK编码,否则邮件会有乱码
  MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
  try {
   messageHelper.setTo(email.getEmail());//接受者   
   messageHelper.setFrom("xxx@163.com");//发送者
   messageHelper.setSubject("测试邮件");//主题
   //邮件内容,注意加参数true
   messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
   //附件内容
   messageHelper.addInline("a", new File("E:/xiezi.jpg"));
   messageHelper.addInline("b", new File("E:/logo.png")); 
   File file=new File("E:/测试中文文件.rar");  
   // 这里的方法调用和插入图片是不同的,使用MimeUtility.encodeWord()来解决附件名称的中文问题
   messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file); 
   sender.send(mailMessage);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics