The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications- JavaMail API
This post is about sending eMail and attachments from any SMTP (Simple Mail Transfer Protocol) as Gmail or any other server (mail.awasthiashish.com)
You can read my previous post about mail integration with ADF that was specifically about using Gmail Server
Gmail Integration with Oracle ADF using Java Mail API
So all basic configuration is described in previous post , now in this post i am only writing a java method to send mail and attachments
Don't forget to download these 2 jar files
1.mail.jar 2. Activation.jar- Download
Add both jar files to project library path and then use this method
Helper method (JavaCode) to send simple eMail and eMail with attachment-
/**Method to send mail from any SMTP server using JavaMail API
* Provide Correct Parameters
* @return
* @param msg- Email Message Body
* @parsm subject- Subject of Email
* @param FromUser- Email Id of Sender
* @param ToUser- Email Id of Reciever
* @param pwd- Password of sender's email address
* @param hostName- Host Name of Mail server (smtp.gmail.com)
* @param isAnyAtchmnt- "Y" for yes there is an attachement and "N" for no attachment
* @param fileNamePath- abolute path of file on server if there is any attachement
*/
public String sendMail(String msg, String subject, String FromUser, String ToUser, String pwd, String hostName,
String isAnyAtchmnt, String fileNameNPath){
// Setting Properties
Properties emailProperties =new Properties();
emailProperties.put("mail.smtp.host", hostName);
emailProperties.put("mail.smtp.auth","true");
emailProperties.put("mail.smtp.starttls.enable","true");
//Login Credentials
final String user = FromUser;//change accordingly
final String password = pwd;//change accordingly
//Authenticating...
Session session = Session.getInstance(emailProperties,new javax.mail.Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
returnnewPasswordAuthentication(user, password);
}
});
//1) create MimeBodyPart object and set your message content
MimeMessage message =new MimeMessage(session);
try{
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(ToUser));
message.setSubject(subject);
BodyPart messageBody =new MimeBodyPart();
messageBody.setText(msg);
// If there is any attachment to send
if("Y".equalsIgnoreCase(isAnyAtchmnt)){
//2) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 =new MimeBodyPart();
System.out.println("Exact path--->"+ fileNameNPath);
DataSource source =new FileDataSource(fileNameNPath);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(fileNameNPath);
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart =new MimeMultipart();
multipart.addBodyPart(messageBody);
multipart.addBodyPart(messageBodyPart2);
//6) set the multiplart object to the message object
message.setContent(multipart);
}
//If there is plain eMail- No Attachment
else{
message.setContent(msg,"text/html");//for a html email
}
}catch(MessagingException e){
}
Transport transport =null;
try{
transport = session.getTransport("smtp");
}catch(NoSuchProviderException e){
System.out.println("No such Provider Exception");
}
try{
transport.connect(hostName, FromUser, pwd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Email sent successfully.");
return"Y";
}catch(MessagingException e){
System.out.println("Messaging Exception"+ e);
return"N";
}
}
Call this method in your ADF Application to send simple mail or mail with attachment, this is just like plug n play , provide correct parameters and use
Thanks :) Happy Learning