Create a class Lib
1| Create a class library in your package.
2| Just copy & paste the below code; Do few edits that are highlighted in ORANGE
package packagename;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMailSSL {
public static void execute(String reportFileName) throws Exception {
final String username = "yourusername@gmail.com";
final String password = "yourpassword";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("yourusername@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("yourrecepientusername@gmail.com"));
message.setSubject("Reports Availalbe!");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String file = "C:\\Users\\prashanth_sams\\workspace\\jxl\\test-output\\";
String fileName = reportFileName;
DataSource source = new FileDataSource(file + fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
System.out.println("Sending");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Test Class
Add a small snippet [SendMailSSL.execute("report.html");] after your test execution.
@AfterTest
public void tearDown() throws Exception {
driver.quit();
Thread.sleep(3000);
SendMailSSL.execute("emailable-report.html");
}
Is it required to import any jar files to execute this script?
ReplyDeleteSending
ReplyDeleteFAILED CONFIGURATION: @AfterTest tearDown
java.lang.RuntimeException: javax.mail.SendFailedException: Sending failed;
nested exception is:
Just what I was looking for.
ReplyDeleteMany thanks
Hi, the testng report file is not getting generated even after driver.quit(). any way to wait for report like emailable-report.html to get generated first before moving ahead to send them ??
ReplyDeletethanks,
Rahul
Cannot inject @Test annotated Method [execute] with [class java.lang.String].
ReplyDeletegetting the above error while executing the code
i am getting the following issue Must issue a STARTTLS command first. r138sm24776114pgr.12 - gsmtp
ReplyDeleteon execution of the above code .Please Help
Am getting the below errors : not executing
ReplyDeleteException in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
at javax.mail.Session.initLogger(Session.java:230)
at javax.mail.Session.(Session.java:214)
at javax.mail.Session.getInstance(Session.java:251)
at com.mercury.tours.testcases.SendEmailSSL.execute(SendEmailSSL.java:32)
at com.mercury.tours.testcases.SendEmailSSL.main(SendEmailSSL.java:73)
Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more
This will be sent as an attachment. Is there a way that the css and html are embedded properly in the body of the email
ReplyDelete