挂在 send() 上的共享电子邮件示例

发布于 2024-12-07 02:47:21 字数 1823 浏览 12 评论 0原文

我正在尝试让 此示例 使 Apache Commons 电子邮件库正常工作。这是我的代码:

    SimpleEmail email = new SimpleEmail();      
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(465);
    email.setAuthenticator(new DefaultAuthenticator("[email protected]", "password"));     
    email.setTLS(true); 
    try {
        email.setFrom("[email protected]");
        email.setSubject("TestMail");
        email.setMsg("This is a test mail ... :-)");
        email.addTo("[email protected]");
        System.out.println("Sending...");
        email.send();
        System.out.println("Email sent!");

    } catch (Exception e) {
        System.out.println("Email not sent!");
        e.printStackTrace();
    }

正如您所看到的,它与示例基本没有变化,只是我必须使用端口 465 而不是 587,因为 587 会导致 Connection returned 异常(基于 这个问题)。现在这段代码挂在 email.send() 行上。我得到的唯一输出是:

Sending...

但没有抛出异常。我需要在防火墙中打开端口吗? (我可能无法做到这一点,因为我正试图在工作中做到这一点)。谢谢!

编辑

很长一段时间后我得到了这个异常:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
...
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

I'm trying to get this example for the Apache Commons email library to work. Here is my code:

    SimpleEmail email = new SimpleEmail();      
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(465);
    email.setAuthenticator(new DefaultAuthenticator("[email protected]", "password"));     
    email.setTLS(true); 
    try {
        email.setFrom("[email protected]");
        email.setSubject("TestMail");
        email.setMsg("This is a test mail ... :-)");
        email.addTo("[email protected]");
        System.out.println("Sending...");
        email.send();
        System.out.println("Email sent!");

    } catch (Exception e) {
        System.out.println("Email not sent!");
        e.printStackTrace();
    }

As you can see it's basically unchanged from the example, except I have to use port 465 instead of 587 because 587 causes a Connection refused exception (based on this question). Now this code is hanging on the email.send() line. The only output I get is:

Sending...

But no exceptions are thrown. Do I need to open a port in my firewall? (I might not be able to do that as I'm trying to do this from work). Thanks!

Edit

After a long time I get this exception:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
...
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2024-12-14 02:47:21

根据您的编辑和对我的评论的回答,您不应该在 Java 代码中查找问题,而应该在防火墙或网络配置中查找问题。

Based on your edits and answer to my comment, you shouldn't look for your problems in Java code, but in the firewall or your network configuration.

嗼ふ静 2024-12-14 02:47:21

您需要设置以下内容(因为您使用的是 SSL)

Properties props = new Properties();
props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.timeout" , "10000");
    props.put("mail.smtp.connectiontimeout" , "10000");
    props.put("mail.smtp.socketFactory.port", 465);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

You need to set the following (because you are using SSL)

Properties props = new Properties();
props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.timeout" , "10000");
    props.put("mail.smtp.connectiontimeout" , "10000");
    props.put("mail.smtp.socketFactory.port", 465);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文