当前位置:   article > 正文

Springboot整合JavaMail(发送邮件)_spring boot javamailsender yml配置

spring boot javamailsender yml配置

一、邮件的基础概念

  • SMTP(Simple Mail Transfer Protocol):简单邮件传输协议,用于发送电子邮件的传输协议
  • POP3(Post Office Protocol-Version 3):用于接收电子邮件的标准协议
  • IMAP(Internet Mail Access Protocol):互联网消息协议,是POP3的替代协议。

二、springboot整合JavaMail步骤

2.1 maven管理坐标,pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

2.2 yml文件配置

  1. spring:
  2. mail:
  3. host: smtp.qq.com #协议,163邮箱就是smtp.163.com
  4. username: 123456@qq.com #发送端的邮箱地址
  5. password: gmsgycitwulgdifd #发送端的邮箱的smtp的密码
  6. port: 465 #腾讯qq的邮箱端口
  7. default-encoding: utf-8
  8. protocol: smtps #与端口绑定的协议,必须设置,而不是采用默认的smtp。要不然抛异常

2.3 调用客户端

  1. @Autowired
  2. private JavaMailSender javaMailSender; //发送邮件的类
  3. @Override
  4. public void sendMail(Mail mail) {
  5. SimpleMailMessage mailMessage = new SimpleMailMessage(); //邮件的类
  6. mailMessage.setFrom(mail.getFrom()); //发送源的邮箱地址 "123456@qq.com"
  7. mailMessage.setTo(mail.getTo()); //目的邮箱的地址 "1234567@163.com"
  8. mailMessage.setSubject(mail.getSubject()); //邮件的标题
  9. mailMessage.setText(mail.getText()); //邮件内容
  10. javaMailSender.send(mailMessage); //发送邮件
  11. }

三、多部件邮件

3.1 发送html格式的内容

可以是超链接,图片等

  1. @Autowired
  2. private JavaMailSender javaMailSender;
  3. @Override
  4. public void sendMail(Mail mail) {
  5. try {
  6. MimeMessage message = javaMailSender.createMimeMessage();
  7. MimeMessageHelper helper = new MimeMessageHelper(message);
  8. helper.setFrom(mail.getFrom());
  9. List<String> to = mail.getTo();
  10. helper.setTo(to.toArray(new String[to.size()])); //发送给多个邮箱
  11. helper.setSubject(mail.getSubject());
  12. //"<img src='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.lemeitu.com%2Fm00%2F19%2F17%2Ff9bbd26a791748cb501443a8527a8f44__w.jpg&refer=http%3A%2F%2Fimg.lemeitu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1670850728&t=33c72749872d1b0d60e1bab456fcfa29'/><a href='https://www.baidu.com/'>点开有惊喜</a>"
  13. helper.setText(mail.getText(),true);//开启html,内容可以是超链接、图片等
  14. javaMailSender.send(message);
  15. }catch (Exception e){
  16. e.printStackTrace();
  17. }
  18. }

3.2 发送附件

  1. public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart) throws MessagingException {
  2. this(mimeMessage, multipart, (String)null);
  3. }
  1. @Autowired
  2. private JavaMailSender javaMailSender;
  3. @Override
  4. public void sendMail(Mail mail) {
  5. try {
  6. MimeMessage message = javaMailSender.createMimeMessage();
  7. MimeMessageHelper helper = new MimeMessageHelper(message,true); //开启附件传输
  8. helper.setFrom(mail.getFrom());
  9. List<String> to = mail.getTo();
  10. helper.setTo(to.toArray(new String[to.size()])); //发送给多个邮箱
  11. helper.setSubject(mail.getSubject());
  12. helper.setText(mail.getText(),true);
  13. //添加附件
  14. File f1 = new File(mail.getFileName());
  15. helper.addAttachment(f1.getName(),f1);//文件名,文件句柄
  16. javaMailSender.send(message);
  17. }catch (Exception e){
  18. e.printStackTrace();
  19. }
  20. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/422118
推荐阅读
相关标签
  

闽ICP备14008679号