通过java实现发送邮件功能
引入依赖
<!--发送邮件相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
controller层
package com.tools.controller;
import com.tools.services.SendEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/email")
public class SendEmailController {
@Autowired SendEmailService sendEmailService;
@GetMapping(value = "/sendMail")
public void sendMail(@RequestParam(value = "title") String title,
@RequestParam(value = "context") String context) {
sendEmailService.sendMail(title, context);
}
}
services层
package com.tools.services;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;
@Service
@Slf4j
public class SendEmailService {
// 发件人邮箱地址
private static String from = "xxx@qq.com";
// 发件人称号,同邮箱地址
private static String user = "xxx@qq.com";
// 发件人邮箱客户端授权码
private static String password = "在邮箱设置中获取到的授权码";
//发件人的邮箱服务器
private static String mailHost = "smtp.qq.com";
//接收地址
private static String receivers = "xxx@163.com";
/**
* 使用加密的方式,利用465端口进行传输邮件,开启ssl
* @param text
* @param title
*/
/* 发送验证信息的邮件 */
public static boolean sendMail( String title,String text) {
// String to = "fushimin98@163.com";
Properties props = new Properties();
// 设置发送邮件的邮件服务器的属性(这里使用qq的smtp服务器)
props.put("mail.smtp.host", mailHost);
// 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
props.put("mail.smtp.auth", "true");
// 用刚刚设置好的props对象构建一个session
Session session = Session.getDefaultInstance(props);
// 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使用(你可以在控制台(console)上看到发送邮件的过程)
// session.setDebug(true);
// 用session为参数定义消息对象 通过会话,得到一个邮件,用于发送
MimeMessage message = new MimeMessage(session);
try {
// 加载发件人地址
message.setFrom(new InternetAddress(from));
// 加载收件人地址 ,to为收件人,cc为抄送,bcc为密送
message.addRecipient(Message.RecipientType.TO, new InternetAddress(receivers));
// message.addRecipient(Message.RecipientType.CC, new InternetAddress("抄送邮箱"));
// message.addRecipient(Message.RecipientType.BCC, new InternetAddress("密送邮箱"));
// 加载标题
message.setSubject(title);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent("本机IP:"+ getLocalIP() +","+text, "text/html;charset=utf-8");
multipart.addBodyPart(contentPart);
message.setContent(multipart);
message.saveChanges(); // 保存变化
// 连接服务器的邮箱
Transport transport = session.getTransport("smtp");
// 把邮件发送出去
transport.connect(mailHost, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
log.debug("邮件发送成功");
} catch (MessagingException e) {
log.debug("邮件发送出现异常");
e.printStackTrace();
return false;
}
return true;
}
/**
* 获取本机IP
*
* @return
*/
private static String getLocalIP() {
InetAddress address = null;
String ip = null;
try {
address = InetAddress.getLocalHost();
ip = address.getHostAddress();
} catch (UnknownHostException e) {
log.debug("----获取本机IP出错----");
}
return ip; //返回IP地址
}
}
访问地址:
localhost:8080/email/sendMail?title=测试标题&context=测试内容
获取邮箱授权码方法
1、点击设置:在邮箱的网页版中,点击左上角的“设置”。
2、在设置界面点击账户
3、开启POP3/SMTP服务 生成授权码
在开启服务中 开启POP3/SMTP服务
点击生成授权码