一、發送簡單文本郵件
在Springboot中,可以使用JavaMailSender類很方便地發送簡單的文本郵件。首先,需要在其配置文件中添加郵件配置,如下所示:
spring.mail.host=smtp.qq.com spring.mail.username=YOUR_USERNAME@qq.com spring.mail.password=YOUR_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.properties.mail.smtp.ssl.enable=true spring.mail.default-encoding=UTF-8
然後,在需要發送郵件的地方,可以使用如下代碼來發送簡單文本郵件:
@Autowired private JavaMailSender mailSender; public void sendSimpleEmail() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("YOUR_USERNAME@qq.com"); message.setTo("recipient@example.com"); message.setSubject("Subject"); message.setText("Body"); mailSender.send(message); }
二、發送HTML郵件
如果需要發送帶有HTML標籤的郵件,可以使用MimeMessageHelper類來實現。具體步驟如下:
- 在郵件配置文件中添加HTML郵件配置
- 使用MimeMessageHelper類構造HTML郵件內容
- 調用JavaMailSender類的send方法發送郵件
具體代碼實現如下:
@Autowired private JavaMailSender mailSender; public void sendHtmlEmail() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("YOUR_USERNAME@qq.com"); helper.setTo("recipient@example.com"); helper.setSubject("Subject"); helper.setText("<html><body><p>Html <b>body</b> with a <a href='http://www.example.com'>link</a>.</p></body></html>", true); mailSender.send(message); }
三、發送帶有附件的郵件
類似地,如果需要在郵件中添加附件,可以使用MimeMessageHelper類構造含有附件的MimeMessage。具體代碼實現如下:
@Autowired private JavaMailSender mailSender; public void sendEmailWithAttachment() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("YOUR_USERNAME@qq.com"); helper.setTo("recipient@example.com"); helper.setSubject("Subject"); helper.setText("Body"); ClassPathResource file = new ClassPathResource("attachment.pdf"); helper.addAttachment("attachment.pdf", file); mailSender.send(message); }
四、發送圖片郵件
如果需要在郵件中嵌入圖片,可以使用MimeMessageHelper類構造含有嵌入式圖片的MimeMessage。具體代碼實現如下:
@Autowired private JavaMailSender mailSender; public void sendEmailWithInlineImage() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("YOUR_USERNAME@qq.com"); helper.setTo("recipient@example.com"); helper.setSubject("Subject"); helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true); ClassPathResource image = new ClassPathResource("image.jpg"); helper.addInline("identifier1234", image); mailSender.send(message); }
五、使用Thymeleaf來發送HTML郵件
使用Thymeleaf模板引擎可以方便地構造HTML格式的郵件。首先,在pom.xml文件中添加Thymeleaf依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
然後,在郵件配置文件中增加如下配置:
spring.thymeleaf.check-template-location=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false
最後,在需要發送郵件的地方,使用Thymeleaf模板渲染郵件內容,具體實現如下:
@Autowired private JavaMailSender mailSender; @Autowired private TemplateEngine templateEngine; public void sendEmailWithThymeleafTemplate() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("YOUR_USERNAME@qq.com"); helper.setTo("recipient@example.com"); helper.setSubject("Subject"); Context context = new Context(); context.setVariable("title", "郵件標題"); context.setVariable("message", "郵件內容"); String htmlContent = templateEngine.process("email-template", context); helper.setText(htmlContent, true); mailSender.send(message); }
六、小結
在Springboot中,發送郵件有多種方式,可以根據不同的需求選擇不同的方法。在此,我們介紹了發送簡單文本郵件、發送HTML郵件、發送帶有附件的郵件、發送圖片郵件以及使用Thymeleaf來發送HTML郵件等不同方法。
原創文章,作者:BOYIA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/371400.html