package cn.itcast.travel.util;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
发邮件工具类
*/
public final class MailUtils {
private static final String USER = “[email protected]”; // 发件人称号,同邮箱地址
private static final String PASSWORD = “fspkyqwpagaihgfj”; // 如果是qq邮箱可以使户端授权码,或者登录密码
/**
*
@param to 收件人邮箱
@param text 邮件正文
@param title 标题
/
/ 发送验证信息的邮件 */
public static boolean sendMail(String to, String text, String title){
try {
final Properties props = new Properties();
props.setProperty(“mail.transport.protocol”, “SMTP”);// 连接协议
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.host”, “smtp.qq.com”);
props.put(“mail.debug”, “true”);
props.put(“mail.smtp.connectiontimeout”,“20000”);
props.put(“mail.smtp.timeout”,“25000”);
props.put(“mail.smtp.writetimeout”,“20000”);
props.put(“mail.smtp.ssl.enable”, “true”);
// 发件人的账号
props.put(“mail.user”, USER);
//发件人的密码
props.put(“mail.password”, PASSWORD);
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
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);
try {
//防止成为垃圾邮件,披上outlook的马甲
message.addHeader("X-Mailer","Microsoft Outlook Express");
// 设置发件人
String username = props.getProperty("mail.user");
InternetAddress form = new InternetAddress(username);
message.setFrom(form);
// 设置收件人
InternetAddress toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 设置邮件标题
message.setSubject(title);
// 设置邮件的内容体
message.setContent(text, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
return true;
}catch (Exception e){
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
return false;
}
public static void main(String[] args) throws Exception { // 做测试用
MailUtils.sendMail(“[email protected]”,“您收到是一封激活邮件”,“旅游网”);
}
}
“D:\idea\IntelliJ IDEA 2019.2.3\jbr\bin\java.exe” “-javaagent:D:\idea\IntelliJ IDEA 2019.2.3\lib\idea_rt.jar=57125:D:\idea\IntelliJ IDEA 2019.2.3\bin” -Dfile.encoding=UTF-8 -classpath C:\Users\Smlie.Wang\Desktop\shanHe_travel\target\classes;C:\Users\Smlie.Wang.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Smlie.Wang.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Smlie.Wang.m2\repository\mysql\mysql-connector-java\8.0.18\mysql-connector-java-8.0.18.jar;C:\Users\Smlie.Wang.m2\repository\com\google\protobuf\protobuf-java\3.6.1\protobuf-java-3.6.1.jar;C:\Users\Smlie.Wang.m2\repository\com\alibaba\druid\1.1.21\druid-1.1.21.jar;C:\Users\Smlie.Wang.m2\repository\org\springframework\spring-core\4.1.2.RELEASE\spring-core-4.1.2.RELEASE.jar;C:\Users\Smlie.Wang.m2\repository\org\springframework\spring-jdbc\4.1.2.RELEASE\spring-jdbc-4.1.2.RELEASE.jar;C:\Users\Smlie.Wang.m2\repository\org\springframework\spring-tx\4.1.2.RELEASE\spring-tx-4.1.2.RELEASE.jar;C:\Users\Smlie.Wang.m2\repository\org\springframework\spring-beans\4.1.2.RELEASE\spring-beans-4.1.2.RELEASE.jar;C:\Users\Smlie.Wang.m2\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;C:\Users\Smlie.Wang.m2\repository\commons-beanutils\commons-beanutils\1.9.2\commons-beanutils-1.9.2.jar;C:\Users\Smlie.Wang.m2\repository\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;C:\Users\Smlie.Wang.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.3.3\jackson-databind-2.3.3.jar;C:\Users\Smlie.Wang.m2\repository\com\fasterxml\jackson\core\jackson-core\2.3.3\jackson-core-2.3.3.jar;C:\Users\Smlie.Wang.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.3.3\jackson-annotations-2.3.3.jar;C:\Users\Smlie.Wang.m2\repository\javax\mail\javax.mail-api\1.6.0\javax.mail-api-1.6.0.jar;C:\Users\Smlie.Wang.m2\repository\com\sun\mail\javax.mail\1.6.0\javax.mail-1.6.0.jar;C:\Users\Smlie.Wang.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Users\Smlie.Wang.m2\repository\redis\clients\jedis\2.7.0\jedis-2.7.0.jar;C:\Users\Smlie.Wang.m2\repository\org\apache\commons\commons-pool2\2.3\commons-pool2-2.3.jar cn.itcast.travel.util.MailUtils
DEBUG: JavaMail version 1.6.0
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: protocolConnect returning false, host=smtp.qq.com, [email protected], password=
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host “smtp.qq.com”, port 465, isSSL true
javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465;
nested exception is:
javax.net.ssl.SSLException: Couldn’t kickstart handshaking
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2196)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:726)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at cn.itcast.travel.util.MailUtils.sendMail(MailUtils.java:70)
at cn.itcast.travel.util.MailUtils.main(MailUtils.java:82)
Caused by: javax.net.ssl.SSLException: Couldn’t kickstart handshaking
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:127)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:320)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:263)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:405)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:619)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:393)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2160)
… 8 more
Suppressed: java.net.SocketException: Connection reset by peer: socket write error
at java.base/java.net.SocketOutputStream.socketWrite0(Native Method)
at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:110)
at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150)
at com.sun.mail.util.TimeoutOutputStream.write(WriteTimeoutSocket.java:386)
at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:351)
… 14 more
Caused by: java.net.SocketException: Connection reset by peer: socket write error
at java.base/java.net.SocketOutputStream.socketWrite0(Native Method)
at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:110)
at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150)
at com.sun.mail.util.TimeoutOutputStream.write(WriteTimeoutSocket.java:386)
at java.base/sun.security.ssl.SSLSocketOutputRecord.flush(SSLSocketOutputRecord.java:251)
at java.base/sun.security.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:89)
at java.base/sun.security.ssl.ClientHello$ClientHelloKickstartProducer.produce(ClientHello.java:658)
at java.base/sun.security.ssl.SSLHandshake.kickstart(SSLHandshake.java:515)
at java.base/sun.security.ssl.ClientHandshakeContext.kickstart(ClientHandshakeContext.java:107)
at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:227)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:395)
… 12 more
Process finished with exit code 0