真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

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

網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站設(shè)計(jì),高端網(wǎng)頁制作,對三輪攪拌車等多個行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)成都網(wǎng)站推廣優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。

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

 @Override
 public void sendHtmlMail(String to, String subject, String content) {
 MimeMessage message = mailSender.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);
  mailSender.send(message);
  log.info("html郵件發(fā)送成功");
 } catch (MessagingException e) {
  log.error("發(fā)送html郵件時發(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ā)送帶附件的郵件時發(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)資源的郵件時發(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="\n" +
  "\n" +
  " 

hello world ! 這是一封Html郵件!

\n" + "\n" + ""; 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="這是有圖片的郵件:"; 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é)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。


網(wǎng)頁名稱:SpringBoot集成E-mail發(fā)送各種類型郵件的方法
網(wǎng)頁URL:http://weahome.cn/article/gdjesg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部