前排提醒
如果出现了javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
请看以下链接中的高赞答案(需翻墙)
前排提醒2
qq邮箱比其他邮箱多需要设置SSL加密和另外一个函数参数,已经在代码中标出
前排提醒3
需要的两个maven 依赖
前排提醒4
如需添加图片或附件到邮件中,请参考MIME Email的内容撰写方法(下面也有附代码)
参考视频
纯文本
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class MailPracticeSend {
private static String username = "你的发送用的邮箱地址";
private static String password = "你的密码(QQ邮箱用的是授权码)";
private static String recipient = "收件人邮箱地址";
public static void main(String[] args) {
Properties prop = new Properties();
prop.setProperty("mail.host","smtp.qq.com"); //设置QQ邮件服务器
prop.setProperty("mail.transport.protocol","smtp"); //设置邮件发送协议
prop.setProperty("mail.smtp.auth","true"); //设置需要验证用户名密码
//关于QQ邮箱,还要设置SSL加密,加上以下代码即可,其他品牌邮箱不需要
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
}catch(GeneralSecurityException e){
e.printStackTrace();
}
if(sf!=null) {
sf.setTrustAllHosts(true);
prop.setProperty("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
}
//使用JavaMail发送邮件的五个步骤
//1.创建定义整个应用程序所需要的环境信息的Session对象
Session session = Session.getDefaultInstance(prop,
//以下参数(Authenticator对象)为QQ邮箱特别需要
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
session.setDebug(true); //开启Session的Debug模式,这样就可以查看到程序发送Email的运行状态
//2.通过Session得到transport对象
Transport ts = null;
try {
ts = session.getTransport();
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
}
try {
//3.使用邮箱的用户名和授权码连上邮件服务器
ts.connect(prop.getProperty("mail.host"),username, password);
//4.创建邮件
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject("由Java 控制台程序自动发送的邮件", "UTF-8");
message.setContent("<h1 style=\"color:blue\">hello,world!你好,世界!</h1>", "text/html;charset=UTF-8");
//5.发送邮件
ts.sendMessage(message, message.getAllRecipients());
//6.关闭连接
ts.close();
}catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
含图片、附件、文本的邮件(MIME)
package com.smbms.util;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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.security.GeneralSecurityException;
import java.util.Properties;
public class MailPracticeSend {
private static String username = "发送者的邮箱地址";
private static String password = "发送者的密码(QQ邮箱是授权码)";
private static String recipient = "收件人地址";
public static void main(String[] args) {
Properties prop = new Properties();
prop.setProperty("mail.host","smtp.qq.com"); //设置QQ邮件服务器
prop.setProperty("mail.transport.protocol","smtp"); //设置邮件发送协议
prop.setProperty("mail.smtp.auth","true"); //设置需要验证用户名密码
//关于QQ邮箱,还要设置SSL加密,加上以下代码即可,其他品牌邮箱不需要
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
}catch(GeneralSecurityException e){
e.printStackTrace();
}
if(sf!=null) {
sf.setTrustAllHosts(true);
prop.setProperty("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
}
//使用JavaMail发送邮件的五个步骤
//1.创建定义整个应用程序所需要的环境信息的Session对象
Session session = Session.getDefaultInstance(prop,
//以下参数(Authenticator对象)为QQ邮箱特别需要
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
session.setDebug(true); //开启Session的Debug模式,这样就可以查看到程序发送Email的运行状态
//2.通过Session得到transport对象
Transport ts = null;
try {
ts = session.getTransport();
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
}
try {
//3.使用邮箱的用户名和授权码连上邮件服务器
ts.connect(prop.getProperty("mail.host"),username, password);
//4.创建邮件
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject("由Java 控制台程序自动发送的邮件", "UTF-8");
//message.setContent("<h1 style=\"color:blue\">hello,world!你好,世界!</h1>", "text/html;charset=UTF-8");
//MIME Multipart(带图片、附件的)邮件内容
//准备图片数据
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("图片路径"));
image.setDataHandler(dh);
image.setContentID("gys.png");
//准备正文数据
MimeBodyPart text = new MimeBodyPart();
text.setContent("<h2>hello,world!这是一封邮件正文带图片<img src='cid:gys.png'>的邮件</h2>","text/html;charset=UTF-8");
//描述数据关系(只包含图片和文字时,启用这段代码)
// MimeMultipart mm = new MimeMultipart();
// mm.addBodyPart(image);
// mm.addBodyPart(text);
// mm.setSubType("mixed");
//准备附件数据
MimeBodyPart body3 = new MimeBodyPart();//附件1
body3.setDataHandler(new DataHandler(new FileDataSource("附件1路径")));
body3.setFileName("附件1别名");
MimeBodyPart body4 = new MimeBodyPart();//附件2
body4.setDataHandler(new DataHandler(new FileDataSource("附件2路径")));
body4.setFileName("");
//拼装邮件正文(文字+图片)内容
MimeMultipart multipart1 = new MimeMultipart();
multipart1.addBodyPart(image);
multipart1.addBodyPart(text);
multipart1.setSubType("related");//也可以写mixed
//将拼装好的正文内容设置为主体
MimeBodyPart contentText = new MimeBodyPart();
contentText.setContent(multipart1);
//拼接附件
MimeMultipart allFile = new MimeMultipart();
allFile.addBodyPart(body3);
allFile.addBodyPart(body4);
allFile.addBodyPart(contentText);
allFile.setSubType("mixed");
//设置到邮件中,保存修改
message.setContent(allFile);
message.saveChanges();
//5.发送邮件
ts.sendMessage(message, message.getAllRecipients());
//6.关闭连接
ts.close();
}catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}