yx: SpringBoot发送邮件功能
2017-07-27 21:33
477 查看
如果是发送纯文本邮件不带附件,可以直接用SimpleMailMessage
如果需要发送带附件的邮件,必须用Spring的JavaMailSender及MimeMessage。
下面的代码展示了附件以及邮件模板的应用。
例: import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.SimpleMailMessage; @Autowired private JavaMailSender mailSender; public void sendSimpleTextMail(String from, String[] to, String subject, String content){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); this.mailSender.send(message); }
如果需要发送带附件的邮件,必须用Spring的JavaMailSender及MimeMessage。
下面的代码展示了附件以及邮件模板的应用。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Locale; import java.util.Map; @Service public class MailTemplateService { @Autowired private JavaMailSender mailSender; @Autowired @Qualifier("email") private TemplateEngine templateEngine; /** * 发送不带附件的纯文本邮件 * @param from * @param to * @param subject * @param content * @throws MessagingException */ public void sendSimpleTextMail(String from, String[] to, String subject, String content) throws MessagingException { sendSimpleTextMailWithAttachments(from, to, subject,content, null); } /** * 发送带附件的纯文本邮件 * @param from * @param to * @param subject * @param content * @param attachments * @throws MessagingException */ public void sendSimpleTextMailWithAttachments(String from, String[] to, String subject, String content, String[] attachments) throws MessagingException { final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); setMailSubjectFromTo(message, subject, from, to); message.setText(content); setMailAttachments(message,attachments); this.mailSender.send(mimeMessage); } /** * 根据thymeleaf模板发送不带附件的邮件 * @param subject * @param from * @param to * @param templateName - 模板文件,需要放到 src/main/resources/mail/目录下,且后缀名必须为 .html * @param params - 模板文件中定义的参数 * @throws MessagingException */ public void sendTemplateMail(String subject, String from, String[] to,String templateName, Map<String, Object> params) throws MessagingException { sendTemplateMailWithAttachments(subject, from, to, templateName, params,null); } /** * 根据thymeleaf模板发送带附件的邮件 * @param subject * @param from * @param to * @param templateName - 模板文件,需要放到 src/main/resources/mail/目录下,且后缀名必须为 .html * @param params - 模板文件中定义的参数 * @param attachments -附件 * @throws MessagingException */ public void sendTemplateMailWithAttachments(String subject, String from, String[] to,String templateName, Map<String, Object> params, String[] attachments) throws MessagingException { final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); setMailSubjectFromTo(message, subject, from, to); setMailTemplate(message, templateName, params); setMailAttachments(message,attachments); this.mailSender.send(mimeMessage); } /** * 设置邮件的subject,from,to * @param message * @param subject * @param from * @param to * @throws MessagingException */ private void setMailSubjectFromTo(MimeMessageHelper message,String subject, String from, String[] to) throws MessagingException { message.setSubject(subject); message.setFrom(from); message.setTo(to); } /** * 设置邮件的thymeleaf模板 * @param message * @param templateName * @param params * @throws MessagingException */ private void setMailTemplate(MimeMessageHelper message,String templateName, Map<String, Object> params) throws MessagingException { final Context context = new Context(Locale.CHINA); if(templateName!=null && !"".equals(templateName) && params!=null) { for (Map.Entry<String, Object> entry : params.entrySet()) { context.setVariable(entry.getKey(), entry.getValue()); } // Create the HTML body using Thymeleaf final String htmlContent = this.templateEngine.process(templateName, context); message.setText(htmlContent, true); // true = isHtml } } /** * 设置邮件的附件 * @param message * @param attachments * @throws MessagingException */ private void setMailAttachments(MimeMessageHelper message, String[] attachments) throws MessagingException { if(attachments!=null) { for (String attachment : attachments) { FileSystemResource file = new FileSystemResource(new File(attachment)); String fileName = attachment.substring(attachment.lastIndexOf(File.separator)+1); message.addAttachment(fileName, file); } } } }
相关文章推荐
- springboot 实现邮件发送功能
- SpringBoot中发送QQ邮件功能的实现代码
- Spring Boot 发送邮件功能案例分析
- SpringBoot + Thymeleaf实现邮件发送功能
- springboot实现添加邮件发送及压缩功能
- springboot邮件发送功能和两个常见问题
- SpringBoot之发送邮件功能
- javaMail 的邮件发送包括了抄送(CC),密送(BCC)采用springBoot
- SpringBoot添加Email发送功能及常见异常
- springboot 普通发送邮件 和 freemarker模板发送邮件
- Spring Boot发送邮件
- Spring学习笔记3之消息队列(rabbitmq)发送邮件功能
- SpringBoot中实现邮件找回密码的功能
- Spring实现JavaMail邮件发送功能
- Spring Boot (教程十四:发送邮件)
- Spring实现发送邮件功能(简易篇)
- Spring Boot中使用JavaMailSender发送邮件
- springboot发送邮件
- spring- boot 发送邮件接口+freemarker视图模板引擎
- Spring Boot发送邮件详解