在Java中,Commons电子邮件不使用附件

在Java中,Commons电子邮件不使用附件,java,eclipse,email,attachment,Java,Eclipse,Email,Attachment,我正在Eclipse Java代码中使用SeleniumWebDriver开发自动测试。并且能够使用gmail帐户发送简单的Commons电子邮件,并且在端口465587上工作正常(我已经打开了不太安全的应用程序)。下面的代码工作得很好 import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.Email; import org.apache.commons.mail.EmailExce

我正在Eclipse Java代码中使用SeleniumWebDriver开发自动测试。并且能够使用gmail帐户发送简单的Commons电子邮件,并且在端口465587上工作正常(我已经打开了不太安全的应用程序)。下面的代码工作得很好

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

使用端口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
只需添加:

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>

org.apache.commons
公用电子邮件
1.2
希望对您有所帮助:)

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