import java.util.PRoperties;import javax.mail.Message;import javax.mail.session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import com.sun.mail.util.MailSSLSocketFactory;/** * JavaMail发送邮件:前提是QQ邮箱里帐号设置要开启POP3/SMTP协议,如何开启自行百度 */public class SendMail { public static void main(String[] args) throws Exception { Properties prop = new Properties(); // 开启debug调试,以便在控制台查看 //prop.setProperty("mail.debug", "true"); // 设置邮件服务器主机名 prop.setProperty("mail.host", "smtp.qq.com"); // 发送服务器需要身份验证 prop.setProperty("mail.smtp.auth", "true"); // 发送邮件协议名称 prop.setProperty("mail.transport.protocol", "smtp"); // 开启SSL加密,否则会失败 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable", "true"); prop.put("mail.smtp.ssl.socketFactory", sf); // 创建session Session session = Session.getInstance(prop); // 通过session得到transport对象 Transport ts = session.getTransport(); // 连接邮件服务器:邮箱类型,帐号,授权码代替密码(更安全) ts.connect("smtp.qq.com", "QQ账号", "授权码"); // 创建邮件 Message message = createSimpleMail(session); // 发送邮件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createSimpleMail * @Description: 创建一封只包含文本的邮件 */ public static MimeMessage createSimpleMail(Session session) throws Exception { // 创建邮件对象 MimeMessage message = new MimeMessage(session); // 指明邮件的发件人 message.setFrom(new InternetAddress("xxx@qq.com")); // 指明邮件的收件人(测试时可以自己发给自己) message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxx@qq.com")); // 邮件的标题 message.setSubject("JavaMail测试"); // 邮件的文本内容 message.setContent("JavaMail发送邮件成功!", "text/html;charset=UTF-8"); // 返回创建好的邮件对象 return message; }}javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure 解决方案
这个是jdk导致的,jdk里面有一个jce的包,安全性机制导致的访问https会报错,官网上有替代的jar包,换掉就好了
目录 %JAVA_HOME%/jre/lib/security里的local_policy.jar,US_export_policy.jar
JDK7 http://www.Oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
JDK8 http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
新闻热点
疑难解答