here i have two methods:
1/getMailAdresse():it returns the list of mail adresses of my iterator in the jspx page.
2/sendMail():It sends mails from my personel mail adress "ol***" to the list of mail that i got it from the method getMailAdresse().
here is the bean code:
public class JavaEmailBean {
public JavaEmailBean() {
}
Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;
String toWhom;
String messagae;
String subject;
String hostName = "smtp.gmail.com";
String FromUser = "ol***"; //just the id alone without @gmail.com
String pwd = "******";
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");
}
public ArrayList<String> getMailAdresse() {
ArrayList <String> usrMails = new ArrayList();
DCIteratorBinding dcIterDoc =ADFUtils.findIterator("mailUser1Iterator");
Long c=dcIterDoc.getViewObject().getEstimatedRowCount();
for (int i = 0; i < dcIterDoc.getViewObject().getEstimatedRowCount(); i++)
{
Row r = dcIterDoc.getRowAtRangeIndex(i);
String mail = (String)r.getAttribute("Mail");
System.out.println("mail= "+mail);
usrMails.add(mail);
} return usrMails;
}
public String sendMail() {
// Setting Properties
String msg="The author x has added a new document and has shared it with you.";
String subject="new Document added.";
ArrayList<String> ToUser=this.getMailAdresse();
Properties emailProperties = new Properties();
emailProperties.put("mail.smtp.host", hostName);
emailProperties.put("mail.smtp.auth", "true");
emailProperties.put("mail.smtp.starttls.enable", "true");
final String user = "ol***@gmail.com"; //change accordingly
final String password = "*****"; //change accordingly
Session session = Session.getInstance(emailProperties, new javax.mail.Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
//1) create MimeBodyPart object and set your message content
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(user));
for (String email : ToUser) {
System.out.println("Mail Id is-" + email);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
}
message.setSubject(subject);
BodyPart messageBody = new MimeBodyPart();
messageBody.setContent(msg, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBody);
message.setContent(msg, "text/html"); //for a html email
} catch (MessagingException e) {
}
Transport transport = null;
try {
transport = session.getTransport("smtp");
} catch (NoSuchProviderException e) {
System.out.println("No such Provider Exception");
}
try {
transport.connect(hostName, FromUser, pwd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Email sent successfully.");
return "Y";
} catch (MessagingException e) {
System.out.println("Messaging Exception" +e);
return "N";
}
}
}
when i run the application i got this error:
mail= ol***@gmail.com
Mail Id is=ol***@gmail.com
Messaging Exceptionjavax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
<oracle.adf.view> <NavigationPaneRenderer> <_renderContent> <Warning: There are no items to render for this level>
***Moderator action (Timo): removed valid looking email address! User please don't post private information here this public forum!***