JSLEE Discussions
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Sending mail through SMTP
Forum Index » JAIN SLEE Discussions
Author Message
minus


Joined: 12/04/2012 21:41:53
Messages: 38
Offline

Hi

I am trying to send mail (to gmail using SMTP)from my Java code using JAINSLEE. Below is my code and the logs.
I am getting the exception "javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, p
ort: 465;"
Kindly guide me where i am wrong.

CODE:

System.out.println("Final loop entered");
boolean debug = true;

Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");

//Properties p = System.getProperties();
//p.setProperty("proxySet","true");
//p.setProperty("socksProxyHost","10.1.5.18");
//p.setProperty("socksProxyPort","6064");
System.setProperty("javax.net.ssl.keyStore", "keystore.jks");
System.setProperty("javax.net.ssl.trustStrore", "cacerts.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");



String trustStore = System.getProperty("javax.net.ssl.trustStore");
if (trustStore == null) {
System.out.println("javax.net.ssl.trustStore is not defined");
} else {
System.out.println("javax.net.ssl.trustStore = " + trustStore);
}




PasswordAuthentication pa= new PasswordAuthentication("tcscns@gmail.com", "tcs@123456");//Mail-id and password

System.out.println("Get the password"+pa.getPassword());
System.out.println("Get the user"+pa.getUserName());


Authenticator au=new Authenticator() {

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("tcscns@gmail.com", "tcs@123456");//Mail-id and password
}
};
System.out.println("Authentication mail"+au.toString());
Session session = Session.getInstance(props,au);

Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
session.setDebug(debug);

//Transport transport=null;
//try {
//transport = session.getTransport("smtp");
//try {
//transport.connect("mail.smtp.host", "tcscns@gmail.com","tcs@123456");
//} catch (MessagingException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
//}

//} catch (NoSuchProviderException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
//}
Message msg = new MimeMessage(session);
InternetAddress addressFrom = null; try {
addressFrom = new InternetAddress(emailFromAddress );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
msg.setFrom(addressFrom);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

InternetAddress addressTo = new InternetAddress();

try {
addressTo = new InternetAddress(sendTo);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
msg.setRecipient(Message.RecipientType.TO,addressTo);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



// Setting the Subject and Content Type
try {
msg.setSubject(emailSubjectTxt);
System.out.println("In set subject");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//System.out.println("Content:\n"+req.getContent());
msg.setContent("Hello", "text/plain");

} catch (Exception e) {
System.out.println("In set content");
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Transport.send(msg);
} catch (Exception e) {
System.out.println("In set send"+ e.toString());
// TODO Auto-generated catch block
e.printStackTrace();
}
}



LOGS:

DEBUG: JavaMail version 1.4.5
DEBUG: successfully loaded resource: /META-INF/javamail.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax
.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsyste
ms, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com
.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {smtps=javax.mail.Provider[TRANSPORT,smtps,
com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], smtp=javax.mail.Provi
der[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.address.map
DEBUG: setDebug: JavaMail version 1.4.5
In set subject
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
In set sendjavax.mail.MessagingException: Could not connect to SMTP host: smtp.g
mail.com, port: 465;

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, p
ort: 465;
nested exception is:
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc
eption: PKIX path building failed: sun.security.provider.certpath.SunCertPathBui
lderException: unable to find valid certification path to requested target

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc
eption: PKIX path building failed: sun.security.provider.certpath.SunCertPathBui
lderException: unable to find valid certification path to requested target
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:64
2)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.opencloud.slee.services.sip.b2bua.BackToBackUserAgentSbb.sendHttp
(BackToBackUserAgentSbb.java:600)
oliver


Joined: 29/10/2008 09:36:04
Messages: 680
Location: Cambridge, UK
Offline

minus wrote: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

You don't have a suitable certificate chain that validates the SSL certificate that Google's servers are providing.
Use the search engine of your choice, give it that exception message, and you will find tons of info on what you need to do to fix it.

(Also note that setting global system properties is probably not going to do what you want - they're global, and some of them are actually set by Rhino already, so you're probably setting them too late anyway; find another approach e.g. configuring SSL directly with keystores)

This message was edited 1 time. Last update was at 01/01/2013 01:21:31

[Email]
 
Forum Index » JAIN SLEE Discussions
Go to:   
Powered by JForum 2.1.8 © JForum Team