Today we will share code how to send any type of selenium report in mail using java. For sending the email using JavaMail API, you need to load the two jar files:
package SeleniumNew;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail
{
public static void main(String[] args)
{
String host="smtp.gmail.com";
final String user="ashishxx@gmail.com";//change accordingly
final String password="XXX";//change accordingly
/*String[] to={"AshishXX@gmail.com","Ashishxxx@gmail.com"};
String[] cc={};
String[] bcc={};
*/
String to="ashishxx@gmail.com";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
/*for(int i=0;i<to.length;i++){
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
}
for(int i=0;i<cc.length;i++){
msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
}
for(int i=0;i<bcc.length;i++){
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
msg.saveChanges();*/
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("ClubMahindra Automation Report");
// message.setText("Please find the attached Clubmahindra Automation Report");
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("Please find the attached Clubmahindra Automation Report");
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
//File Path
String Report="Report10.html";
String filename =System.getProperty("user.dir")+"//Report//"+Report;
//"E:\\SeleniumHeadless\\Report\\Report10.html";//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
//6) set the multiplart object to the message object
message.setContent(multipart );
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
} catch (MessagingException e) {e.printStackTrace();}
}
}
- mail.jar
- activation.jar
package SeleniumNew;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail
{
public static void main(String[] args)
{
String host="smtp.gmail.com";
final String user="ashishxx@gmail.com";//change accordingly
final String password="XXX";//change accordingly
/*String[] to={"AshishXX@gmail.com","Ashishxxx@gmail.com"};
String[] cc={};
String[] bcc={};
*/
String to="ashishxx@gmail.com";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable","true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
//Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
/*for(int i=0;i<to.length;i++){
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
}
for(int i=0;i<cc.length;i++){
msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
}
for(int i=0;i<bcc.length;i++){
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
msg.saveChanges();*/
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("ClubMahindra Automation Report");
// message.setText("Please find the attached Clubmahindra Automation Report");
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("Please find the attached Clubmahindra Automation Report");
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
//File Path
String Report="Report10.html";
String filename =System.getProperty("user.dir")+"//Report//"+Report;
//"E:\\SeleniumHeadless\\Report\\Report10.html";//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
//6) set the multiplart object to the message object
message.setContent(multipart );
//send the message
Transport.send(message);
System.out.println("message sent successfully...");
} catch (MessagingException e) {e.printStackTrace();}
}
}
I am very impressed with your post because this post is very beneficial for me and provide new knowledge to me
ReplyDeleteDriver Automation Tool Crack
WavePad Sound Editor Crack
MovieMator Video Editor Pro Crack
Adobe Acrobat Pro Crack