我正在使用Eclipse Java代码中的Selenium WebDriver开发自动化测试。并能够使用gmail帐户发送带有Commons-Email的简单电子邮件,并且在465和587端口均正常工作(我已打开Less Secure App)。以下代码可以完美工作。
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
答案 0 :(得分:0)
只需添加:
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>
希望对您有帮助:)