Send eMail and attachment from any SMTP server using JavaMail in Oracle ADF

Sharing is Caring
The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications- JavaMail API. In this post, I am going to show that how to send email and attachment from any SMTP (Simple Mail Transfer Protocol) as Gmail or any other server (like
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 the 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.jarDownload

Add both jar files to project library path and then use this method

Send eMail and attachment

Necessary packages to import –

import java.util.ArrayList;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

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 (
 * @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, ArrayList<String> ToUser, String pwd,
                       String hostName, String isAnyAtchmnt, ArrayList<String> fileNameNPath) {
    // Setting Properties
    Properties emailProperties = new Properties();
    emailProperties.put("", 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
    Session session = Session.getInstance(emailProperties, new javax.mail.Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password);

    //1) create MimeBodyPart object and set your message content
    MimeMessage message = new MimeMessage(session);
    try {
        message.setFrom(new InternetAddress(user));
        for (String email : ToUser) {
            System.out.println("Mail Id is-" + email);
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

        BodyPart messageBody = new MimeBodyPart();

        messageBody.setContent(msg, "text/html");

        // If there is any attachment to send
        //5) create Multipart object and add MimeBodyPart objects to this object
        Multipart multipart = new MimeMultipart();

        if ("Y".equalsIgnoreCase(isAnyAtchmnt)) {
            //2) create new MimeBodyPart object and set DataHandler object to this object

            for (String path : fileNameNPath) {
                MimeBodyPart messageBodyPart2 = new MimeBodyPart();
                System.out.println("Exact path--->" + path);
                DataSource source = new FileDataSource(path);
                messageBodyPart2.setDataHandler(new DataHandler(source));
                // System.out.println("FileName is-"+path.substring(path.lastIndexOf("//")+1, path.length()));
                messageBodyPart2.setFileName(path.substring(path.lastIndexOf("//") + 2, path.length()));

            //6) set the multiplart object to the message object
        //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());

        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

Related Posts

0 thoughts on “Send eMail and attachment from any SMTP server using JavaMail in Oracle ADF”

Leave a Reply

Your email address will not be published. Required fields are marked *