1
votes

Hello people of stackoverflow!

I've been trying my hands on the JavaMail API

// Recipient's email ID needs to be mentioned.
          String to = "[email protected]";//change accordingly

          // Sender's email ID needs to be mentioned
          String from = "[email protected]";//change accordingly
          final String email = "[email protected]";//change accordingly
          final String password = "xxxxxxxxx";//change accordingly

          // Assuming you are sending email through relay.jangosmtp.net
          String host = "smtp.gmail.com";

          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", "465");

          // Get the Session object.
          Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
             protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(email, password);
             }
          });

          try {
             // Create a default MimeMessage object.
             Message message = new MimeMessage(session);

             // Set From: header field of the header.
             message.setFrom(new InternetAddress(from));

             // Set To: header field of the header.
             message.setRecipients(Message.RecipientType.TO,
             InternetAddress.parse(to));

             // Set Subject: header field
             message.setSubject("Testing Subject");

             // Now set the actual message
             message.setText("Hello, this is sample for to check send "
                + "email using JavaMailAPI ");

             // Send message
             Transport.send(message);

             System.out.println("Sent message successfully....");

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

The programs seems to have no error, after executing I'm getting:

java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465; nested exception is: java.net.ConnectException: Connection timed out: connect

Can anybody please help me out on this??? << -> Yes, I've configured the GMail access to less secured apps' option.... >>

Thnks in advance!!

2
I hope that's not your real password...vinz
Are you connected to the Internet? Using a proxy or something?dotvav
Yes there's a proxy: Automatic configuration script: "pac.zscalertwo.net/philips.com/global-pac.pac"Chetan Hireholi

2 Answers

0
votes

Your firewall or proxy or anti-virus program is probably preventing direct connections.

The JavaMail FAQ has tips for connecting through a proxy, and debugging tips to help determine why you can't connect.

0
votes

When sending mail via Gmail SMTP server using TLS you should use port 587

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");

Port 465 is for sending email using SSL, should look something like this

Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");