一,启用smtp
我用的163邮箱。
记得新增授权码
二,pom
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </dependency>
三,application.xml
<!-- 邮件配置 Begin--> <bean id="javaMail" class="com.kintech.common.utils.JavaMail" autowire="byType" /> <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl" p:host="smtp.163.com" p:port="25" p:protocol="smtp" p:username="邮箱" p:password="授权码"> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" p:templateLoaderPath="classpath:mailTemplate/"> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">1800</prop> <prop key="default_encoding">UTF-8</prop> <prop key="locale">zh_CN</prop> </props> </property> </bean> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" p:corePoolSize="10" p:maxPoolSize="30" /> <!-- 邮件配置 End-->
四,java mail 工具类
package com.kintech.common.utils; import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.apache.log4j.Logger; import org.springframework.core.io.ClassPathResource; import org.springframework.core.task.TaskExecutor; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import com.kintech.common.utils.log.LogerHelper; import freemarker.template.Template; /** * 发送邮件工具类 * @author helka067 */ @SuppressWarnings(value={"rawtypes","unchecked"}) public class JavaMail { private static Logger logger = Logger.getLogger(JavaMail.class); private JavaMailSenderImpl sender; private FreeMarkerConfigurer freeMarkerConfigurer; private TaskExecutor taskExecutor;// 拥有异步执行能力的任务执行器 public JavaMail(JavaMailSenderImpl sender, FreeMarkerConfigurer freeMarkerConfigurer, TaskExecutor taskExecutor) { super(); this.sender = sender; this.freeMarkerConfigurer = freeMarkerConfigurer; this.taskExecutor = taskExecutor; } public void sendMail(String[] receiver ,String subject,String content) throws MessagingException { MimeMessage msg = sender.createMimeMessage(); //MimeMessageHelper helper = new MimeMessageHelper(msg,false,"utf-8"); MimeMessageHelper helper = new MimeMessageHelper(msg,true,"utf-8"); helper.setFrom(sender.getUsername()); // String recipients = PropertiesUtil.getEnvField("recipients");// helper.setTo(receiver); helper.setSubject("Subject:Test"); helper.setSentDate(DateUtil.getCurDate()); String htmlText="content test"; helper.setText(htmlText, true); sender.send(msg); } //异步发送 public void sendAsyncMail() { taskExecutor.execute(new Runnable() { public void run() { try { sendMail(......);// 异步调用 logger.info("邮件发送成功!"); } catch (Exception e) { logger.error("邮件发送失败!,异常信息:" + LogerHelper.getPrintStr(e)); } } }); } //演示发送附件 public void sendTemplateMail() throws MessagingException { MimeMessage msg = sender.createMimeMessage(); //MimeMessageHelper helper = new MimeMessageHelper(msg,false,"utf-8"); MimeMessageHelper helper = new MimeMessageHelper(msg,true,"utf-8"); helper.setFrom(sender.getUsername()); String recipients = PropertiesUtil.getEnvField("recipients");// helper.setTo(recipients.split(",")); helper.setSubject("Subject"); helper.setSentDate(DateUtil.getCurDate()); String htmlText = "content";// 使用模板产生HTML 邮件体内容 helper.setText(htmlText, true); // 增加附件 File img = new File("/fgdf.jpg"); helper.addInline("myImg", img); /*File file = new File("xxx.zip"); helper.addAttachment("xxx.zip", file); */ ClassPathResource file2 = new ClassPathResource("log4j\\log4j.properties"); helper.addAttachment("log4j.properties", file2); sender.send(msg); } }
五,调用
@RequestMapping(value = "test2", method = RequestMethod.POST) public String test2(JavaMailRequest request) throws Exception { jml.sendMail(request.getReceiver(),request.getSubject(),request.getContent()); return "ok"; }
六,结果