溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot集成E-mail發(fā)送各種類型郵件的方法

發(fā)布時(shí)間:2020-06-23 14:56:19 來源:億速云 閱讀:180 作者:清晨 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)SpringBoot集成E-mail發(fā)送各種類型郵件的方法,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

JDK本身有自帶發(fā)送郵件api,加上SpringBoot在進(jìn)行封裝,使得現(xiàn)在使用起來十分快速簡潔。
話不多說,參考純潔的微笑博客,更改jar版本為2.0.4 開干,基本沒什么坑。
就是配置郵箱賬號密碼是,如果是qq郵箱,需要開啟PO30和STMP服務(wù),并且獲取臨時(shí)授權(quán)碼。

開啟服務(wù)鏈接:

https://mail.qq.com/cgi-bin/frame_html?sid=a5ZSbreeNm9pHyl1&r=a83225170e94773c650a460c10f7a05c 

SpringBoot集成E-mail發(fā)送各種類型郵件的方法

與Springboot集成

導(dǎo)入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'

配置郵箱

#郵箱服務(wù)器地址,各大運(yùn)營商不同
spring.mail.host=smtp.qq.com
#用戶名
spring.mail.username=9118542413@qq.com
#密碼,如果是qq的,要申請臨時(shí)授權(quán)碼
spring.mail.password=faw124awfawfawg
spring.mail.default-encoding=UTF-8
#以誰來發(fā)送郵件
mail.fromMail.addr=9118542413@qq.com

發(fā)送各種類型的郵件

@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("簡單郵件已經(jīng)發(fā)送。");
 } catch (Exception e) {
  log.error("發(fā)送簡單郵件時(shí)發(fā)生異常!", e);
 }
 }

 @Override
 public void sendHtmlMail(String to, String subject, String content) {
 MimeMessage message = mailSender.createMimeMessage();

 try {
  //true表示需要?jiǎng)?chuàng)建一個(gè)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郵件發(fā)送成功");
 } catch (MessagingException e) {
  log.error("發(fā)送html郵件時(shí)發(fā)生異常!", 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("帶附件的郵件已經(jīng)發(fā)送。");
 } catch (MessagingException e) {
  log.error("發(fā)送帶附件的郵件時(shí)發(fā)生異常!", 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("嵌入靜態(tài)資源的郵件已經(jīng)發(fā)送。");
 } catch (MessagingException e) {
  log.error("發(fā)送嵌入靜態(tài)資源的郵件時(shí)發(fā)生異常!", 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() {
 //創(chuàng)建郵件正文
 Context context = new Context();
 context.setVariable("id", "006");
 mailService.sendTemplateMail("15017263512@163.com","主題:這是模板郵件",
  "emailTemplate",context);
 }
}

關(guān)于SpringBoot集成E-mail發(fā)送各種類型郵件的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI