首先要导入maven依赖:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
注意这里不要导错依赖,一开始我是导入的下面的依赖:
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
程序没有语法错误,但是就是一直报错:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at com.zang.mail.SimpleMail.main(SimpleMail.java:48)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:170)
at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:98)
at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:238)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:394)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:373)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 4 more
Process finished with exit code 1
最后附上代码:
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SimpleMail {
public static void main(String[] args) throws Exception{
Properties properties = new Properties();
//设置QQ邮件服务器
properties.setProperty("mail.host","smtp.qq.com");
//邮件发送协议
properties.setProperty("mail.transport.protocol","smtp");
//需要验证用户名密码
properties.setProperty("mail.smtp.auth","true");
//还要设置SSL加密,加上以下代码即可
MailSSLSocketFactory mailSSLSocketFactory = new MailSSLSocketFactory();
mailSSLSocketFactory.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable","true");
properties.put("mail.smtp.ssl.socketFactory",mailSSLSocketFactory);
//使用JavaMail发送邮件的5个步骤
//1、创建定义整个应用程序所需环境信息的 Session 对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
//发件人用户名,授权码
return new PasswordAuthentication("邮箱","授权码");
}
});
//开启Session的debug模式,这样就可以查看程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport transport = session.getTransport();
//3、使用用户名和授权码连上邮件服务器
transport.connect("smtp.qq.com","邮箱","授权码");
//4、创建邮件:写邮件
//注意需要传递Session
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress("邮箱"));
//指明邮件的收件人,现在发件人和收件人是一样的,就是自己给自己发
message.setRecipient(Message.RecipientType.TO , new InternetAddress("邮箱"));
message.setSubject("只包含文本的简单邮件");
message.setContent("你好啊","text/html;charset=UTF-8");
//5、发送邮件
transport.sendMessage(message,message.getAllRecipients());
//6、关闭连接
transport.close();
}
}