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
}