springboot整合mail邮箱

sun公司提供了javaMail用来实现邮件发送,但是配置繁琐,spring中提供了javaMailSender用来简化邮件配置,springboot则提供了MailSendAutoConfiguration对邮件发送做了进一步的简化.

下面我们以QQ邮箱为例,进行发送操作

废话以后补充

1.登录QQ邮箱,进入设置

2.在账户选项卡中找到POP3/SMTP服务,点击开启按钮

springboot整合mail邮箱_第1张图片

3.(注意)下面我们获得一下授权码,因为项目需要发送邮箱的账户和密码,我们不能因为编写项目将自己账户的密码编写在项目中,所以这里的授权码就是代替密码的一个编码,

springboot整合mail邮箱_第2张图片

4.拿到授权码后,准备工作就完成了。

------------------------------------------------------------------

下面开始搭建环境

1.加入依赖

        
        
            org.springframework.boot
            spring-boot-starter-mail
        

2.配置application.yml

spring:
  mail:
    host: smtp.qq.com
    port: 465
    username: 发送邮箱
    password: 授权码
    default-encoding: UTF-8
    properties:
      mail:
        smtp:
          socketFactory:
            class: javax.net.ssl.SSLSocketFactory
        debug: true

springboot整合mail邮箱_第3张图片

3.发送一个最简单的邮件

@Service
public class MailService {

    @Resource
    JavaMailSender javaMailSender;

    /**
     * 最简单的邮件发送
     * @param from  发送方
     * @param to    接收方
     * @param cc    第三方(非必填)
     * @param subject   标题
     * @param content   内容
     */
    public void sendSimpleMail(String from,String to,String cc,String subject,String content){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setCc(cc);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        javaMailSender.send(simpleMailMessage);
    }
}

3.1测试一下(这里不管你们是Test测试也好,启动容器也好,小编就不写了)

   @Resource
   MailService mailService;

     public void sendSimpleMail(){
    mailService.sendSimpleMail("[email protected]","[email protected]","[email protected]","标题","Hello World");
    }

4.发送带附件的邮件


    /**
     * 代附件的邮件
     * @param from
     * @param to
     * @param subject
     * @param content
     * @param file
     */
    public void sendAttachFileMail(String from, String to, String subject, String content, File file) {
        try{
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content);
            mimeMessageHelper.addAttachment(file.getName(),file);
            javaMailSender.send(mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

4.1测试

  public void sendAttachFileMail(){
        mailService.sendAttachFileMail("[email protected]","[email protected]","标题","lll",new File("D://anz-t//U1902.exe"));
    }

5.带图片的邮件

    /**
     * 带图片的邮件
     * @param from
     * @param to
     * @param subject
     * @param content
     * @param srePAth
     * @param resIds
     */
    public void sendMailWithImg(String from,String to,String subject,String content,String [] srePAth,String[]resIds){
        if (srePAth.length!=resIds.length){
            System.out.println("发送失败");
            return;
        }
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(content,true);
            for (int i = 0; i < srePAth.length; i++) {
                FileSystemResource res = new FileSystemResource(new File(srePAth[i]));
                mimeMessageHelper.addInline(resIds[i],res);
            }
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

5.1测试

public void sendMailWithImg(){
        mailService.sendMailWithImg("[email protected]","[email protected]","标题","图片1:
图片2:
",new String[]{"C:\\Users\\cyc\\Pictures\\Fedora.png","C:\\Users\\cyc\\Pictures\\windows7.jpg"},new String[]{"po1","po2"}); }

也可以是FreeMarker或者thymeleaf模板,下面使用thymeleaf模板编写邮件发送功能

1.加依赖

     
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

2.服务

   /**
     * 使用thymeleaf模板
     * @param from
     * @param to
     * @param subject
     * @param context
     */
    public void sendHtmlMail(String from,String to ,String subject,String context){
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setText(context);
            mimeMessageHelper.setSubject(subject);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

3.在resources/templates目录下创建mailtemplate.html文件,代码如下:




    
    邮件



邮件激活
核对无误请点击本链接激活邮箱

4.测试

   @Autowired
    TemplateEngine templateEngine;

    public void sendHtmlMailThymeleaf(){
        Context context = new Context();
        context.setVariable("username","cyc");
        String mail = templateEngine.process("mailtemplate.html",context);
        mailService.sendHtmlMail("[email protected]","[email protected]","标题",mail);
    }

下面是我邮箱收到的邮件,内容不一样不要见怪

springboot整合mail邮箱_第4张图片

 

你可能感兴趣的:(spring,boot)