gpt4 book ai didi

java - Java发送邮件时出错

转载 作者:太空宇宙 更新时间:2023-11-04 06:37:51 28 4
gpt4 key购买 nike

我有桌面应用程序,但在发送邮件 javax.mail.MessagingException 时遇到此错误:无法连接到 SMTP 主机:smtp.gmail.com,端口:587;嵌套异常是:java.net.ConnectException:连接被拒绝:连接

~code~

String host="smtp.mail.yahoo.com";
Properties props = new Properties();

props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.host",host);
props.put("mail.smtp.host", host);
props.put("mail.stmp.user", "abc@yahoo.ca");//User name
//To use TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "mypassword"); //Password
props.put("mail.smtp.socketFactory.fallback", "false");
Session session1 = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String username = "abc@yahoo.ca";
String password = "mypassword";
return new PasswordAuthentication(username, password);
}
});



MimeMessage msg = new MimeMessage(session1);
String from = "abc@yahoo.ca";
String subject = "Testing...";

msg.setFrom(new InternetAddress(from));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));
msg.setSubject(subject);
Transport.send(msg);

最佳答案

YahooMail smtp port465587

添加此:-

props.put("mail.smtp.port", "465");

props.put("mail.smtp.port", "587");

关于java - Java发送邮件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25117335/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com