Springboot发送邮件(集成JavaMailSender MimeMessage)

1.在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
2.以QQ邮箱为例,在application.yml中加入如下配置(注意替换自己的用户名和密码):
spring:
  #邮件配置
  mail:
    # 设置邮箱主机
    host: smtp.qq.com
    # 开启邮箱POP3/SMTP服务,获取客户端授权码(注意并不是邮箱密码,而是授权码)
    password: uriarqfegqchgcge
    # 邮箱的用户名
    username: 6666@qq.com
    properties:
      mail:
        smtp:
          # 设置是否需要认证,如果为true,那么用户名和密码就必须的。如果设置false,可以不设置用户名和密码,当然也得看你的对接的平台是否支持无密码进行访问的。
          auth: true
          starttls:
            # STARTTLS[1]  是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL),而不是另外使用一个端口作加密通信。
            enable: true
            require: true
3.下面是发送邮件的单元测试用例:
import lombok.extern.slf4j.Slf4j;
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.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class SendExMailTest {

    @Autowired
    private JavaMailSender mailSender;

    @Test
    public void testSend() {
        MimeMessage message = mailSender.createMimeMessage();
        // true代表是multipart类型
        log.info("开始发送邮件");
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            //发送者
            helper.setFrom("6666@qq.com");
            //接收者
            helper.setTo("5555@qq.com");
            //邮件主题
            helper.setSubject("test报告");
            //邮件内容
            helper.setText("test", true);

            //带有附件
            FileSystemResource file1 = new FileSystemResource(new File("C:/Users/Administrator/Desktop/eee.png"));
            helper.addAttachment("ees.png",file1);
            FileSystemResource file2 = new FileSystemResource(new File("C:/Users/Administrator/Desktop/test.jpg"));
            helper.addAttachment("test.jpg",file2);

            //嵌入图片
            //邮件内容,第二个参数指定发送的是HTML格式
            //说明:嵌入图片<img src='cid:head'/>,其中cid:是固定的写法,而aaa是一个contentId。
            helper.setText("<body>这是图片:<img src='cid:aaa' /></body>", true);
            FileSystemResource file3 = new FileSystemResource(new File("C:/Users/Administrator/Desktop/test.jpg"));
            helper.addInline("aaa",file3);

            mailSender.send(message);
            log.info("邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
}

参考链接:https://412887952-qq-com.iteye.com/blog/2305992

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用SpringBoot发送邮件的示例代码,其中包括了如何发送带附件的邮件和如何开启TLS验证: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootApplication @EnableConfigurationProperties(MailProperties.class) public class MailApplication { @Autowired private JavaMailSender mailSender; @Autowired private MailProperties mailProperties; public static void main(String[] args) { SpringApplication.run(MailApplication.class, args); } public void sendMailWithAttachment() throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(mailProperties.getUsername()); helper.setTo("recipient@example.com"); helper.setSubject("Test email with attachment"); // 添加附件 FileSystemResource file = new FileSystemResource(new File("attachment.txt")); helper.addAttachment("attachment.txt", file); // 发送邮件 mailSender.send(message); } } ``` 在application.properties文件中添加以下配置: ``` spring.mail.username=xxxxxxx@outlook.com spring.mail.password=xxxxxxxxx spring.mail.port=587 spring.mail.host=smtp-mail.outlook.com spring.mail.properties.mail.smtp.starttls.required=true ``` 注意:在使用Outlook发送邮件时,需要开启TLS验证,否则会显示匿名用户无法通过验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值