springboot发送email邮件

添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>

配置:去邮箱中开启SMTP服务

注意密码是邮箱的生成授权码

代码:

 1 package com.drawnblue.springbootemail;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.core.io.FileSystemResource;
 6 import org.springframework.mail.SimpleMailMessage;
 7 import org.springframework.mail.javamail.JavaMailSender;
 8 import org.springframework.mail.javamail.MimeMessageHelper;
 9 import org.springframework.stereotype.Component;
10 
11 import javax.mail.MessagingException;
12 import javax.mail.internet.MimeMessage;
13 import java.io.File;
14 import java.util.Date;
15 
16 @Component
17 public class EmailUtil {
18     private String from="1248375279@qq.com";
19     @Autowired
20     private JavaMailSender sender;
21 
22     /**
23      * 发送一般文本邮件
24      * @param to
25      * @param subject
26      * @param content
27      */
28     public void sendTextEmail(String to,String subject,String content){
29         SimpleMailMessage message = new SimpleMailMessage();
30         message.setFrom(from);
31         message.setTo(to);
32         message.setSubject(subject);
33         message.setText(content);
34         message.setSentDate(new Date());
35         sender.send(message);
36     }
37 
38     /**
39      * @param to
40      * @param subject
41      * @param content
42      * @param imgPath
43      * @param imgId
44      * @throws MessagingException
45      * 发送带图片并显示在邮件中的邮件
46      */
47     public void sendImageMail(String to, String subject, String content, String imgPath, String imgId) throws MessagingException {
48         //创建message
49         MimeMessage message = sender.createMimeMessage();
50         MimeMessageHelper helper = new MimeMessageHelper(message, true);
51         //发件人
52         helper.setFrom(from);
53         //收件人
54         helper.setTo(to);
55         //标题
56         helper.setSubject(subject);
57         //true指的是html邮件,false指的是普通文本
58         helper.setText(content, true);
59         //添加图片
60         FileSystemResource file = new FileSystemResource(new File(imgPath));
61         helper.addInline(imgId, file);
62         //发送邮件
63         sender.send(message);
64     }
65 
66 }

测试

package com.drawnblue.springbootemail;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootEmailApplicationTests {
@Autowired
EmailUtil text;

    /**
     * 文本
     */
    @Test
    public void contextLoads() {
        text.sendTextEmail("yourEmailAddr","test","helloworld!!!");
    }

    /**
     * @throws MessagingException
     * 发送带图片的邮件
     */
    @Test
    public void sendImageEmailTest() throws MessagingException {
        text.sendImageMail("yourEmailAddr","image测试","<h1 style='color:red'>helloWorld</h1><img src='cid:0011'/>","G:\壁纸\timg.jpg","001");
    }
}

效果

 要了解其他的也可以参考https://www.cnblogs.com/mxwbq/p/10625612.html博文

原文地址:https://www.cnblogs.com/xiaoyao-001/p/11363451.html