spring boot 发送邮箱
Posted 蜗牛的坚持
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot 发送邮箱相关的知识,希望对你有一定的参考价值。
1 sendEmail.properties
#邮件发送协议 PROTOCOL = smtp #发送邮件的服务器的IP和端口 HOST = smtp.exmail.qq.com PORT = 465 #是否要求身份认证 IS_AUTH = true #是否启用调试模式(启用调试模式可打印客户端与服务器交互过程时一问一答的响应消息) IS_ENABLED_DEBUG = true #发送人的邮箱地址 FROM_EMAIL_ADRESS = system@oilchem.net #登陆邮件发送服务器的用户名和密码 USERNAME = 888@com PASSWORD = *****
2.MyAuthenticator
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class MyAuthenticator extends Authenticator { String userName = null; String password = null; public MyAuthenticator() { } public MyAuthenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }
3 sendEmailUtil
import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mysteel.utils.ResourcesUtil; public class sendEmailUtil { // 邮件发送协议 private String protocol; // SMTP邮件服务器 private String host; // SMTP邮件服务器默认端口 private String port; // 是否要求身份认证 private String is_auth; // 是否启用调试模式(启用调试模式可打印客户端与服务器交互过程时一问一答的响应消息) private String is_enabled_debug; // 登陆邮件发送服务器的用户名和密码 private String userName; private String password; //发送人的邮箱地址 private String fromEmail; // 初始化连接邮件服务器的会话信息 private Session session; private Logger logger = LoggerFactory.getLogger(sendEmailUtil.class); public sendEmailUtil() { // 获取配置文件信息 Properties pro = ResourcesUtil.loadProperties("sendEmail.properties"); this.protocol = pro.getProperty("PROTOCOL"); this.host = pro.getProperty("HOST"); this.port = pro.getProperty("PORT"); this.is_auth = pro.getProperty("IS_AUTH"); this.is_enabled_debug = pro.getProperty("IS_ENABLED_DEBUG"); this.userName = pro.getProperty("USERNAME"); this.password = pro.getProperty("PASSWORD"); this.fromEmail = pro.getProperty("FROM_EMAIL_ADRESS"); this.init(); } private void init() { Properties props = new Properties(); props.setProperty("mail.transport.protocol", this.protocol); props.setProperty("mail.smtp.host", this.host); props.setProperty("mail.smtp.port", this.port); props.setProperty("mail.smtp.auth", this.is_auth); props.setProperty("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用 props.setProperty("mail.debug",this.is_enabled_debug); MyAuthenticator myAuthenticator = null; if ("true".equals(this.is_auth)) { myAuthenticator = new MyAuthenticator(this.userName, this.password); } this.session = Session.getDefaultInstance(props, myAuthenticator); } public boolean sendTextMail(String toEmail, String subject, String text) { try { // 创建邮件发送者地址 Address from = new InternetAddress(this.fromEmail);//TODO // 创建邮件的接收者地址并设置到邮件消息中 Address to = new InternetAddress(toEmail); // 根据session创建一个邮件消息 Message mailMessage = new MimeMessage(this.session); // 设置邮件消息的发送者 mailMessage.setFrom(from); mailMessage.setRecipient(Message.RecipientType.TO, to); // 设置邮件消息的主题 mailMessage.setSubject(subject); // 设置邮件消息发送的时间 mailMessage.setSentDate(new Date()); // 设置邮件消息的主要内容 mailMessage.setText(text); // 发送邮件 Transport.send(mailMessage); return true; } catch (Exception e) { logger.error(e.getMessage(),e); return false; } } /*public static void main(String[] args) { try { sendEmailUtil send = new sendEmailUtil(); Boolean flag = send.sendTextMail("xxx@qq.com", "测试发送邮件标题", "测试发送邮件内容"); System.out.println("发送完成==================="+flag); } catch (Exception e) { e.printStackTrace(); } }*/ }
4.用法
sendEmailUtil send = new sendEmailUtil(); Boolean flag = send.sendTextMail(registerInfoVO.getMainContactEmail(), temp.getContent(), message);
以上是关于spring boot 发送邮箱的主要内容,如果未能解决你的问题,请参考以下文章
Java 之 Spring Boot 发送邮箱(解决依赖找不到异常)