1.获取邮箱SMTP服务
例:qq邮箱
注意:某些时候这个验证会抽风出不来,注销邮箱重新开启便可,这里不多做教程了
2.创建Java项目配置依赖
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
//配置读取配置文件使用,springboot也可写在主配置文件无需lang依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
3.创建一个读取配置文件的工具类(按个人习惯了,用spring可以直接配置在主配置文件里,这样的话并不需要这个工具类)
package com.yzdk.yzdk.Util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class PropertiesUtil {
private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static Properties props;
static {
String fileName = "maill.properties";
props = new Properties();
try {
props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
} catch (IOException e) {
logger.error("配置文件读取异常",e);
}
}
public static String getProperty(String key){
String value = props.getProperty(key.trim());
if(StringUtils.isBlank(value)){
return null;
}
return value.trim();
}
public static String getProperty(String key,String defaultValue){
String value = props.getProperty(key.trim());
if(StringUtils.isBlank(value)){
value = defaultValue;
}
return value.trim();
}
}
4.编写配置文件mail.properties
#发送者邮箱
sendMail = 你的邮箱
#腾讯邮箱服务器
host = smtp.qq.com
#授权码
password = 你的授权码
5.创建邮箱工具类
package com.yzdk.yzdk.Util;
import com.sun.mail.util.MailSSLSocketFactory;
import sun.print.BackgroundLookupListener;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class MailUtil {
private static String sendMail = PropertiesUtil.getProperty("sendMail");
private static String host = PropertiesUtil.getProperty("host");
private static String password = PropertiesUtil.getProperty("password");
private String title;
private String main;
private String toMail;
public MailUtil(String title, String main, String toMail) {
this.title = title;
this.main = main;
this.toMail = toMail;
}
public static boolean send(String title,String main,String toMail) throws GeneralSecurityException {
MailUtil mailUtil = new MailUtil(sendMail,host,password);
boolean result = mailUtil.sendMail(title,main,toMail);
return result;
}
private static boolean sendMail(String title,String main,String toMail) throws GeneralSecurityException {
boolean send = false;
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(sendMail, password); //发件人邮件用户名、授权码
}
});
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(sendMail));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(toMail));
// Set Subject: 头部头字段
message.setSubject(title);//标题
// 设置消息体
message.setText(main);//内容
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully");
send = true;
}catch (MessagingException mex) {
mex.printStackTrace();
}
return send;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
public String getToMail() {
return toMail;
}
public void setToMail(String toMail) {
this.toMail = toMail;
}
}
6.测试
package com.yzdk.yzdk;
import com.yzdk.yzdk.Util.MailUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.security.GeneralSecurityException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class YzdkApplicationTests {
@Test
public void contextLoads() throws GeneralSecurityException {
MailUtil.send("你好","hello","你要给谁发送");
}
}
注意:springboot请先配置好数据库连接,或者创建时没有选择sql依赖