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

通过gmail发送时遇到问题';使用Java的SMTP服务器,java,tomcat,gmail,jakarta-mail,mailing,Java,Tomcat,Gmail,Jakarta Mail,Mailing,我的一个客户正在使用Gmail for business(谷歌应用程序的一部分),我不得不重新配置我开发的网站,使其与新的凭据相匹配。由于TLS错误,经过一段时间的努力,我已经成功地使代码在本地主机和测试服务器(ApacheTomcat5.5)上工作。一切都很顺利,直到我不得不在他的服务器上移动它(托管公司告诉我的另一个Tomcat5.5)。在客户端服务器上,我收到以下错误: javax.mail.SendFailedException: Sending failed; nested exc

我的一个客户正在使用Gmail for business(谷歌应用程序的一部分),我不得不重新配置我开发的网站,使其与新的凭据相匹配。由于TLS错误,经过一段时间的努力,我已经成功地使代码在本地主机和测试服务器(ApacheTomcat5.5)上工作。一切都很顺利,直到我不得不在他的服务器上移动它(托管公司告诉我的另一个Tomcat5.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也不行


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

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


这里有一些文档。

去掉所有套接字工厂属性;如果您使用的是合理的最新版本的JavaMail,则不需要它们。看。如果仍然不起作用,您还可以在那里找到调试提示

另外,将Session.getDefaultInstance更改为Session.getInstance


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

能否在日志中找到有关调用TargetException的更多信息?希望有堆栈跟踪?在麻烦的服务器上使用了什么JVM?尝试正确指定mail.smtp.ssl.socketFactory,但我得到了相同的错误…这只是一个预测。您是否尝试ping此主机?此外,请尝试使用任何telnet客户端连接到此主机和此端口。或者使用某些软件(例如WireShark)监视网络活动。可能是网络问题。