美文网首页
javaMail笔记

javaMail笔记

作者: 帅大叔的简书 | 来源:发表于2016-08-16 17:11 被阅读216次
    public class MailTool {
    public static void main(String[] args) throws MessagingException, GeneralSecurityException {
    Properties props = new Properties();
    // 开启debug调试
    props.setProperty("mail.debug", "true");
    // 发送服务器需要身份验证
    props.setProperty("mail.smtp.auth", "true");
    // 设置邮件服务器主机名
    props.setProperty("mail.host", "smtp.qq.com");
    // 发送邮件协议名称
    props.setProperty("mail.transport.protocol", "smtp");
    MailSSLSocketFactory sf = new MailSSLSocketFactory();
    sf.setTrustAllHosts(true);
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.ssl.socketFactory", sf);
    Session session = Session.getInstance(props);
    Message msg = new MimeMessage(session);
    //标题
    msg.setSubject("搞定");
    StringBuilder builder = new StringBuilder();
    builder.append("我的主页 = " + "http://rstyro.top");
    builder.append("\n"+"QQ邮箱发送邮件,搞定");
    builder.append("\n"+"这是我用程序写的");
    builder.append("\n时间 " + System.currentTimeMillis());
    msg.setText(builder.toString());
    msg.setFrom(new InternetAddress("1006059906@qq.com"));
    Transport transport = session.getTransport();
    //发件人邮箱
    transport.connect("smtp.qq.com", "****@qq.com", "自己的授权码");
    //收件人邮箱,下面同时给多人
    transport.sendMessage(msg, new Address[] { new InternetAddress("******@qq.com"),new InternetAddress("****@qq.com")});
    transport.close();
    }
    }
    

    发送邮件失败Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465;
    nested exception is: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure. Failed messages: javax.mail. MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465;
    nested exception is:javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

    你这个是jdk导致的,jdk里面有一个jce的包,安全性机制导致的访问https会报错,官网上有替代的jar包,换掉就好了
    包在这里
    http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
    目录
    %JAVA_HOME%\jre\lib\security里的local_policy.jar,US_export_policy.jar

    原文链接:http://www.jianshu.com/p/5ba3bde60f21
    原文链接:http://www.oschina.net/question/2282830_247657

    相关文章

      网友评论

          本文标题:javaMail笔记

          本文链接:https://www.haomeiwen.com/subject/vgvpsttx.html