2021-07-28 10:08

JavaMail发送邮件之四:Springboot整合JavaMail发送邮件

王姐姐

JavaEE

(2089)

(0)

收藏

1.1. 添加依赖

<!--JavaMail-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

1.2. application.properties配置

#配置smpt服务器地址
spring.mail.host=smtp.qq.com
#发送者邮箱
spring.mail.username=2646541192@qq.com
#配置密码,注意不是真正的密码,而是申请的授权码
spring.mail.password=vltaykcfeqpieajb
#qq邮箱的为 smtp.qq.com 端口 465或者587
#spring.mail.port=465
#默认的邮件编码为UTF-8
#spring.mail.default-encoding=UTF-8
#设置smtp邮件服务器tls加密认证
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
#开启ssl 否则会报503错误
spring.mail.properties.mail.smtp.ssl.enable=true

 126邮箱SMTP服务器地址:smtp.126.com,端口号:465或者994

2163邮箱SMTP服务器地址:smtp.163.com,端口号:465或者994
yeah邮箱SMTP服务器地址:smtp.yeah.net,端口号:465或者994
qq邮箱SMTP服务器地址:smtp.qq.com,端口号465或587*

1.3. 发送邮件类

@Component
public class MailUtil {
    @Autowired
    private JavaMailSender mailSender;
//发送邮件的邮箱
    @Value("${spring.mail.username}")
    private String from;
    //发送普通邮件
    public void sendMail(String to,String subject,String text)  {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setSubject(subject);
            helper.setFrom(from);
            helper.setTo(to);
            //抄送
            //helper.setCc("");
            //密送
            //helper.setBcc("");
            helper.setText(text,true);
            //发送
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

测试:

@Test
public void testSendMail(){
    String text = "<html><body><h2>最新通知</h2><p>大家尽快准备面试服装和面试题!</p></body></html>";
    mailUtil.sendMail("wangrf@qingsoft.net","hello",text);
}

 图片1.png

1.4. 发送带附件的邮件

public void sendMail(String to,String subject,String text,String attachmentFileName,String attachementFilePath)  {
    try {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject(subject);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setText(text,true);
        //设置附件
        if(attachmentFileName!=null){
            helper.addAttachment(attachmentFileName,new FileSystemResource(attachementFilePath));
        }
        //发送
        mailSender.send(mimeMessage);
    } catch (MessagingException e) {
        e.printStackTrace();
    }

}

测试:

@Test
public void testSendMail(){
    String text = "<html><body><h2>最新通知</h2><p>大家尽快准备面试服装和面试题!</p>" +
            "<img src=\"cid:inlineImg\">"+
            "</body></html>";
    mailUtil.sendMail("wangrf@qingsoft.net","hello",text,"操作日志.txt","d:\\log.txt");
}

收件箱收到的邮件如下:

 图片2.png

1.5. 发送带内联资源的邮件

public void sendMail(String to,String subject,String text,String attachmentFileName,String attachementFilePath,String inlineImgPath)  {
    try {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject(subject);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setText(text,true);
        //设置附件
        if(attachmentFileName!=null){
            helper.addAttachment(attachmentFileName,new FileSystemResource(attachementFilePath));
        }
        //设置内联资源
        if(inlineImgPath!=null){

            helper.addInline("inlineImg",new FileSystemResource(inlineImgPath));
        }
        //发送
        mailSender.send(mimeMessage);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

测试:

@Test
public void testSendMail(){
    String text = "<html><body><h2>最新通知</h2><p>大家尽快准备面试服装和面试题!</p>" +
            "<img src=\"cid:inlineImg\">"+
            "</body></html>";
    mailUtil.sendMail("wangrf@qingsoft.net","hello",text,"操作日志.txt","d:\\log.txt","d:\\images\\2.jpg");
}

收件箱收到的邮件如下:

 图片3.png


图小白107444001 (6).png

0条评论

点击登录参与评论