java发送邮件

名词解释

协议

  1. SMTP:Simple Mail Transfer Protocol,简单邮件传输协议,用于发送邮件的协议
  2. POP3:Post Office Protocol 3,邮局通讯协议第三版,用于接收邮件的标准协议
  3. IMAP:Internet Message Access protocol,互联网消息访问协议,是POP3的替代

申请授权码

什么是授权码
授权码是QQ邮箱推出的,用于登录第三方客户端的专用密码
适用于登录以下服务:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务。
不同邮件服务提供商的授权码的有效期不同,如163邮箱的是180天,而QQ的有效只要不修改密码就有效

拿163邮箱为例申请授权码
登录163官网
在这里插入图片描述
如果没有开启smtp服务器,需要先开启下
在这里插入图片描述
在这里插入图片描述
扫码后会弹出授权码,并且提示只有180天有效期,此授权码配置在spring.mail.password上即可

需要的依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-mail</artifactId>
 </dependency>

发送邮件准备

配置文件

spring.mail.username=   # 发送邮箱
spring.mail.password=   # 授权码
spring.mail.host=smtp.qq.com  # 发送邮箱的smtp服务
# STARTTLS 是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL),而不是另外使用一个端口作加密通信。
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

测试代码

package testmail;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@SpringBootTest
public class TestMailApplicationTests {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;
	/**
     * 发送简单的文本邮件
     */
    @Test
    public void contextLoads() {
        // 收件人的电子邮件 ID
        String to = "收件邮箱地址";

        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setSubject("邮件标题111");
            message.setText("邮件内容11");
            message.setTo(to);
            message.setFrom(from);
            javaMailSender.send(message);
            System.out.println("邮件发送成功!");
        } catch (Exception mex) {
            mex.printStackTrace();
        }
    }

/**
     * 发送带图片和附件的邮件
     */
    @Test
    public void sendMultipartMail() {
        // 收件人的电子邮件 ID
        String to = "收件邮箱地址";
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 第二个参数用于指定邮件内容是否应该被处理为多部分(multipart)消息。
            // 多部分消息通常用于包含附件或同时包含文本和HTML内容的邮件。
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("带附件的邮件标题1");
            // 设置邮件内容,并指定是否包含HTML格式的邮件内容。如果是图片则必须是网络可以访问的
            helper.setText("带附件的邮件内容1" + "<br/><img src=\"https://preview.qiantucdn.com/58pic/70/95/82/52Z58PICCK2DBmYahyMK58PIC_origin_PIC2018.jpg!w1024_new_small_1\">", true);
            helper.setTo(to);
            helper.setFrom(from);
            // 设置附件
            helper.addAttachment("附件1.jpg", new File("E:\\workspace\\images/2.jpg"));
            javaMailSender.send(mimeMessage);
            System.out.println("邮件发送成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

遇到的问题

  1. 使用QQ邮箱发送邮件时报Authentication failed、和AuthenticationFailedException: 530 Login fail. A secure connection is requiered(such as ssl). More information at。。。等信息,经查询需要配置上spring.mail.properties.mail.smtp.starttls.enable=true、spring.mail.properties.mail.smtp.starttls.required=true两个配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值