自己的授权码自己记好
引入依赖
<dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.6.2</version>
</dependency>
<dependency><groupId>javax.mail</groupId><artifactId>javax.mail-api</artifactId><version>1.4.7</version>
</dependency>
controller层代码
package com.cao.controller;import com.cao.pojo.Mail;
import com.cao.utils.EmailUtils;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api/email")
@Slf4j
public class SendEmailController {@PostMapping("/send") // http://localhost:8080/api/email/sendpublic String sendEmail(@RequestBody Mail mail) {Gson gson = new Gson();log.info("输入的mail对象是: {}", gson.toJson(mail));try {EmailUtils.createMimeMessage(mail.getSendEmail(),mail.getReceiveEmail(),mail.getReceiveName(),mail.getSubject(),mail.getSendName(),mail.getContent());return "发送完成";} catch (Exception e) {log.error("发送出现了异常", e);throw new RuntimeException(e);}}
}
Service层代码
package com.cao.utils;import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;public class EmailUtils {/*** 发件⼈邮箱密码 - 登录邮件开启 SMTP 服务后,邮件服务商⽣成的“授权码”*/public static final String authorizeCode = "oeqdschg1122lhgecbe12"; //自己的授权码替换下/*** 发件⼈SMTP服务器地址,⼀般的格式为:smtp.xxx.com,其中xxx为邮件服务商名称*/public static final String smtpHost = "smtp.qq.com";/*** 协议*/public static final String protocol = "smtp";public static Session getEmailSession() {// 参数配置,⽤于连接邮件服务器Properties props = new Properties();// 使⽤协议props.setProperty("mail.transport.protocol", protocol);// 发件⼈邮箱的 SMTP 服务器地址props.setProperty("mail.smtp.host", smtpHost);// 需要请求认证props.setProperty("mail.smtp.auth", "true");// 创建会话对象,⽤于与邮箱服务器交互Session session = Session.getInstance(props);// 设置为debug模式,在控制台中可以查看详细的发送⽇志session.setDebug(true);return session;}/*** 发送邮件的工具类*/public static MimeMessage createMimeMessage(String sendEmail, String receiveEmail, String receiveName, String subject, String sendName, String content) throws Exception {Session session = getEmailSession();// 1.创建邮件对象MimeMessage message = new MimeMessage(session);// 2.设置发件⼈,其中 InternetAddress 有三个参数,分别为:邮箱,显示的昵称,昵称的字符集编码message.setFrom(new InternetAddress(sendEmail, sendName, "UTF-8"));// 3.设置收件⼈ - MimeMessage.RecipientType.TOmessage.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveEmail, receiveName, "UTF-8"));// 4.设置邮件主题message.setSubject(subject, "UTF-8");// 5.设置邮件正⽂内容,指定格式为HTML格式message.setContent(content, "text/html;charset=UTF-8");// 6.设置显示发件时间message.setSentDate(new Date());// 7.保存设置message.saveChanges();// 8.根据 Session 获取邮件传输对象Transport transport = session.getTransport();// 9.连接邮件服务器transport.connect(sendEmail, authorizeCode);// 10.发送邮件transport.sendMessage(message, message.getAllRecipients());// 11.关闭连接transport.close();return message;}
}
测试: 调用controller
查看是否有邮件收到