gpt4 book ai didi

java邮件smtp连接问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:56 24 4
gpt4 key购买 nike

我正在尝试以下示例使用从 here 中选择的 ssl 发送邮件并如下更改我的凭据并获得如图所示的异常,你能帮我解决吗

import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;


public class SimpleSSLMail {

private static final String SMTP_HOST_NAME = "xx.xx.xx.xx";
private static final int SMTP_HOST_PORT = 25 ;//465;
private static final String SMTP_AUTH_USER = "xxx@yyy.com";
private static final String SMTP_AUTH_PWD = "xxxx";

public static void main(String[] args) throws Exception{
new SimpleSSLMail().test();
}

public void test() throws Exception{
Properties props = new Properties();

props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
// props.put("mail.smtps.quitwait", "false");

Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("This is a test", "text/plain");

message.addRecipient(Message.RecipientType.TO,
new InternetAddress("myid@gmail.com"));

transport.connect
(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}

异常(exception):

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host:xx.xx.xx.xx, port: 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:275)

最佳答案

出现此类问题是由于网络问题。可能是您需要身份验证,您可能输入了错误的用户名、密码。

无论如何,您的代码中遗漏了很多东西。获取与 gmail 服务器的正确 SSL 连接的示例

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.port", "465");

我在您的代码中的任何地方都看不到这一点。那么认证的方法在哪里呢?

只需查看以下链接:

Sending E-mail Using Gmail In Java 在这里您可以选择使用 TLS 或 SSl

Java Mail API

E-mailing through Java

keys used in java mailing system for your reference

关于java邮件smtp连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6941294/

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