ADF for beginners

Friday, 20 March 2015

Send an Email using Oracle ADF (JavaMail API)

This is a simple post to send an email in Oracle ADF.
I have used jdeveloper 11.1.1.7.0 and also used Gmail Gateway properties.
Below is the sample jsf page.


In the send button action i have coded the below method.

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendEmail {
    private String toemail;
    private String subj;
    private String txt;
    public SendEmail() {
    }

    public String sent_action() {
        sentEmailGmail();
        return null;
    }
   
    public void sentEmailGmail() {
        // Recipient's email ID needs to be mentioned.
        String to = this.getToemail(); //change accordingly
        // Sender's email ID needs to be mentioned
        String from = "asadrina@gmail.com"; //change accordingly
        final String username = "asadrina"; //change accordingly
        final String password = "@sadrina89"; //change accordingly
        // Assuming you are sending email through relay.jangosmtp.net
        String host = "smtp.gmail.com";
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "587");
        // Get the Session object.
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
        try {
            // Create a default MimeMessage object.
            Message message = new MimeMessage(session);
            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));
            // Set To: header field of the header.
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            // Set Subject: header field
            message.setSubject(this.getSubj());
            // Now set the actual message
            message.setText(this.getTxt());
            // Send message
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    public void setToemail(String toemail) {
        this.toemail = toemail;
    }

    public String getToemail() {
        return toemail;
    }

    public void setSubj(String subj) {
        this.subj = subj;
    }

    public String getSubj() {
        return subj;
    }

    public void setTxt(String txt) {
        this.txt = txt;
    }

    public String getTxt() {
        return txt;
    }
}


Now just run the page and enjoy sending the mail. No other configuration are required.