如果是发送纯文本邮件不带附件,可以直接用SimpleMailMessage
例:
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);
}
}
}
}