使用Spring实现邮件发送

来源:互联网 发布:python 爬虫要学多久 编辑:程序博客网 时间:2024/05/22 06:42

查看原文:http://www.ibloger.net/article/290.html


可以使用Spring方式、javax.mail-1.4.4.jar方式使用等等,这里的spring方式也用到了javax.mail-1.4.4.jar包

javax.mail参考文章:http://www.blogjava.net/wangfun/archive/2009/04/15/265748.html或http://jingyan.baidu.com/article/d713063501fc2d13fdf475e6.html

Spring方式用到spring-context-support-4.0.7.RELEASE.jar中的SimpleMailMessage类和JavaMailSenderImpl类

经测试,QQ邮间顺利接收,本实例实用普通Java类实现 ,实际项目中使用配置文件指定参数,这里实例使用硬编码的方式测试
参考大牛文献:http://www.cnblogs.com/codeplus/archive/2011/11/03/2232893.html

也可以集成在Spring配置中,参考其他人文章http://blog.csdn.net/thc1987/article/details/5812567

发送普通文本

package com.rapido.test;import java.io.File;import java.util.Properties;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;public class SendEmail {/** * 发送简单文本内容 */public static void simpleContents(){/** 服务器设置  */JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();senderImpl.setHost("smtp.qq.com");// 设定邮件服务器地址senderImpl.setPort(25); // 设置邮件服务器端口(可省略),smtp端口统一(25),POP3(110)senderImpl.setUsername("1056856191"); // 根据自己的情况,设置username,senderImpl.setPassword("xiaokui88521");// 根据自己的情况, 设置password/** 邮件信息设置   */SimpleMailMessage message = new SimpleMailMessage();// 建立邮件消息// 设置多个收件人,也可以单独发送给一个人:simpleMessage.setTo("10086@qq.com")String[] array = new String[] {"1056856191@qq.com", "zbwzxm@qq.com" };message.setTo(array);message.setFrom("1056856191@qq.com");// 设置发送人message.setSubject("this is a send message test for X-rapdio!"); // 邮件标题message.setText("X-rapido is Pretty boy ,是大帅哥"); // 内容/** 其他配置 */Properties prop = new Properties(); // 可省略prop.put("mail.smtp.auth", "true"); // 服务器进行认证,认证用户名和密码是否正确(可省略)prop.put("mail.smtp.timeout", "25000"); // 可省略senderImpl.setJavaMailProperties(prop); // 可省略senderImpl.send(message); // 发送}/** * 发送带附件内容 */public static void simpleContentsWithAttached(){/** 服务器设置  */JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();senderImpl.setHost("smtp.qq.com");senderImpl.setPort(25);  senderImpl.setUsername("1056856191@qq.com");  senderImpl.setPassword("xiaokui88521"); /** 邮件信息设置   */MimeMessage msg = senderImpl.createMimeMessage();//MimeMessage,支付更加复杂的邮件格式和内容 try {MimeMessageHelper helper = new MimeMessageHelper(msg, true); //创建MimeMessageHelper对象,处理MimeMessage的辅助类  //使用辅助类MimeMessage设定参数  helper.setFrom(senderImpl.getUsername());  // 发送人helper.setTo("1056856191@qq.com");  helper.setSubject("带附件的邮件内容发送");  helper.setText("<h1 style='color:#FF6666'>The X-rapido is a Handsome Boy! year, this's right!</h1><img src='cid:file'/>",true);File file = new File("D:\\Pretty Boy.png");helper.addAttachment("X-rapido.png", file);// 加入附件,这里的名称可以更改,但后缀一定要加上} catch (MessagingException e) {e.printStackTrace();}  senderImpl.send(msg);  // 发送}public static void main(String[] args) {//simpleContents();simpleContentsWithAttached();System.out.println(" 邮件发送成功.. ");}}
效果图


发送富文本内容

其他内容,比如发送富文本内容,把发送内容设置如下

helper.setSubject("发送富文本内容");  // 第二个参数true,表示text的内容为html,否则发送的都是字符串内容了// 然后注意<img/>标签,src='cid:file','cid'是contentId的缩写,'file'是一个标记,需要调用MimeMessageHelper的addInline方法替代成文件  helper.setText("<h1 style='color:#FF6666'>The X-rapido is a Handsome Boy! year, this's right!</h1><img src='cid:file'/>",true);File file = new File("D:\\Boy.png");helper.addInline("file", file);
效果图


发送Velocity模版内容

发送velocity模版另外需要用到有velocity-1.7.jar;commons-logging-1.2.jar;commons-collections-3.2.1.jar;commons-lang-2.6.jar的支持.
velocity下载地址:http://velocity.apache.org/download.cgi
导入以后定制index.vm内容,我的位于com.rapido.template包下
<html><head><style type="text/css">h4{color:red;  background:#efefef;  }</style></head><body><h4>${user} </h4><p><p><i>${content}</i><p>这是一个Velocity模版方式进行邮件发送...</p></body></html>

看起来貌似很容易理解,只是普通的Html文件,使用了一些${placeholder}作为占位符。

Java要做的,就是加载模板,并将相应的值插入到占位符当中。

/** * 发送Velocity模版内容 */public static void sendTemplateByVelocity(){/** 服务器设置  */JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();senderImpl.setHost("smtp.qq.com");senderImpl.setPort(25);  senderImpl.setUsername("1056856191@qq.com");  senderImpl.setPassword("xiaokui88521"); // 声明Map对象,并填入用来填充vm模板文件的键值对  Map<String, Object> model = new HashMap<String, Object>();  model.put("user", "X-rapdio");  model.put("content", "<h1 style='color:#FF6666'>The X-rapido is a Handsome Boy! year, this's right!</h1>"); MimeMessage msg = senderImpl.createMimeMessage();//MimeMessage,支付更加复杂的邮件格式和内容 // Spring提供的VelocityEngineUtils将模板进行数据填充,并转换成普通的String对象  // Velocity的参数,通过VelocityEngineFactoryBean创建VelocityEngine,也是推荐在配置文件中配置的  Properties props = System.getProperties();  props.put("resource.loader", "class");  props.put("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");  VelocityEngineFactoryBean v = new VelocityEngineFactoryBean();  v.setVelocityProperties(props);try {VelocityEngine velocityEngine = v.createVelocityEngine();String emailText = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "com/template/vm/index.vm", model);  // 和上面一样的发送邮件的工作  MimeMessageHelper helper = new MimeMessageHelper(msg, true);  helper.setFrom(senderImpl.getUsername());  helper.setTo("1056856191@qq.com");  helper.setSubject("发送Velocity模版的文本内容");  helper.setText(emailText, true);}catch (Exception e) {e.printStackTrace();}senderImpl.send(msg);  // 发送}
效果图


Spring可谓是大大简化了邮件的发送步骤,虽然我们自己封装可能实现起来并不复杂,但是,有现成的有何必要重新造轮子呢?  ——.net






1 0