當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
5、SpringBoot 发送邮件
生活随笔
收集整理的這篇文章主要介紹了
5、SpringBoot 发送邮件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1、引入依賴:
- 2、application.yml中配置郵件相關(guān)的參數(shù):
- 3、郵件service代碼:
- 4、單元測試代碼:
- 5、注意事項;
簡單幾步,實現(xiàn)在spring boot中發(fā)送郵件:
1、引入依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId> </dependency>2、application.yml中配置郵件相關(guān)的參數(shù):
spring:mail:host: smtp.qq.comusername: zhoumin@qq.compassword: zhouminproperties:mail:smtp:auth: truestarttls:enable: truerequired: true3、郵件service代碼:
@Service public class MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender sender; @Value("${spring.mail.username}") private String from; /*** 發(fā)送純文本的簡單郵件* @param to* @param subject* @param content*/ 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 { sender.send(message); logger.info("簡單郵件已經(jīng)發(fā)送。"); } catch (Exception e) { logger.error("發(fā)送簡單郵件時發(fā)生異常!", e); } } /*** 發(fā)送html格式的郵件* @param to* @param subject* @param content*/ public void sendHtmlMail(String to, String subject, String content){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要創(chuàng)建一個multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); sender.send(message); logger.info("html郵件已經(jīng)發(fā)送。"); } catch (MessagingException e) { logger.error("發(fā)送html郵件時發(fā)生異常!", e); } } /*** 發(fā)送帶附件的郵件* @param to* @param subject* @param content* @param filePath*/ public void sendAttachmentsMail(String to, String subject, String content, String filePath){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要創(chuàng)建一個multipart message 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)); helper.addAttachment(fileName, file); sender.send(message); logger.info("帶附件的郵件已經(jīng)發(fā)送。"); } catch (MessagingException e) { logger.error("發(fā)送帶附件的郵件時發(fā)生異常!", e); } } /*** 發(fā)送嵌入靜態(tài)資源(一般是圖片)的郵件* @param to* @param subject* @param content 郵件內(nèi)容,需要包括一個靜態(tài)資源的id,比如:<img src=\"cid:rscId01\" >* @param rscPath 靜態(tài)資源路徑和文件名* @param rscId 靜態(tài)資源id*/ public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){ MimeMessage message = sender.createMimeMessage(); try { //true表示需要創(chuàng)建一個multipart message 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); sender.send(message); logger.info("嵌入靜態(tài)資源的郵件已經(jīng)發(fā)送。"); } catch (MessagingException e) { logger.error("發(fā)送嵌入靜態(tài)資源的郵件時發(fā)生異常!", e); } } }4、單元測試代碼:
public class MailTests extends BasicUtClass{@Autowiredprivate MailService mailService;private String to = "xujijun@mail.cn";@Testpublic void sendSimpleMail() {mailService.sendSimpleMail(to, "主題:簡單郵件", "測試郵件內(nèi)容");}@AutowiredVelocityEngine velocityEngine;@Testpublic void sendHtmlMail() {Map<String, Object> model = new HashMap<String, Object>();model.put("time", XDateUtils.nowToString());model.put("message", "這是測試的內(nèi)容。。。");model.put("toUserName", "張三");model.put("fromUserName", "老許");String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "welcome.vm", "UTF-8", model);mailService.sendHtmlMail(to, "主題:html郵件", content);}@Testpublic void sendAttachmentsMail() {mailService.sendAttachmentsMail(to, "主題:帶附件的郵件", "有附件,請查收!", "C:\\Users\\Xu\\Desktop\\csdn\\1.png");}@Testpublic void sendInlineResourceMail() {String rscId = "rscId001";mailService.sendInlineResourceMail(to,"主題:嵌入靜態(tài)資源的郵件","<html><body>這是有嵌入靜態(tài)資源:<img src=\'cid:" + rscId + "\' ></body></html>","C:\\Users\\Xu\\Desktop\\csdn\\1.png",rscId);} }5、注意事項;
QQ郵箱需要開通相應(yīng)權(quán)限;
設(shè)置- 帳戶- POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務(wù)
總結(jié)
以上是生活随笔為你收集整理的5、SpringBoot 发送邮件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4、SpringBoot 配置和使用定时
- 下一篇: SpringCloud之微服务