gpt4 book ai didi

JavaMail API 不适用于我,但有异常 com.sun.mail.util.MailConnectException : Couldn't connect to host, 端口

转载 作者:行者123 更新时间:2023-12-02 09:56:42 26 4
gpt4 key购买 nike

我已经看过有关此问题的所有问题和答案,我已阅读常见问题解答,尝试了我找到的所有解决方案(除了编写自己的解决方案),我还关闭了不太安全的应用 Gmail 等。我检查了 telnet 连接,当然,尝试 ping smtp.gmail.com 并且一切正常。即使我已经用 C# 编写了(当然)一个小应用程序,这也花了我 5 分钟(而且这个可以工作!)。请给我一个线索,我该如何解决这个问题。对于使用 ttl 或 ssl 的每个解决方案,我都会得到这个堆栈跟踪

    com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
at javax.mail.Service.connect(Service.java:366)
at javax.mail.Service.connect(Service.java:246)
at com.luga.culturalpickup.UsableSendMail.sendFromGMail(UsableSendMail.java:64)
at com.luga.culturalpickup.UsableSendMail.main(UsableSendMail.java:84)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:359)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
... 5 more

我已完全禁用防火墙。

即使我在 JavaMail Api 周围使用了一些包装器来发送信件,但在内部它们却遇到了这个异常!

我想使用java发送小电子邮件!

<小时/>

例如,此代码会产生上面指定的错误

public class GoogleTest {

private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_PORT = "465";
private static final String emailMsgTxt = "Test Message Contents";
private static final String emailSubjectTxt = "A test from gmail";
private static final String emailFromAddress = "mail4@gmail.com";
private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[] sendTo = { "alex.95@mail.ru" };


public static void main(String args[]) throws Exception {

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

new GoogleTest().sendSSLMessage(sendTo, emailSubjectTxt,
emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully mail to All Users");
}

public void sendSSLMessage(String recipients[], String subject,
String message, String from) throws MessagingException {
boolean debug = true;

Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mail4@gmail.com", "password");
}
});

session.setDebug(debug);

Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
}

我得到以下堆栈跟踪

    DEBUG: JavaMail version 1.6.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.6.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
Exception in thread "main" com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
at javax.mail.Service.connect(Service.java:366)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:267)
at javax.mail.Transport.send0(Transport.java:252)
at javax.mail.Transport.send(Transport.java:174)
at com.luga.culturalpickup.GoogleTest.sendSSLMessage(GoogleTest.java:72)
at com.luga.culturalpickup.GoogleTest.main(GoogleTest.java:29)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:359)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:217)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
... 8 more

最佳答案

配置细节对我来说看起来不错。

最大的线索是您收到“连接被拒绝”。理论上,对此有几种可能的解释:

  • smtp.gmail.com 服务器全部关闭。这不可能。
  • smtp.gmail.com 服务器可能已将您的 IP 地址或包含您的 IP 地址的网络范围列入黑名单。 (也许 gmail.com 服务器通过您的 ISP 收到了太多垃圾邮件。)
  • 也许全国范围内都禁止发送电子邮件。 (例如,it has been claimed,中国有时会这样做。)
  • 也许 ISP 正在阻止这些端口上与 smtp.gmail.com 的连接...或者可能是该端口上的所有出站连接。这样做可能是为了防止 ISP 的客户发送垃圾邮件。查看您的 ISP 关于发送电子邮件的条款和条件,并查看有关您应该如何发送电子邮件的信息。
  • 也许您的组织正在阻止与 smtp.gmail.com 的连接。这样做可以让他们监控所有发出的电子邮件。它还可以防止垃圾邮件或可能被解释为垃圾邮件的行为。请咨询您本地的 IT 和网络经理。
  • 也许是您机器的内部防火墙。 (虽然你说你禁用了它......)

您可以执行一个简单的测试来验证这一点。使用“telnet”命令尝试在这两个端口上连接到 smtp.gmail.com。如果您收到“连接被拒绝”的消息,则这是某处发生某种阻塞的有力证据。

最后,如果端口似乎已打开,请根据本教程页面检查您正在执行的操作:

<小时/>

关于JavaMail API 不适用于我,但有异常 com.sun.mail.util.MailConnectException : Couldn't connect to host, 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55950092/

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