无法通过gmail的SMTP服务器使用Java进行发送 [英] Trouble sending via gmail's SMTP server with Java

查看:171
本文介绍了无法通过gmail的SMTP服务器使用Java进行发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一位客户正在将Gmail用于商业(Google Apps的一部分),我不得不重新配置我开发的网站,以便与新的凭据相匹配。由于TLS错误导致了一些困难,我设法让代码在本地主机和我的测试服务器上工作(都是Apache Tomcat 5.5)。一切都很顺利,直到我不得不在他的服务器上移动它(托管公司告诉我的另一个Tomcat 5.5)。在客户端的服务器上,我收到以下错误消息:

  javax.mail.SendFailedException:发送失败; 
嵌套异常是:
class javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,端口:465;
嵌套异常是:
java.io.IOException:无法使用javax.net.ssl.SSLSocketFactory套接字工厂连接到主机,端口:smtp.gmail.com,465;异常: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);
字符串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);
字符串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));



验证器auth = null;

userName = Messages.getDBConfString(mail.username);
userPassword = Messages.getDBConfString(mail.userpassword); $!
$ b if(!CMSUtils.isEmptyString(userName)&&!CMSUtils.isEmptyString(userPassword)){
/ * props.put(mail.smtp.auth,true ); * /

auth =新SMTPAuthenticator(userName,userPassword);
}

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

address = new InternetAddress [1];

地址[0] =新InternetAddress(收件人);

mbText = new MimeBodyPart();
mbText.setContent(text,text / html);
mp = new MimeMultipart();
mp.addBodyPart(mbText);
} catch(MessagingException e){
e.printStackTrace();
抛出新的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 =密码
mail.from=website@example.com
mail.to=mail@example.com

我尝试了其他端口用于GMail,587,但它不适用于任何服务器。即使端口25也不会这样做。



那么,我做错了什么,我应该怎么做邮件工作?

解决方案

摆脱所有套接字工厂的属性;如果您使用的是相当新的JavaMail版本,则不需要它们。有关如何配置JavaMail访问Gmail的信息,请参阅 JavaMail FAQ 。如果仍然无法正常工作,那么您还会在其中找到调试技巧。

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

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


One of my customers is using Gmail for business (part of Google Apps) and I had to reconfigure the website I've developed so it would match the new credentials. After a bit of struggle due to TLS errors, I've managed to make the code work on localhost and on my test server (both Apache Tomcat 5.5). Everything was going smooth until I had to move it on his server (another Tomcat 5.5 as the hosting company told me). On the client's server I get the following error:

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

The strange thing is that on localhost and the test server the port 465 works fine, and the guys from hosting said that port is opened on their server.

The code that connects to the mailserver is:

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();
    }

}

With the following .properties file

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

I tried the other ports for GMail, 587, but it doesn't work on any of the servers. Not even port 25 won't do the trick.

So, what am I doing wrong, and what should I do to make the mailing work?

解决方案

Get rid of all the socket factory properties; if you're using a reasonably recent version of JavaMail you don't need them. See the JavaMail FAQ for how to configure JavaMail to access Gmail. You'll also find debugging tips there if it still doesn't work.

Also, change Session.getDefaultInstance to Session.getInstance.

And finally, if you're setting "mail.transport.protocol" to "smtps", you need to set the other properties as "mail.smtps." properties, not "mail.smtp." properties.

这篇关于无法通过gmail的SMTP服务器使用Java进行发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆