gpt4 book ai didi

gmail - JavaMail - java.net.ConnectException : Connection timed out: connect

转载 作者:行者123 更新时间:2023-12-05 04:16:34 26 4
gpt4 key购买 nike

这是我的代码。虽然发送邮件应该很简单,但我一直遇到下面提到的错误。我试过到处搜索,但没有结果。提前致谢!

import java.util.Properties;  
import javax.mail.*;
import javax.mail.internet.*;

public class SendMailBySite {
public static void main(String[] args) {

String to="abc@gmail.com";//change accordingly

//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc@gmail.com","********");//change accordingly
}
});

//compose message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc@gmail.com"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Hello");
message.setText("Testing.......");

//send message
Transport.send(message);

System.out.println("message sent successfully");

} catch (MessagingException e) {throw new RuntimeException(e);}

}
}

这是遇到的错误:

java.net.ConnectException: Connection timed out: connect

at java.net.DualStackPlainSocketImpl.connect0(Native Method)

at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)

at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)

at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)

at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)

at java.net.Socket.connect(Socket.java:579)

at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)

at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)

at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)

at ftpserver.FTPServer.main(FTPServer.java:25)

最佳答案

首先,修复所有这些common mistakes .

然后阅读这些debugging tips for connection problems .

您可能想阅读这些 tips for connecting to Gmail .

关于gmail - JavaMail - java.net.ConnectException : Connection timed out: connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26949800/

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