1

Hi I am trying to send a test email using some java code that i have written. Unfortunately, when i run my program i get this error:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1922)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.brookfieldres.operations.Email.main(Email.java:61)

This is my code below. Please note that i have another properties file where all my Resources are stored. I need to figure out why i keep getting this error. What am i doing wrong?

package com.brookfieldres.operations;

import javax.mail.Session;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;

import java.util.*; 
import java.util.Properties;
import java.util.ResourceBundle;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Transport; //responsible for sending the actual email. 
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress; 

public class Email {

public Message message; 
public String sendTo; 
public String sentFrom; 

public static void main (String[] Args) { 

final Logger aLogger = Logger.getLogger(NewLocation.class.getName()); 
ResourceBundle resource = ResourceBundle.getBundle("Resources");

    String sendTo = resource.getString("RECIEVING_USER_EMAIL"); 

    String sentFrom = resource.getString("SENDING_USER_EMAIL");
    final String userName = resource.getString("SMTP_USER"); 
    final String password = resource.getString("SMTP_PASSWORD");

    final String host = resource.getString("SMTP_SERVER"); 

    Properties prop = new Properties(); 
    prop.put("mail.smtp.host", host); 
    prop.put("mail.smtp.port", "465");
    prop.put("mail.smtp.auth", "True"); 


    Session session = Session.getDefaultInstance(prop, new Authenticator() { 

        protected PasswordAuthentication getPasswordAuthentication() { 
            return new PasswordAuthentication(userName, password); 
        }
    }); 

    try {

        Message message = new MimeMessage(session); 
        message.setFrom(new InternetAddress(sentFrom));
        message.setRecipient(Message.RecipientType.TO, new          InternetAddress(sendTo));
        message.setSubject("Test");
        message.setText("Test");


        Transport.send(message);

        System.out.println("Sent Message Successfully.");

    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}//main

}
MesamH
  • 91
  • 1
  • 2
  • 6
  • 1
    Don't you have to set properties for `ssl.enable`, `starttls.enable`, etc? I think there are missing properties. Also, look at other answers here on SO such as [this one](http://stackoverflow.com/questions/35007279/javamail-could-not-connect-to-smtp-host?rq=1) – KevinO Apr 12 '16 at 15:52
  • Thanks man! I think your previous comment i accidentally deleted. I appreciate it. – MesamH Apr 12 '16 at 16:12
  • 1
    Fix these [common mistakes](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes), follow these [Gmail instructions](http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail), and if it still doesn't work follow these [connection debugging instructions](http://www.oracle.com/technetwork/java/javamail/faq/index.html#condebug) and post the results here. – Bill Shannon Apr 12 '16 at 19:20
  • Thanks Bill figured it out. I was missing the ssl stuff, and my password was not a 16 character password from Google. – MesamH Apr 13 '16 at 13:53

0 Answers0