Sending mail from your computer using java, what do you need?

I have a windows 7 system with no smtp servers installed, only java. I want to send mail from my program via gmail. I have written a java program to google connect via smtp and have telnet option enabled, but I am getting below error. I've tried using ports 465 and 587, but no change.


Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: not connect to SMTP host:, port: 587;
 nested exception is: PKIX path validation failed: timestamp check failed
at mail1.SendMailSSL.main(



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 SendMailSSL {

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("", "");
        props.put("mail.smtp.socketFactory.port", "465");

        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("", "");

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");

       Session session = Session.getDefaultInstance(props,newjavax.mail.Authenticator()
           protected PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication("","mypassword");                  
       } );

       try {
           Message message = new MimeMessage(session);
           message.setFrom(new InternetAddress(""));
           message.setRecipient(Message.RecipientType.TO, new InternetAddress(
           message.setSubject("Testing Subject");
           message.setText("Dear Mail Crawler,"
                    + "\n\n No spam to my email, please!");

        } catch (MessagingException e) {
           throw new RuntimeException(e);


Can anyone tell me what I need to do to send an email from my personal system? Please tell me step by step and include something like software installation because I have to do it for my project. Your answer is greatly appreciated. Thank you in advance.


source to share

2 answers

Two years ago I had the same problem, but I don't remember everything that helped me.

  • I have used JavaMail.
  • In some cases you need to use 587, in some cases you need to use port 465.
  • You have to allow SMTP (or POP?) In your gmail settings. You must use authorization.

I think these posts will be helpful:



Required jar files:

  • activation.jar

Steps to send mail:

  • Use google smtp server port 465

  • Enable telnet on Windows 7:

    Control Panel > Programs > Turn Windows features on or off

    Check "Telnet Server" and "Telnet Client"

  • Make sure your system has an accurate date

  • And, of course, an internet connection is required.


All Articles