javax.mail.MessagingException: Could not connect to SMTP host: smtp.xx.com, port: 465;
2016-11-16 11:41
971 查看
使用javax.mail,通过SMTP来发送邮件,代码如下:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465;
解决办法:
我的环境是eclipse,过去在myeclipse中这段代码是没有错的。找出以前写的项目,测试了下,依旧是能正确运行的。
在网上寻找了一些相关信息:http://blog.csdn.net/levy_cui/article/details/51143104
这篇博客的评论里说是jdk1.8的security级别问题导致的,按照他的说法实验了一下,问题成功解决。
附上下载地址:百度网盘
@Override public void validateMail(String to) throws Exception{ //创建Properties,并设置主机和认证 Properties props = new Properties(); props.setProperty("mail.host", "smtp.qq.com"); props.setProperty("mail.smtp.auth", "true"); //开启SSL加密 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); //创建Authenticator内部类,重写getPasswordAuthentication()方法 Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("userName", "password"); } }; //获取Session Session session = Session.getInstance(props, auth); //创建MimeMessage MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("---------@qq.com")); msg.setRecipients(RecipientType.TO, to); msg.setSubject("来自的一封邮件"); msg.setContent("这是一封垃圾邮件", "text/html;charset=utf-8"); //发送 Transport.send(msg); }测试时抛出异常:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465;
解决办法:
我的环境是eclipse,过去在myeclipse中这段代码是没有错的。找出以前写的项目,测试了下,依旧是能正确运行的。
在网上寻找了一些相关信息:http://blog.csdn.net/levy_cui/article/details/51143104
这篇博客的评论里说是jdk1.8的security级别问题导致的,按照他的说法实验了一下,问题成功解决。
附上下载地址:百度网盘
相关文章推荐
- 解决问题:javax.mail.MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 25;
- javax.mail.MessagingException: Could not connect to SMTP host
- 关于使用Java Mail进行邮件发送,抛出Could not connect to SMTP host: xx@xxx.com, port: 25的异常可能
- Could not connect to SMTP host: smtp.***.com, port: 465, response: -1
- 发送自动邮件时Could not connect to SMTP host: 10.1.1.195, port: 25;报错的解决方法
- mac 配置 ssh 到git (Could not resolve hostname github.com, Failed to connect to github.com port 443 Operation timed out)
- Could not connect to SMTP host: localhost, port: 25;
- 黄聪:VPS服务器如何配置PHP.ini解决wordpress使用WP-Mail-SMTP插件发邮件出现Could not connect to SMTP host的解决办法
- Could not connect to SMTP host: pop.163.com, port: 25 解决方案
- git clone时出现ssh: connect to host github.com port 22: Connection timed out fatal: Could not read from
- Could not connect to host localhost on port 21212. java.net.ConnectException: Connection timed out: connect
- Could not connect to SMTP host: smtp.163.com, port: 25;阿里云 ECS
- Could not connect to SMTP host: smtp.qq.com, port: 25;
- 关于应用javamail的心得::Could not connect to SMTP host: smtp.sina.cn, port: 25
- Could not connect to SMTP host: smtp.163.com, port: 25
- Could not connect to SMTP host: localhost, port: 25;
- Could not connect to SMTP host,PKIX path building failed,unable to find valid certification path...
- 在服务器上用PHPMAIL发送邮件出现Could not connect to SMTP host错误 解决办法
- 解决2个smtp的问题: SMTP Error: Could not connect to SMTP host.
- Selenium2学习-038-firefox、webdriver版本不对称问题解决:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055