gpt4 book ai didi

java - 邮件发送失败,SMTP 连接失败

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

我尝试使用以下程序发送邮件,但收到以下错误消息。

             public class SMTPTest {
//private Logger log = Logger.getLogger(this.getClass());

public boolean sendSimpleMail(String to, String subject, String body) {
Properties props = new Properties();
props.put("mail.smtp.user", "amrita_test");
props.put("mail.smtp.password", "aview");
props.put("mail.smtp.host", "192.168.0.25");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.debug", "true");
props.put("mail.smtp.debug", "false");
props.put("mail.smtp.socketFactory.port", "425");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
String content = body;
msg.setSubject(subject);
msg.setFrom(new InternetAddress("buddhiedge@gmail.com"));
Address[] addresses = new Address[1];
addresses[0] = new InternetAddress("buddhiedge@gmail.com");
msg.setReplyTo(addresses);
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setContent(content, "text/html");
Transport.send(msg);
return true;
} catch (Exception mex) {
mex.printStackTrace();
System.out.println("Error in sending mail :: " + mex.getMessage());
return false;
}
}

private class SMTPAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("amrita_test",
"aview");
}
}
public static void main(String args[]){
SMTPTest test=new SMTPTest();
test.sendSimpleMail("praveenpkd@gmail.com", "subject", "body");
}
}

错误是: javax.mail.MessagingException:无法连接到 SMTP 主机:192.168.0.25,端口:25;

    nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:297)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at SMTPTest.sendSimpleMail(SMTPTest.java:45)
at SMTPTest.main(SMTPTest.java:62)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 8 more

但是 SMTP 服务器或连接没有问题。 远程登录 192.168.0.25 25连接良好。怎么解决这个问题。

最佳答案

请尝试通过仅从代码中删除以下内容来再次发送邮件。

    props.put("mail.smtp.socketFactory.port", "425");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

正常情况下,异常(exception)

java.net.ConnectException: Connection timed out

仅当 java 邮件无法在指定端口连接到上述服务器时才会出现。最可能的原因是 java mail 尝试连接到 session 属性中指定的 425 端口。

关于java - 邮件发送失败,SMTP 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16415222/

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