0

I am a newbie when it comes to java web programming. I tried doing a javamail code. here is my code

    import java.util.Properties;..

     public class SendEmail {
     public void sendEmail(final CodeToEmailCommand emailSender) {
      try {
        Properties props = System.getProperties();

        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.ssl.trust", "*");

        Session mailSession = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(emailSender.getUsername(), emailSender.getPassword());
            }
        });

        mailSession.setDebug(true);

        Message mailMessage = new MimeMessage(mailSession);

        mailMessage.setFrom(new InternetAddress(emailSender.getFromEmail()));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(emailSender.getToEmail()));
        mailMessage.setSubject(emailSender.getSubject());

        mailMessage.setContent(emailSender.getMessage(), "text/html");
        mailMessage.setSubject(emailSender.getSubject());
        Transport transport = mailSession.getTransport("smtp");
        transport.connect("smtp.gmail.com", emailSender.getUsername(), emailSender.getPassword());
        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    }
    }

But it kept giving me this exception

DEBUG: setDebug: JavaMail version 1.4.4
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: trying to connect to host "smtp.gmail.com", port 465, isSSL false
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.SunCertPathBuilderExc
eption: unable to find valid certification path to requested target

    ..

I tried changing ports. Enabling firewall (in cmd) but it is still not working. Pls. Help. I am running out of ideas

  • Did you went through SO questions? These links may help you https://stackoverflow.com/questions/46663/how-can-i-send-an-email-by-java-application-using-gmail-yahoo-or-hotmail https://stackoverflow.com/questions/15597616/sending-email-via-gmail-smtp-server-in-java Once you have written Java code, you will need to enable less trusted application to be allowed as mentioned in https://support.google.com/accounts/answer/6010255?hl=en – Kamal Singh Nov 25 '17 at 05:42
  • You clearly copied someone's old and incorrect code that's full of these [common JavaMail mistakes](https://javaee.github.io/javamail/FAQ#commonmistakes). See the JavaMail FAQ for [Gmail instructions](https://javaee.github.io/javamail/FAQ#gmail). – Bill Shannon Nov 28 '17 at 20:26
  • yeah, my mistake. I tried checking the documentation and learned my lesson. sorry about that :) – user6498696 Dec 06 '17 at 06:06

1 Answers1

0

I think the port value should be as below

props.put("mail.smtp.port", "587");
Praveen Kumar
  • 1,515
  • 1
  • 21
  • 39
  • 1
    thank you but I already solved this problem just now. It looks like I only need these to make it work: props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.ssl.trust", "*"); – user6498696 Nov 25 '17 at 11:16