SpringBoot 整合 E-mail傳送各種型別的郵件
阿新 • • 發佈:2018-12-23
SpringBoot 整合 E-mail傳送郵件
JDK本身有自帶傳送郵件api,加上SpringBoot在進行封裝,使得現在使用起來十分快速簡潔。
話不多說,參考純潔的微笑部落格,更改jar版本為2.0.4 開幹,基本沒什麼坑。
就是配置郵箱賬號密碼是,如果是qq郵箱,需要開啟PO30和STMP服務,並且獲取臨時授權碼。
開啟服務連結:
https://mail.qq.com/cgi-bin/frame_html?sid=a5ZSbreeNm9pHyl1&r=a83225170e94773c650a460c10f7a05c
與Springboot整合
匯入jar包
compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '2.0.4.RELEASE' compile 'org.springframework.boot:spring-boot-starter-thymeleaf:2.0.4.RELEASE'
配置郵箱
#郵箱伺服器地址,各大運營商不同
spring.mail.host=smtp.qq.com
#使用者名稱
[email protected]
#密碼,如果是qq的,要申請臨時授權碼
spring.mail.password=faw124awfawfawg
spring.mail.default-encoding=UTF-8
#以誰來發送郵件
[email protected]
傳送各種型別的郵件
@Service
@Slf4j
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
/**
* The Template engine.
*/
@Autowired
TemplateEngine templateEngine;
@Value("${mail.fromMail.addr}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
log.info("簡單郵件已經發送。");
} catch (Exception e) {
log.error("傳送簡單郵件時發生異常!", e);
}
}
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要建立一個multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
log.info("html郵件傳送成功");
} catch (MessagingException e) {
log.error("傳送html郵件時發生異常!", e);
}
}
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
helper.addAttachment(fileName, file);
mailSender.send(message);
log.info("帶附件的郵件已經發送。");
} catch (MessagingException e) {
log.error("傳送帶附件的郵件時發生異常!", e);
}
}
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
log.info("嵌入靜態資源的郵件已經發送。");
} catch (MessagingException e) {
log.error("傳送嵌入靜態資源的郵件時發生異常!", e);
}
}
@Override
public void sendTemplateMail(String to, String subject, String template, Context context) {
String emailContent = templateEngine.process(template, context);
sendHtmlMail("[email protected]", "主題:這是模板郵件", emailContent);
}
}
測試類:
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailServiceTest {
@Autowired
MailService mailService;
@Test
public void sendSimpleMailTest() {
mailService.sendSimpleMail("[email protected]","test simple mail"," hello this is simple mail");
}
@Test
public void sendHtmlMailTest(){
String content="<html>\n" +
"<body>\n" +
" <h3>hello world ! 這是一封Html郵件!</h3>\n" +
"</body>\n" +
"</html>";
mailService.sendHtmlMail("[email protected]","test html mail",content);
}
@Test
public void sendAttachmentsMail(){
String filePath="E:\\var\\log\\elkTest\\error\\2018-11-30.log";
mailService.sendAttachmentsMail("[email protected]", "主題:帶附件的郵件", "有附件,請查收!", filePath);
}
@Test
public void sendInlineResourceMail() {
String rscId = "neo006";
String content="<html><body>這是有圖片的郵件:<img src=\'cid:" + rscId + "\' ></body></html>";
String imgPath = "C:\\Users\\Admin\\Pictures\\Camera Roll\\9499189867_1476052069.jpg";
mailService.sendInlineResourceMail("[email protected]", "主題:這是有圖片的郵件", content, imgPath, rscId);
}
@Test
public void sendTemplateMail() {
//建立郵件正文
Context context = new Context();
context.setVariable("id", "006");
mailService.sendTemplateMail("[email protected]","主題:這是模板郵件",
"emailTemplate",context);
}
}
上面的郵箱和密碼是我亂填的,注意自己更改。
專案原始碼以上傳至GitHub:https://github.com/liaozihong/SpringBoot-Learning/tree/master/SpringBoot-Email