javax.mail.MessagingException: Could not convert socket to TLS; 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
my code:
public class SendMail implements ActionListener
{
public void actionPerformed(ActionEvent e1)
{
String TO = textTo.getText();
Component Mail=sendMail;
if((TO.trim().length() < 1))
{
JOptionPane.showMessageDialog(Mail,
"Please enter some address to send mail",
"No Address to send mail", JOptionPane.ERROR_MESSAGE);
}
else{
String host = "smtp.gmail.com";//host name
String from = "[email protected]";//sender id
String to = textTo.getText();//reciever id
String pass = "password";//sender's password
String fileAttachment = textAttFile.getText();//file name for attachment
//system properties
Properties prop = System.getProperties();
// Setup mail server properties
prop.put("mail.smtp.gmail", host);
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", host);
prop.put("mail.smtp.user", from);
prop.put("mail.smtp.password", pass);
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
//session
Session session = Session.getInstance(prop, null);
// Define message
MimeMessage message = new MimeMessage(session);
try
{
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(textSub.getText());
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
//message body
messageBodyPart.setText(body.getText());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
//send message to reciever
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
JOptionPane.showMessageDialog(sendMail,
"Mail Sent Successfully.",
"Mail Sent.", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}}
}