【问题标题】:Commons-Email not working with attachment in JavaCommons-Email 不适用于 Java 中的附件
【发布时间】:2018-07-11 22:15:37
【问题描述】:

我正在 Eclipse Java 代码中使用 Selenium WebDriver 开发自动化测试。并且能够使用 gmail 帐户通过 Commons-Email 发送简单的电子邮件,并且在端口 465、587 上都可以正常工作(我已经打开了不太安全的应用程序)。以下代码完美运行。

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SendEmail {

public static void main(String[] args) throws EmailException {
    System.out.println("sending email");
    Email email = new SimpleEmail();
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(587);
    email.setAuthenticator(new DefaultAuthenticator("myaccount@gmail.com", 
"mypassword"));
    email.setSSLOnConnect(true);
    email.setFrom("myaccount@gmail.com");
    email.setSubject("TestNG Reports");
    email.setMsg("Attahced is the TestNG tests execution report");
    email.addTo("rec1@gmail.com","rec2@gmail.com");
    email.send();
    System.out.println("email sent");
 }
}

但是,当我尝试发送附件然后出现错误时,我尝试了 465、587 端口,我还尝试更改附件类型和路径。以下是代码和错误信息:

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class SendEmail {

public static void main(String[] args) throws EmailException {
     System.out.println("sending email");
     EmailAttachment attachment = new EmailAttachment();
     attachment.setPath("D:\\failed.png");
     attachment.setDisposition(EmailAttachment.ATTACHMENT);
     attachment.setDescription("TestNG Report");
     attachment.setName("QA");

     MultiPartEmail email = new MultiPartEmail();
     email.setHostName("smtp.gmail.com");
     email.setSmtpPort(465);
     email.setAuthenticator(new DefaultAuthenticator("myaccount@gmail.com", 
"mypassword"));
     email.addTo("rec1@gmail.com","rec2@gmail.com");
     email.setFrom("myaccount@gmail.com");
     email.setSubject("Test NG Execution Report");
     email.setMsg("Please find the attached Test NG test cases execution 
report");
     email.attach(attachment);
     email.send();
     System.out.println("email sent");
}

}

Exception in thread "main" org.apache.commons.mail.EmailException: Sending 
the email to the following server failed : smtp.gmail.com:465
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1469)
at org.apache.commons.mail.Email.send(Email.java:1496)
at setup.SendEmail.main(SendEmail.java:26)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: 
smtp.gmail.com, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1949)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1459)
... 2 more

使用 587 端口时出现以下错误

Exception in thread "main" org.apache.commons.mail.EmailException: Sending 
the email to the following server failed : smtp.gmail.com:587
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1469)
at org.apache.commons.mail.Email.send(Email.java:1496)
at setup.SendEmail.main(SendEmail.java:26)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a 
STARTTLS command first. o4-v6sm35725484wra.3 - gsmtp

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1459)
... 2 more

【问题讨论】:

    标签: java eclipse email attachment


    【解决方案1】:

    只需添加:

    email.setTLS(true);
    

    在代码中:

    package com.demo.utils;
    
    import org.apache.commons.mail.DefaultAuthenticator;
    import org.apache.commons.mail.EmailAttachment;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.MultiPartEmail;
    
    public class SendEmail {
    
        public static void main(String[] args) throws EmailException {
            System.out.println("sending email");
            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath("C:\\Users\\12345\\Desktop\\Reebok\\BAIT-YG-Reebok-Classic-Nylon.jpg");
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription("TestNG Report");
            attachment.setName("QA");
    
            MultiPartEmail email = new MultiPartEmail();
            email.setHostName("smtp.gmail.com");
            email.setSmtpPort(587);
            email.setTLS(true);
            email.setAuthenticator(new DefaultAuthenticator("myaccount@gmail.com", "myPassword"));
            email.setFrom("myaccount@gmail.com");
    
            email.addTo("rec1@gmail.com","rec2@gmail.com");
            email.setSubject("Test NG Execution Report");
            email.attach(attachment);
            email.send();
            System.out.println("email sent");
        }
    }
    

    我正在使用 maven 依赖:

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-email</artifactId>
                <version>1.2</version>
            </dependency>
    

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 2015-07-19
      • 2012-12-30
      • 2010-12-09
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 2018-05-25
      • 2018-04-30
      • 1970-01-01
      相关资源
      最近更新 更多