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博文

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以通过JavaMailSender发送HTML邮件。以下是发送HTML邮件的步骤: 1. 在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 在application.properties文件中添加SMTP服务器的配置信息: ``` spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=your-email@gmail.com spring.mail.password=your-email-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建一个邮件服务类,该类使用JavaMailSender发送HTML邮件: ``` @Service public class EmailService { @Autowired private JavaMailSender javaMailSender; public void sendHtmlEmail(String to, String subject, String htmlBody) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo(to); helper.setSubject(subject); helper.setText(htmlBody, true); javaMailSender.send(message); } } ``` 4. 在需要发送HTML邮件的地方调用邮件服务类的sendHtmlEmail方法: ``` @Autowired private EmailService emailService; public void sendEmail() throws MessagingException { String to = "recipient-email@example.com"; String subject = "Test HTML Email"; String htmlBody = "<h1>Hello World!</h1><p>This is a test HTML email.</p>"; emailService.sendHtmlEmail(to, subject, htmlBody); } ``` 以上就是使用Spring Boot发送HTML邮件的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值