Java mail sending probelm?
I try to make a java coding to send mail. java code as follow
/*but it creates exception error, and i also find out solution for this error in google, nobody can properly clear this error. error as follows:
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sendemail;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.security.Security;
import javax.mail.Message.RecipientType;
/**
*
* @author mohamed
*/
public class SendEmail {
public static void main(String[] args) {
String from = "#-Link-Snipped-#";
String to = "#-Link-Snipped-#";
String subject = "Test Message";
String message = "This is A test message sent via Gmail ";
SendMail sendMail = new SendMail(from, to, subject, message);
sendMail.send();
}
}
class SendMail {
private String from;
private String to;
private String subject;
private String text;
public SendMail(String from, String to, String subject, String text)
{this.from = from;
this.to = to;
this.subject = subject;
this.text = text;}
public void send(){
String host = "smtp.gmail.com";
String userid = "#-Link-Snipped-#";
String password = "mypassword";
try
{
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.setProperty("mail.transport.protocol", "smtps");
props.put("mail.smtp.user", userid);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "465");
props.put("mail.smtps.auth", "true");
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e) {
e.printStackTrace();
}
message.setFrom(fromAddress);
message.setRecipient(RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(text);
//SMTPSSLTransport transport =(SMTPSSLTransport)session.getTransport("smtps");
Transport transport = session.getTransport("smtps");
transport.connect(host, userid, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
--------------------Configuration: <Default>--------------------
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
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
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at sendemail.SendMail.send(SendEmail.java:82)
at sendemail.SendEmail.main(SendEmail.java:29)
Caused by: 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
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1540)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:203)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:197)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:994)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:142)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:532)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:470)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:837)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1049)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1076)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1060)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 5 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:302)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:205)
at sun.security.validator.Validator.validate(Validator.java:235)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:147)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:230)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:270)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:973)
... 15 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:191)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:255)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:297)
... 21 more
Process completed.
0