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



我的一个客户正在使用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 常见问题解答,了解如何配置 JavaMail 以访问 Gmail。 如果它仍然不起作用,您还会在那里找到调试提示。

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

最后,如果要将"mail.transport.protocol"设置为"smtps

",则需要将其他属性设置为"mail.smtps"。"属性,而不是"邮件.smtp"。性能。

看来你对java.lang.reflect.InvocationTargetException有问题.所以,我的意思是这不是网络问题。可能是您指定了一些错误的参数,并且 JavaMail API 路由无法调用方法。可能您还应该指定属性mail.smtp.ssl.socketFactory

此处的一些文档 http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html .

最新更新