gpt4 book ai didi

javax.mail : not picking up properties

转载 作者:行者123 更新时间:2023-12-02 01:31:05 25 4
gpt4 key购买 nike

我正在尝试从代码向 smtp 服务器发送电子邮件,该服务器既不在本地主机上,也不在默认端口 25 上。

我有一个如下所示的代码:

// Set the host SMTP address
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", hostname);
props.put("mail.smtp.port", "8025");
props.put("mail.smtp.auth", "true");

// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
Session session = Session.getInstance(props, auth);

Transport trans = session.getTransport("smtp");

...piece of the code where message is created...

trans.send(message);

但它在 Transport.send() 上失败,并出现超时 -1 错误,因为尝试连接到端口 25 上的本地主机,但不连接到具有上面指定端口的主机。

我的问题是如何检查现有属性(默认 localhost:25)或者是否已生成任何其他传输 session ?

最佳答案

send 方法是静态的,因此它使用给定消息的 session 属性。如果您想使用您创建的传输,您需要调用 connect , sendMessage ,然后 close .

// Set the host SMTP address
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", hostname);
props.put("mail.smtp.port", "8025");
props.put("mail.smtp.auth", "true");

// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
Session session = Session.getInstance(props, auth);

Transport trans = session.getTransport("smtp");

//...piece of the code where message is created...

trans.connect();
try {
trans.sendMessage(message, message.getAllRecipients());
} finally {
trans.close();
}

关于javax.mail : not picking up properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56049719/

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