【问题标题】:Sending an Email Using Commons-Email to Gmail使用 Commons-Email 向 Gmail 发送电子邮件
【发布时间】:2010-12-19 12:22:09
【问题描述】:
Email email = new SimpleEmail();
String authuser = "......@gmail.com";
String authpwd = "*******";
// Very Important, Don't use email.setAuthentication()
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true); // true if you want to debug
email.setHostName("smtp.gmail.com");

email.getMailSession().getProperties().put("mail.smtp.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtp.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("........@gmail.com", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo(".............@gmail.com", "ToName");
email.send();

它给出了以下异常

SEVERE: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465

【问题讨论】:

  • 你能发布整个异常堆栈跟踪吗?异常的原因可能在其中的某处列出。例外是通用的。这可能是简单的身份验证失败,也可能是其他原因。欢呼

标签: java email apache-commons-email


【解决方案1】:

这对我有用

Email email = new SimpleEmail();
String authuser = "...@gmail.com";
String authpwd = "xxxx";
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
email.setDebug(true);
email.setHostName("smtp.gmail.com");
email.getMailSession().getProperties().put("mail.smtps.auth", "true");
email.getMailSession().getProperties().put("mail.debug", "true");
email.getMailSession().getProperties().put("mail.smtps.port", "587");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
email.setFrom("........@gmail.com", "SenderName");
email.setSubject("TestMail");
email.setMsg("This is a test mail?");
email.addTo("xxxx@gmail.com", "ToName");
email.setTLS(true);
email.send();

【讨论】:

  • 像魅力一样工作,谢谢!
  • 这段代码对我有用(只需要删除 email.setTLS(true))- 谢谢 jitter
【解决方案2】:

你不需要tell Commons Email that you're sending a TLS email:

email.setTLS(true);

在您调用 email.send() 之前?

我不确定这是否能解决您的问题,因为我不确定您是否在连接到 smtp.gmail.com:465 或成功发送到它时遇到问题(错误消息/异常是正如你所提出的那样模棱两可),但据我所知,这绝对是缺失的东西。

【讨论】:

  • 自 1.3 起已弃用,请改用 setStartTLSEnabled()
  • 使用端口 587 或者设置这个:email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
【解决方案3】:

Commons Email 用户指南有一个使用 SSL 的 Gmail 示例。

https://commons.apache.org/proper/commons-email/userguide.html

SSL/TLS(端口 465) -> email.setSSLOnConnect(true);

Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

STARTTLS(端口 587) -> email.setStartTLSEnabled(true);

Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setStartTLSEnabled(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

【讨论】:

  • 我收到类 javax.mail.MessagingException: 530 5.7.0 必须先发出 STARTTLS 命令。 g66sm11757390ywh.8 - gsmtp 异常。但是,我更新了与您的代码相同的代码。
【解决方案4】:

如果您想使用 TLS,请使用端口 587 和 starttls:

email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
// or call this:
email.setStartTLSEnabled().
// and set the port to 587
email.setSmtpPort(smtpPort);

如果您想使用端口 465,请不要设置为 true,而是调用以下函数:

email.setSSLOnConnect(true);
email.setSslSmptPort("465");
// or can set below but remember it is not "smtps" it is "smtp" and port nmber should be 465.

email.getMailSession().getProperties().put("mail.smtp.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.port", "465");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
email.getMailSession().getProperties().put("mail.smtp.socketFactory.fallback", "false");

【讨论】:

    【解决方案5】:

    别忘了启用谷歌Less secure app access

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多