1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java发送QQ邮件问题Could not connect to SMTP host: smtp.qq.com port: 465(内附完整代码)

Java发送QQ邮件问题Could not connect to SMTP host: smtp.qq.com port: 465(内附完整代码)

时间:2022-02-13 12:20:41

相关推荐

Java发送QQ邮件问题Could not connect to SMTP host: smtp.qq.com  port: 465(内附完整代码)

邮件发送失败,本地测试可以,部署到服务器就不行。

之前就碰到这个问题,解决办法就是把port端口改成587,然后注释这一段

MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);properties.put("mail.smtp.ssl.enable", "true");properties.put("mail.smtp.ssl.socketFactory", sf);

之前这样改还可行,但是最近又不行了,报错

Could not connect to SMTP host: , port: 587;

试了重新生成授权码也不行,最后改好了,加上了这一句 ,指定协议。也可以去jdk配置里面删除关于这个协议的配置。

properties.put("mail.smtp.ssl.protocols", "TLSv1.2");

用了好几年了,最近换服务器才出现这个问题的。

Java发送QQ邮件完整代码

package .helper;import com.sun.mail.util.MailSSLSocketFactory;import javax.mail.*;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.util.Properties;public class SendEmail implements Runnable{private String email;// 收件人邮箱private String userName;// 用户名private Long id; // 用户idprivate String detail; //留言内容private int type; //留言/回复private String pageUrl; //页面路径private String articleTitle; //文章标题public SendEmail(String email, String detail, String userName, int type, String pageUrl, String articleTitle) {this.email = email;this.userName = userName;this.detail=detail;this.type = type;this.pageUrl=pageUrl;this.articleTitle=articleTitle;}public void run() {// 1.创建连接对象javax.mail.Session// 2.创建邮件对象 javax.mail.Message// 3.发送一封激活邮件String from = "z*******n@";// 发件人电子邮箱String host = ""; // 指定发送邮件的主机(QQ)|(网易)String code = "h******i"; // 发件人邮箱账号、授权码 Properties properties = System.getProperties();// 获取系统属性properties.setProperty("mail.smtp.host", host);// 设置邮件服务器properties.setProperty("mail.transport.protocol","smtp");// properties.setProperty("mail.smtp.port", "587"); //不需要 默认端口properties.setProperty("mail.smtp.auth", "true");// 打开认证// properties.put("mail.smtp.socketFactory", ".ssl.SSLSocketFactory");try {// QQ邮箱需要下面这段代码,163邮箱不需要MailSSLSocketFactory sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);properties.put("mail.smtp.ssl.enable", "true");properties.put("mail.smtp.ssl.socketFactory", sf);properties.put("mail.smtp.ssl.protocols", "TLSv1.2"); //加上这句解决问题// 1.获取默认session对象Session session = Session.getDefaultInstance(properties, new Authenticator() {@Overridepublic PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(from, code);}});session.setDebug(true);//2、通过session得到transport对象Transport transport = session.getTransport();//3、使用用户名和授权码连上邮件服务器transport.connect(host,from,code);// 4.创建邮件对象Message message = new MimeMessage(session);// 4.1设置发件人message.setFrom(new InternetAddress(from));// 4.2设置接收人message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));// 4.3设置邮件主题String title = "ZJBLOG留言回复通知";if(type==0){title = "亲爱的主人,ZJBLOG有新留言了";}else if(type ==1){title = "ZJBLOG留言回复通知";}else if(type ==2){title = "亲爱的主人,ZJBLOG有新评论了";}else if(type ==3){title = "ZJBLOG评论回复通知";}message.setSubject(title);// 2.4设置邮件内容String content = "";if(type==0){content = "<html><head></head><body>******</body></html>";}else if(type==3){content = "<html><head></head><body>*****</body></html>";}message.setContent(content, "text/html;charset=UTF-8");//5、发送邮件transport.sendMessage(message,message.getAllRecipients());//6、关闭连接transport.close();System.out.println("邮件成功发送!");} catch (Exception e) {e.printStackTrace();}}}

调用测试

public static void main(String[] args) {new Thread(new SendEmail("17***93@", "测试邮件内容","用户名zj",0,"","")).start();}

结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。