那么,废话少说,直接上代码。
1. 目录结构
重点是那几个带mail的,其他文件不用管。
2. pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxjz</groupId>
<artifactId>HelloSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
</project>
其他文件都是基础的,主要是引入这个mail。 我这个版本比较老旧,更新一下也是可以的。
3. 主要代码
先搞一个邮件实体类
package com.xxjz.sendemail;
import java.io.Serializable;
public class Mail implements Serializable {
private static final long serialVersionUID = -543077416549260588L;
private String sendTo;
private String subject;
private String text;
public String getSendTo() {
return sendTo;
}
public void setSendTo(String sendTo) {
this.sendTo = sendTo;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Mail() {
super();
}
}
再搞一个邮箱实体类
package com.xxjz.sendemail;
import java.io.Serializable;
public class MailBox implements Serializable {
private static final long serialVersionUID = -543077416549260588L;
private String mailSmtpAuth; // SMTP?????????
private String mailSmtpHost; //SMTP??????
private String mailSmtpPort; //?????QQ?????????? ????
private String mailUser; //???
private String mailPassword; //????
public String getMailSmtpAuth() {
return mailSmtpAuth;
}
public void setMailSmtpAuth(String mailSmtpAuth) {
this.mailSmtpAuth = mailSmtpAuth;
}
public String getMailSmtpHost() {
return mailSmtpHost;
}
public void setMailSmtpHost(String mailSmtpHost) {
this.mailSmtpHost = mailSmtpHost;
}
public String getMailSmtpPort() {
return mailSmtpPort;
}
public void setMailSmtpPort(String mailSmtpPort) {
this.mailSmtpPort = mailSmtpPort;
}
public String getMailUser() {
return mailUser;
}
public void setMailUser(String mailUser) {
this.mailUser = mailUser;
}
public String getMailPassword() {
return mailPassword;
}
public void setMailPassword(String mailPassword) {
this.mailPassword = mailPassword;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
最后再搞一个发送邮件的工具类
package com.xxjz.sendemail;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendMail {
private static volatile SendMail singleton = null;
private MailBox mailBox = new MailBox();
private Mail mail = new Mail();
private SendMail() {
}
public static SendMail getSingleton() {
if (singleton == null) {
synchronized (SendMail.class) {
if (singleton == null) {
singleton = new SendMail();
}
}
}
return singleton;
}
public void setProperties(MailBox b) {
this.mailBox = b;
}
public void setMail(Mail b) {
this.mail = b;
}
public void sendQQSmtpEmail() throws Exception {
// 创建Properties 类用于记录邮箱的一些属性
final Properties props = new Properties();
// 表示SMTP发送邮件,必须进行身份验证
props.put("mail.smtp.auth", mailBox.getMailSmtpAuth());
// 此处填写SMTP服务器
props.put("mail.smtp.host", mailBox.getMailSmtpHost());
// 端口号,QQ邮箱给出了两个端口
props.put("mail.smtp.port", mailBox.getMailSmtpPort());
// 此处填写你的账号
props.put("mail.user", mailBox.getMailUser());
// 此处的密码就是前面说的16位STMP口令
props.put("mail.password", mailBox.getMailPassword());
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人的邮箱
InternetAddress to = new InternetAddress(mail.getSendTo());
message.setRecipient(RecipientType.TO, to);
// 设置邮件标题
message.setSubject(mail.getSubject());
// 设置邮件的内容体
message.setContent(mail.getText(), "text/html;charset=UTF-8");
// 最后当然就是发送邮件啦
Transport.send(message);
}
}
最后,让我们写一个测试方法来进行测试。
package com.xxjz.sendemail;
public class TEST {
public static void main(String[] args) throws Exception {
SendMail sendMail = SendMail.getSingleton();
MailBox mailBox = new MailBox();
Mail mail = new Mail();
mailBox.setMailSmtpAuth("true");
mailBox.setMailSmtpHost("smtp.qq.com");
mailBox.setMailSmtpPort("587");
mailBox.setMailUser("xxxx@qq.com");
mailBox.setMailPassword("gpbutnthtrvrbabe");
mail.setSendTo("xxxx@qq.com");
mail.setText("� � * hello world\n");
mail.setSubject("����test");
sendMail.setProperties(mailBox);
sendMail.setMail(mail);
sendMail.sendQQSmtpEmail();
}
}
根据自己的邮箱填写相应的参数,就可以测试发送咯
如果大家想要代码,就点击下方链接(为了参加某些活动,设成vip的了)。不过,当然代码全部在上面,不下载也可以。文章来源:https://www.toymoban.com/news/detail-508346.html
https://download.csdn.net/download/qqhxmdq/87952277文章来源地址https://www.toymoban.com/news/detail-508346.html
到了这里,关于Java发送邮件-工具类-基于springboot的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!