Email code is required for anything when we want to send someone our content be it our test case results or anything of the sorts.
This can be integrated into a hybrid framework as well.
The JavaMail is an API that is used to compose,write and read electronic messages(emails).
The JavaMail API provides protocol-independent and platform-independent framework for sending and receiving mails.
The javax.mail and javax.mail.activation packages contain the core classes of the JavaMail API.
The JavaMail facility can be applied to many events. It can be used at the time of registering the user (sending notification, such as thanks for your interest to my site), forgot password (sending a password to the user’s email id), sending notifications for important updates etc. So there can be various usage of the Java mail API.
For sending the email using JavaMail API, you need to load the two jar files:
Sending email with attachment using JavaMail API:
There are total 7 steps for sending attachments with email. They are:
Get the session object.
Compose a message.
Create ‘MimeBodyPart’ object and set your message text.
Create new ‘MimeBodyPart’ object and set a ‘DataHandler’ object to this object.
Create ‘Multipart’ object and add ‘MimeBodyPart’ objects to this object.
Set the ‘multipart’ object to the message object.
Send message.
Package com.javabykiran.email;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
class SendAttachment{
public static void main(String [] args){
String to="javabykiran@gmail.com"; //change accordingly
final String user="kiran1999@gmail.com "; //change accordingly
final String password="xxxxx"; //change accordingly
//1) get the session object
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "mail.javatpoint.com");
properties.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthenticationgetPasswordAuthentication(){
return new PasswordAuthentication(user,password);
}
});
//2) compose message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, newInternetAddress(to));
message.setSubject("Message Alert");
//3) create MimeBodyPart object and set your message text
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
//4) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = "SendAttachment.java"; //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 );
//7) send message
Transport.send(message);
System.out.println("message sent.");
}catch (MessagingException ex){
ex.printStackTrace();
}
}
}