gpt4 book ai didi

java - 使用 Java 通过 gmail 的 SMTP 服务器发送时遇到问题

转载 作者:行者123 更新时间:2023-11-28 21:56:55 25 4
gpt4 key购买 nike

我的一位客户正在使用 Gmail 开展业务(Google Apps 的一部分),我不得不重新配置我开发的网站,以便它与新的凭据相匹配。由于 TLS 错误,经过一番努力后,我设法使代码在本地主机和我的测试服务器(均为 Apache Tomcat 5.5)上运行。一切都很顺利,直到我不得不将它移到他的服务器上(托管公司告诉我的是另一个 Tomcat 5.5)。在客户端的服务器上,我收到以下错误:

javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.io.IOException: Couldn't connect using "javax.net.ssl.SSLSocketFactory" socket factory to host, port: smtp.gmail.com, 465; Exception: java.lang.reflect.InvocationTargetException

奇怪的是,在本地主机和测试服务器上,端口 465 工作正常,托管人员说该端口在他们的服务器上打开。

连接到邮件服务器的代码是:

private void initConfigParams() throws CMSGeneralException {
try {
props = System.getProperties();
String smtpHost = Messages.getDBConfString("mail.smtp.host");
String mailPort = Messages.getDBConfString("mail.smtp.port");
String socketFallback = Messages.getDBConfString("mail.smtp.socketFactory.fallback");
String enableTls = Messages.getDBConfString("mail.smtp.starttls.enable");
String authSmtp = Messages.getDBConfString("mail.smtp.auth");
String tlsRequired = Messages.getDBConfString("mail.smtp.stattls.required");
String sktFactory = Messages.getDBConfString("mail.smtp.socketFactory.class");

props.put("mail.smtp.starttls.enable", enableTls);
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", authSmtp);
props.put("mail.smtp.starttls.required", tlsRequired);


props.setProperty("mail.smtp.socketFactory.class", sktFactory);
props.setProperty("mail.smtp.socketFactory.fallback", socketFallback);
props.setProperty("mail.smtp.port", mailPort);
props.setProperty("mail.smtp.socketFactory.port", mailPort);



props.put("mail.transport.protocol", Messages.getDBConfString("mail.transport.protocol"));



Authenticator auth = null;

userName = Messages.getDBConfString("mail.username");
userPassword = Messages.getDBConfString("mail.userpassword");

if (!CMSUtils.isEmptyString(userName) && !CMSUtils.isEmptyString(userPassword)){
/* props.put("mail.smtp.auth", "true"); */

auth = new SMTPAuthenticator(userName, userPassword);
}

session = Session.getDefaultInstance(props, auth);
session.setDebug(false);

address = new InternetAddress[1];

address[0] = new InternetAddress(recipients);

mbText = new MimeBodyPart();
mbText.setContent(text, "text/html");
mp = new MimeMultipart();
mp.addBodyPart(mbText);
} catch (MessagingException e) {
e.printStackTrace();
throw new CMSGeneralException();
}

}

使用以下 .properties 文件

mail.smtp.starttls.enable=true
mail.smtp.host=smtp.gmail.com
mail.smtp.auth=true
mail.smtp.starttls.required=true
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
mail.smtp.port=465
mail.transport.protocol=smtps
mail.username=website@example.com
mail.userpassword=password
mail.from=website@example.com
mail.to=mail@example.com

我尝试了 GMail 的其他端口 587,但它在任何服务器上都不起作用。甚至端口 25 也无法解决问题。

那么,我做错了什么,我应该怎么做才能使邮寄工作正常进行?

最佳答案

去掉所有的套接字工厂属性;如果您使用的是相当新的 JavaMail 版本,则不需要它们。查看JavaMail FAQ for how to configure JavaMail to access Gmail .如果它仍然不起作用,您还会在那里找到调试提示。

此外,将 Session.getDefaultInstance 更改为 Session.getInstance。

最后,如果您将“mail.transport.protocol”设置为“smtps”,您需要将其他属性设置为“mail.smtps.”属性,而不是“mail.smtp”。”属性。

关于java - 使用 Java 通过 gmail 的 SMTP 服务器发送时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10409517/

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