Gmail Integration with Oracle ADF using Java Mail API

Sharing is Caring

Gmail is a free secure webmail provided by Google, can be accessed by using POP3 or IMAP4 protocol
we have the option to integrate Gmail with Java using Java Mail API and I have tried using same for Gmail Integration with Oracle ADF.
I have developed an application that can send mail (with Attachments) using Gmail in Oracle ADF,
using bounded TaskFlow.

  • First, In bounded task flow, a login page is created, and a page to send mail is created
  • Now you can get values from the page and use in bean- So I am not going to write these things
  • Integration point starts when you get mail server properties in managed Bean Code
  • You have to use 2 JAR files in order to use Java Mail API
  1.mail.jar 2. Activation.jarDownload

Properties emailProperties;

public void setMailServerProperties() {
String emailPort = “587”; //gmail’s smtp port
emailProperties = System.getProperties();
emailProperties.put(“mail.smtp.port”, emailPort);
emailProperties.put(“mail.smtp.auth”, “true”);
emailProperties.put(“mail.smtp.starttls.enable”, “true”);

}

  • Now you have to create your message (With or Without attachments)

public void createEmailMessageWidtAtchmnt() throws AddressException, MessagingException {
toWhom = toBind.getValue().toString();
subject = subjectBind.getValue().toString();
messagae = messageBind.getValue().toString();
String[] toEmails = { toWhom };

String emailSubject = subject;
String emailBody = messagae;

mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);

for (int i = 0; i < toEmails.length; i++) {
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
}

emailMessage.setSubject(emailSubject);

//1) create MimeBodyPart object and set your message content
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(emailBody);

emailMessage.setContent(emailBody, “text/html”); //for a html email

}

  • Here I am getting values from page component binding so don’t get confused by this- subjectBind, messageBind are component binding of page componenets.
  • Code snippet to create mail with attachments, you have to do little change in createEmailMessageWidtAtchmnt() to add files in mail. This code browses files from D drive of the system .

//1) create MimeBodyPart object and set your message content
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(emailBody);

//2) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();

String filename = “D://” + file_name; //change accordingly
System.out.println(“Exact path—>” + filename);
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);

//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);

//6) set the multiplart object to the message object
emailMessage.setContent(multipart);

  • To add files in mail in ADF you have to use af:inputFile component, and create a ValueChangeListener on component that get fileName when you browse any file from D drive of your system

private String file_name;

public void uploadedFileAttachmentVCE(ValueChangeEvent valueChangeEvent) {
UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue();
file_name = file.getFilename();
}

  • From login page you will get login emailId and password, that is set in managed bean code to authenticate user- and send mail

public void sendEmail() {

String emailHost = “smtp.gmail.com”;
String fromUser = emailId; //just the id alone without @gmail.com
String fromUserEmailPassword = pwd;
Transport transport = null;
try {
transport = mailSession.getTransport(“smtp”);
} catch (NoSuchProviderException e) {
System.out.println(“No such Provider Exception”);
}
try {
transport.connect(emailHost, fromUser, fromUserEmailPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
StringBuilder msgEmail = new StringBuilder(“Email Sent”);
FacesMessage msg = new FacesMessage(msgEmail.toString());
msg.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(“No Network Error !”, msg);
System.out.println(“Email sent successfully.”);
} catch (MessagingException e) {
System.out.println(“somthing went wrong–>Messaging Exception”);

}

}

Download Complete Application and map these code to bean-Download Sample ADF Application

Cheers 🙂 Happy Learning

Related Posts

2 thoughts on “Gmail Integration with Oracle ADF using Java Mail API”

Leave a Reply

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