I am developing automated test using Selenium WebDriver in Eclipse Java code. And able to send simple email with Commons-Email using gmail account and its working fine on both port 465, 587 (I have turned on Less Secure App). following code works perfectly.
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");
}
}
However when I try to send attachment then getting errors, I have tried both ports 465, 587, I have also tried to change attachment type and path. following is the code and error messages:
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
When port 587 used then getting following errors
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
Just add :
email.setTLS(true);
in code :
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");
}
}
I am using maven dependency :
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.2</version>
</dependency>
Hope that helps you:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.