您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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 #用戶名 spring.mail.username=9118542413@qq.com #密碼,如果是qq的,要申請臨時授權碼 spring.mail.password=faw124awfawfawg spring.mail.default-encoding=UTF-8 #以誰來發送郵件 mail.fromMail.addr=9118542413@qq.com
發送各種類型的郵件
@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("15017263512@163.com", "主題:這是模板郵件", emailContent); } }
測試類:
@RunWith(SpringRunner.class) @SpringBootTest public class EmailServiceTest { @Autowired MailService mailService; @Test public void sendSimpleMailTest() { mailService.sendSimpleMail("15017263512@163.com","test simple mail"," hello this is simple mail"); } @Test public void sendHtmlMailTest(){ String content="<html>\n" + "<body>\n" + " <h4>hello world ! 這是一封Html郵件!</h4>\n" + "</body>\n" + "</html>"; mailService.sendHtmlMail("15017263512@163.com","test html mail",content); } @Test public void sendAttachmentsMail(){ String filePath="E:\\var\\log\\elkTest\\error\\2018-11-30.log"; mailService.sendAttachmentsMail("15017263512@163.com", "主題:帶附件的郵件", "有附件,請查收!", 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("15017263512@163.com", "主題:這是有圖片的郵件", content, imgPath, rscId); } @Test public void sendTemplateMail() { //創建郵件正文 Context context = new Context(); context.setVariable("id", "006"); mailService.sendTemplateMail("15017263512@163.com","主題:這是模板郵件", "emailTemplate",context); } }
關于SpringBoot集成E-mail發送各種類型郵件的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。