Java异步邮件工具类SpringMailUtil2.0

2017-01-10· 4145 次浏览
很给力的邮件发送工具类,支持线程池异步发送。 JavaWeb工具类目录 [http://baike.xsoftlab.net/view/1059.html](http://baike.xsoftlab.net/view/1059.html) 源码: ``` package com.itshidu.ssh.commons.util; import java.io.File; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.task.TaskExecutor; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; /** * 快速发送邮件 * @author Mr.Xia v1.0 create:2016.03.29 http://www.zhenzhigu.com * @author Mr.Xia v2.0 create:2017.01.11 http://www.zhenzhigu.com */ @Component public class MailUtil { //Spring不支持静态属性注入 private static JavaMailSenderImpl sender; private static TaskExecutor executor; @Autowired public void setSender(JavaMailSenderImpl jmsi){ MailUtil.sender = jmsi; } @Autowired public void setExecutor(TaskExecutor executor){ MailUtil.executor = executor; } public static void addSendMailTask(final MimeMessage mimeMessage) { try { executor.execute(new Runnable() { public void run(){ sender.send(mimeMessage); } }); } catch (Exception e) { e.printStackTrace(); } } /** * 发送带附件的Mime邮件,可指定附件名 * @param to * @param subject * @param text * @param fileNames[] * @param files[] * @param asyn 是否异步 * @throws MessagingException */ public static void send(String to,String subject,String text,String[] fileNames,File[] files,boolean asyn) throws MessagingException{ //使用JavaMail的MimeMessage,支付更加复杂的邮件格式和内容 MimeMessage msg = sender.createMimeMessage(); //创建MimeMessageHelper对象,处理MimeMessage的辅助类 MimeMessageHelper helper = new MimeMessageHelper(msg, true); //使用辅助类MimeMessage设定参数 helper.setFrom(sender.getUsername()); helper.setTo(to); helper.setSubject(subject); helper.setText(text,true); //加入附件 if(files!=null&&files.length>0){ for(int i=0;i<files.length;i++){ if(fileNames!=null&&i<fileNames.length&&fileNames[i]!=null){ helper.addAttachment(fileNames[i], files[i]); }else{ helper.addAttachment(files[i].getName(), files[i]); } } } if(asyn){ //使用线程池异步发送 addSendMailTask(msg); }else{ sender.send(msg); } } /** * 发送带单个附件的Mime邮件,可指定附件名 * @param to * @param subject * @param text * @param file * @param asyn 是否异步 * @throws MessagingException */ public static void send(String to,String subject,String text,String fileName,File file,boolean asyn) throws MessagingException{ send(to, subject, text, new String[]{fileName}, new File[]{file},asyn); } /** * 发送带附件的Mime邮件 * @param to * @param subject * @param text * @param fileNames[] * @param files[] * @param asyn 是否异步 * @throws MessagingException */ public static void send(String to,String subject,String text,File[] files,boolean asyn) throws MessagingException{ send(to, subject, text, null, files,asyn); } /** * 发送带单个附件的Mime邮件,使用默认附件名 * @param to * @param subject * @param text * @param file * @param asyn 是否异步 * @throws MessagingException */ public static void send(String to,String subject,String text,File file,boolean asyn) throws MessagingException{ send(to, subject, text, file==null? null:file.getName(), file,asyn); } /** * 发送无附件的mime邮件 * @param to * @param subject * @param text * @param asyn 是否异步 * @throws MessagingException */ public static void send(String to,String subject,String text,boolean asyn) throws MessagingException{ send(to, subject, text, new File[]{},asyn); } } ``` applicationContext.xml ``` <!-- JavaMail相关配置 --> <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" > <property name="host" value="smtp.zhenzhigu.com"></property> <property name="username" value="xxx@itshidu.com"></property> <property name="password" value="xxxxxx"></property> <property name="protocol" value="smtp"></property> <property name="defaultEncoding" value="utf-8"></property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.from">xxx@itshidu.com</prop> <prop key="mail.debug">false</prop> </props> </property> </bean> <!-- 用于异步发送邮件的线程池 --> <bean id="mailTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 核心线程数 --> <property name="corePoolSize" value="10" /> <!-- 最大线程数 --> <property name="maxPoolSize" value="50" /> <!-- 最大队列数 --> <property name="queueCapacity" value="10000" /> <!-- 线程池维护线程所允许的空闲时间 --> <property name="keepAliveSeconds" value="60" /> </bean> ``` 使用方法 ``` SpringMailUtil.send("shoujianren@itshidu.com", "测试邮件", "测试一下新的邮件工具类", new File[]{ new File("d:/abc/单词表.txt"), new File("d:/abc/book.xml"), new File("d:/abc/笔记-零配置.txt") },true); ``` 支持html格式,也可以在邮件中添加附件 最后一个参数代表是否使用线程池进行异步发送,true使用,false不使用