Spring中默認有starter實現(xiàn)類Mail
依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
配置文件
# JavaMailSender 郵件發(fā)送的配置
spring.mail.host=smtp.qq.com
spring.mail.username=用戶qq郵箱
#QQ郵箱的授權(quán)碼
spring.mail.password=密碼
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8
簡單郵件
public void sendSimpleMail(){
try{
SimpleMaileMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage .setFrom("sendFrom@qq.com");
simpleMailMessage .setTo("sendTo@qq.com");
simpleMailMessage.setSubject("主題");
simpleMailMessage.setText("內(nèi)容");
javaMailSender.send(simpleMailMessage);//發(fā)送
}catch(Exception e){
logger.error("發(fā)送失敗",e.getMessage());
}
}
自定義發(fā)件人名稱
String nick = "";
try {
nick = javax.mail.internet.MimeUtility.encodeText("張三");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 設(shè)置發(fā)件人
mimeMsg.setFrom(new InternetAddress("sendFrom@qq.com", nick));
html格式郵件
public void sendHTMLMail(MailBean mailBean) {
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
mimeMessageHelper.setText("<h1>hello</h1>", true);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
logger.error("郵件發(fā)送失敗", e.getMessage());
}
}
帶附件格式的郵件
public void sendAttachmentMail(MailBean mailBean) {
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
mimeMessageHelper.setText(mailBean.getContent());
//文件路徑 spring的FileSystemResource,使用絕對路徑訪問文件資源
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/mail.png"));
mimeMessageHelper.addAttachment("mail.png", file);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
logger.error("郵件發(fā)送失敗", e.getMessage());
}
}
發(fā)送帶靜態(tài)資源的郵件
public void sendInlineMail(MailBean mailBean) {
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
mimeMessageHelper.setText("<html><body>帶靜態(tài)資源的郵件內(nèi)容,這個一張IDEA配置的照片:<img src='cid:picture' /></body></html>", true);
//文件路徑
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/mail.png"));
mimeMessageHelper.addInline("picture", file);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
logger.error("郵件發(fā)送失敗", e.getMessage());
}
}
基于Freemarker的模板郵件
public void sendTemplateMail(MailBean mailBean) {
MimeMessage mimeMailMessage = null;
try {
mimeMailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
mimeMessageHelper.setFrom(MAIL_SENDER);
mimeMessageHelper.setTo(mailBean.getRecipient());
mimeMessageHelper.setSubject(mailBean.getSubject());
Map<String, Object> model = new HashMap<String, Object>();
model.put("content", mailBean.getContent());
model.put("title", "標題Mail中使用了FreeMarker");
Template template = configuration.getTemplate("mail.ftl");
String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
mimeMessageHelper.setText(text, true);
javaMailSender.send(mimeMailMessage);
} catch (Exception e) {
logger.error("郵件發(fā)送失敗", e.getMessage());
}
}
動態(tài)添加郵箱配置信息
和之前使用接口javaMailSender
不同涣楷,這次使用的是接口的實現(xiàn)類揩晴。
使用JavaMailSenderImpl
public void sendSimpleMail() throws Exception {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();//直接生產(chǎn)一個實例
String users[] = {"xxxxx@qq.com","xxxxx@126.com"};
mailSender.setHost("smtp.126.com");//動態(tài)添加配置
mailSender.setPassword("xxxxx");
mailSender.setPort(25);
mailSender.setProtocol("smtp");
mailSender.setUsername("xxxxx");
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("xxxxxxx");
message.setTo(users); // 使用數(shù)組的形式還可以群發(fā)
message.setSubject("羽毛球比賽");
message.setText("報名參加羽毛球賽");
mailSender.send(message);
}