Tuesday, 10 March 2015

ADF: Send Email from ADF Web Application

This post will describe how to send email from ADF Web application.

2 ways (which I know) to achieve this:
  1. Use the Apache Commons Email. [This can be used while deploying in GlassFish server. I haven't tried myself yet in Glassfish].
  2. Use the Javax Mail API's along with the MailSessions configuration in Weblogic Server.
Use Apache Commons Email.
 import org.apache.commons.mail.DefaultAuthenticator;  
 import org.apache.commons.mail.Email;  
 import org.apache.commons.mail.EmailException;  
 import org.apache.commons.mail.SimpleEmail;  
 public static void sendEmail() throws EmailException  
 {  
      Email email = new SimpleEmail();  
      email.setHostName("smtp.gmail.com");  
      email.setSmtpPort(587);  
      email.setAuthenticator(new DefaultAuthenticator("XXXXX", "XXXXX"));  
      email.setSSLOnConnect(true);  
      email.setFrom("xxxx@xxxx.com");  
      email.setSubject("Subject of the Email!!");  
      email.setMsg("First Line in the Message Body.\nSecond Line.");  
      email.addTo("yyyy@yyyy.com");  
      email.send();  
      System.out.println("Email Sent successfully to yyyy@yyyy.com");  
 }  
Use JavaX Mail APIs.
  • Javax mail API's are available as part of the Jdev install (Jdev 12.1.3). This file is available under             %MIDDLEWARE_HOME%\Oracle_Home\oracle_common\modules\javax.mail_2.0.0.0_1-4-4.jar
  • Add this jar file to the Project Library.
  • You can configure the mailserver details in the Weblogic server. The Weblogic server configuration and the code to send email is detailed in the Lalit Kapoor's Sending E-Mail From an ADF Application Deployed on WebLogic Server
  • I have a created a word document with the configuration steps and the code, which you can download from here.
Feel free to point out if anything is missing/wrong in this blog.

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi Lucky,

      Where is this error coming ? When you try to compile or when you try to run ?

      Cheers
      AJ

      Delete
    2. The error appears in the source code a red line under the email.setAuthenticator(new DefaultAuthenticator("dildar.singh@oracle.com", "XXXXX")); when I hover over the line i get the message java.mail not allowed.[Java cloud service].

      Delete
    3. Did you add the jar file to your Project Library ? Please double check the import statements.

      Can you please attach you code here or send the file to anoopjohny@gmail.com. Which Jdev version are you using ? I can then try to replicate.

      Delete