Wednesday, December 25, 2013

ADF - Java Mail with attachments

First create a mail session in weblogic server. In Weblogic server under "Services" you can find Mail sessions. Create a new Mail Session. Give following information:
Name - test
JNDI Name - mail/test
Java Mail Properties
mail.smtp.password=[password]
mail.debug=false
mail.smtp.user=[user name]
mail.from=xx-do-not-reply@abc.com
mail.smtp.host=[host name or IP of exchange server]
mail.smtp.auth=false
mail.smtp.port=25
mail.verbose=false
mail.disable=false
mail.transport.protocol=smtp


The following java method can be called to send mail with attachments

destination - array of email addresses to which mail has to be sent
subject - subject of mail
content - body of mail
data - list containing byte[] of the files to be attached in the email
fileNames - names of the attachement
contentTypes - content types of the attachments



 public static void sendMail(String[] destination, String subject,
                              String content, ArrayList<byte[]> data,
                              ArrayList<String> fileNames,
                              ArrayList<String> contentTypes)
    throws Exception
  {
    InitialContext ic = new InitialContext();
//Use JNDI name provided in Mail Session Creation
    Session session = (Session) ic.lookup("mail/test");
    Properties props = session.getProperties();
    ADFLogger _logger = ADFLogger.createADFLogger(AdpcEmailUtil.class);

    String mailhost = props.getProperty("mail.smtp.host");
    String user = props.getProperty("mail.smtp.user");
    String password = props.getProperty("mail.smtp.password");
    String protocol = props.getProperty("mail.transport.protocol");
    String authorization = props.getProperty("mail.smtp.auth");
    String mailDisabled = props.getProperty("mail.disable");
    String verboseProp = props.getProperty("mail.verbose");
    String debugProp = props.getProperty("mail.debug");
    String from = props.getProperty("mail.from");


    String mailer = "smtpsend";

    if (debugProp.equals("true"))
      session.setDebug(true);
    else
      session.setDebug(false);

    Message msg = new MimeMessage(session);

     // Setup container
    MimeMultipart lMultipartRoot = new MimeMultipart("related");
    MimeBodyPart lContentRoot = new MimeBodyPart();
    MimeMultipart lMultipartAlternate = new MimeMultipart("alternative");

    // HTML part
    BodyPart lMessageBodyPart = new MimeBodyPart();
    lMessageBodyPart.setContent(content, "text/html");
    lMultipartAlternate.addBodyPart(lMessageBodyPart);

    // Pull together alternate content
    lContentRoot.setContent(lMultipartAlternate);
    lMultipartRoot.addBodyPart(lContentRoot);

  
    // Attach files
    int size = 0;
    if (data != null)
    {
      size = data.size();
    }
    for (int i = 0; i < size; i++)
    {
      if (data.get(i) == null)
        continue;
      BodyPart reportBodyPart = new MimeBodyPart();
      DataSource reportDataSource =
        new ByteArrayDataSource(data.get(i), contentTypes.get(i));
      reportBodyPart.setDataHandler(new DataHandler(reportDataSource));
      lMultipartRoot.addBodyPart(reportBodyPart);
      reportBodyPart.setFileName(fileNames.get(i));
      msg.setContent(lMultipartRoot);
    }
    msg.setFrom();

    InternetAddress[] addressTo = new InternetAddress[destination.length];
    for (int i = 0; i < destination.length; i++)
    {
      addressTo[i] = new InternetAddress(destination[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);
    msg.setSubject(subject);
    msg.setHeader("X-Mailer", mailer);
    msg.setSentDate(new Date());
    _logger.warning("Metadata set.");
    SMTPTransport t = (SMTPTransport) session.getTransport(protocol);
    _logger.warning("Gettting Transport.");

    try
    {
      t.connect(mailhost, user, password);
      t.sendMessage(msg, msg.getAllRecipients());
    }
    finally
    {
      t.close();
    }
  }


Hope this is helpful.
---deepak

1 comment:

  1. I can not find Services in weblogic 10.3 in ADF 11.1.1.1.7

    ReplyDelete