JavaMail 发送邮件失败
问题描述
用java 实现一个 发送邮件的Demo(用的是QQ邮箱) 但发送总是失败,QQ邮箱中的 Smtp 服务也都开启了,密码用的是获取的授权码
public static void main(String[] args) { // 收件人电子邮箱 String to = '123@qq.com'; // 发件人电子邮箱 String from = '456@qq.com'; // 指定发送邮件的主机为 smtp.qq.com String host = 'smtp.qq.com'; //QQ 邮件服务器 // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty('mail.smtp.host', host); properties.put('mail.smtp.auth', 'true'); properties.put('mail.smtp.port', '465'); properties.put('mail.smtp.socketFactory.port', '465'); properties.put('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory'); properties.put('mail.smtp.starttls.enable', 'true'); properties.put('mail.smtp.socketFactory.fallback', 'false'); properties.put('mail.smtp.starttls.enable', 'true'); properties.put('mail.smtp.ssl.trust', 'smtp.qq.com'); // 获取默认session对象 Session session = Session.getDefaultInstance(properties,new Authenticator(){ public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication('456@qq.com', '123123'); //发件人邮件用户名、密码 } }); session.setDebug(true); try{ // 创建默认的 MimeMessage 对象 MimeMessage message = new MimeMessage(session); // Set From: 头部头字段 message.setFrom(new InternetAddress(from)); // Set To: 头部头字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: 头部头字段 message.setSubject('This is the Subject Line!'); // 设置消息体 message.setText('This is actual message'); // 发送消息 Transport.send(message); System.out.println('Sent message successfully....from w3cschool.cc'); }catch (MessagingException mex) { mex.printStackTrace(); } }
错误提示DEBUG SMTP: trying to connect to host 'smtp.qq.com', port 465, isSSL falsejavax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465; nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targetat com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)at javax.mail.Service.connect(Service.java:317)at javax.mail.Service.connect(Service.java:176)at javax.mail.Service.connect(Service.java:125)at javax.mail.Transport.send0(Transport.java:194)at javax.mail.Transport.send(Transport.java:124)at com.syx.email.MailTest3.main(MailTest3.java:68)
有没有做过类似的大神指点一下 …^.^…
问题解答
回答1:SSL数字证书问题,简单点去掉数字证书验证试试 properties.put('mail.smtp.ssl.trust', 'smtp.qq.com'); // 这行先注释调
相关文章:
1. 修改mysql配置文件的默认字符集重启后依然不生效2. mongodb - windows7下mongod无法正常启动3. angular.js - 关于指令link 中的创建变量问题4. mysql - 请教一个Java做数据库缓存的问题5. php - 类似Apple官网顶部3级导航该如何设计数据库?6. Java局部变量为什么要初始化7. 作为新手,未定义索引username,求解,谢谢8. python - 如何让dataframe A 的一列与dataframe B的一列相减 相加结果记到A的c列?9. javascript - 豆瓣的这个自适应是怎么做的?10. java - 腾讯云mysql数据库可以插入数据,但是不能查询数据,求解答

网公网安备