我的一位客户正在使用Gmail营业(部分Google Apps),我不得不重新配置我开发的网站,以便与新凭据相匹配。在由于TLS错误而经过一些努力之后,我设法让代码在localhost和我的测试服务器上运行(两者都是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
奇怪的是,在localhost和测试服务器端口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号端口都不会做到这一点。
那么,我做错了什么,我该怎样做才能使邮件发挥作用?
答案 0 :(得分:2)
摆脱所有套接字工厂属性;如果您使用的是最新版本的JavaMail,则不需要它们。请参阅JavaMail FAQ for how to configure JavaMail to access Gmail。如果它仍然不起作用,你也会在那里找到调试技巧。
另外,将Session.getDefaultInstance更改为Session.getInstance。
最后,如果您将“mail.transport.protocol”设置为“smtps”,则需要将其他属性设置为“mail.smtps。”属性,而不是“mail.smtp。“properties。
答案 1 :(得分:0)
您似乎遇到java.lang.reflect.InvocationTargetException
的问题。所以,我的意思是这不是一个网络问题。可能是您指定了一些错误的参数,并且JavaMail API路由无法调用方法。也许你应该指定属性mail.smtp.ssl.socketFactory
。
这里有一些文档http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html。