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.
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.
Hi prasad, your scenario is very helpful.. I have a requirement i.e bulk mailing to list of mails(available in database) how can I do this please help me and is it possible with java API? Awaiting reply...
ReplyDeleteYou can use the below method to send the bulk mail.
DeleteString[] toAdressList = null;
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.smtp.host", _host);
props.put("mail.smtp.user",_user);
props.put("mail.smtp.password",_pwrd);
props.put("mail.smtp.port",_port);
props.put("mail.smtp.auth", "true");
String toAddresses = emailid;
if (toAddresses.contains(",")) {
toAdressList = toAddresses.split(",");
} else {
toAdressList = new String[1];
toAdressList[0] = toAddresses;
}
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
InternetAddress[] toAddress = new InternetAddress[toAdressList.length];
BodyPart messageBodyPart = new MimeBodyPart();
try {
messageBodyPart.setText("This is message body");
} catch (MessagingException e) {
}
try {
// To get the array of addresses
for (int i = 0; i < toAdressList.length; i++) {
toAddress[i] = new InternetAddress(toAdressList[i]);
}
for (int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setFrom(new InternetAddress(_senduser));
// message.setSubject(subj, "UTF-8");
// message.setText(txt, "UTF-8");
message.setSubject(subj);
message.setContent(txt, "text/html");
// message.setText(txt, "text/html");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
Transport transport = null;
try {
transport = session.getTransport("smtp");
transport.connect(_host,
_user,
_pwrd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
Hi prasad, your scenario is very helpful.. I have a requirement i.e bulk mailing to list of mails(available in database) how can I do this please help me and is it possible with java API? Awaiting reply...
ReplyDeleteHi prasad, your scenario is very helpful.. I have a requirement i.e bulk mailing to list of mails(available in database) how can I do this please help me and is it possible with java API? Awaiting reply...
ReplyDeleteHi Prasad, Im getting null pointer exception, How to handle this?
ReplyDeletejavax.faces.el.EvaluationException: java.lang.NullPointerException
at org.apache.myfaces.trinidad.component.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:51)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at org.apache.myfaces.trinidad.component.UIXCommand.broadcast(UIXCommand.java:190)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._invokeApplication(LifecycleImpl.java:957)
at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(LifecycleImpl.java:427)
at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:207)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at oracle.adf.model.servlet.ADFBindingFilter.doFilter(ADFBindingFilter.java:205)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at oracle.adfinternal.view.faces.webapp.rich.RegistrationFilter.doFilter(RegistrationFilter.java:128)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:446)
at oracle.adfinternal.view.faces.activedata.AdsFilter.doFilter(AdsFilter.java:60)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:446)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilterImpl(TrinidadFilterImpl.java:271)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilter(TrinidadFilterImpl.java:177)
at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFilter.java:92)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:119)
at java.security.AccessController.doPrivileged(Native Method)
at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:324)
at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:460)
at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:103)
at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:171)
at oracle.security.jps.ee.http.JpsFi
at weblogic.servlet.internal.WebAppServletContext$ServletInvocatio
Actually I placed " return " statement on below...
Deletewithout return statment it will not running..
help me if u available ASAP
This comment has been removed by the author.
ReplyDeleteHi prasad,
ReplyDeletei am getting this error
javax.faces.el.EvaluationException: java.lang.RuntimeException: javax.mail.MessagingException: Can't send command to SMTP host;
Did u test ur SMTP localhost?
DeleteThanks! Helped me a lot :)
ReplyDelete