MailConnectException while sending mail using java mail api

2013-12-25T03:42:14

Trying to send an email using java mail api. And I keep getting MailConnectException. I have tried multiple ways to solve it without success.

Exception is thrown by this statement

transport.connect("smtp.gmail.com", "[email protected]", "myPassword");

Can anyone tell me what I'm doing wrong?

public static void main(String[] args) {
    String host = "smtp.gmail.com";
    String from = "[email protected]";
    Properties props = System.getProperties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", "myPassword");
    props.put("mail.smtp.port", "465"); 
    props.put("mail.smtp.auth", "true");
    try{
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipients(Message.RecipientType.TO, "[email protected]");
        message.setSubject("sending in a group");
        message.setText("Welcome to JavaMail");
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.gmail.com", "[email protected]", "myPassword");//CAUSES EXCEPTION
        transport.sendMessage(message, message.getAllRecipients());
    }catch(MessagingException e){
        e.printStackTrace();
    }
}

Stack trace:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1984)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656)
    at javax.mail.Service.connect(Service.java:345)
    at javax.mail.Service.connect(Service.java:226)
    at com.karmacrafts.util.CustomEmail.main(CustomEmail.java:127)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:301)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
    ... 4 more

Copyright License:
Author:「Susie」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/20766044/mailconnectexception-while-sending-mail-using-java-mail-api

About “MailConnectException while sending mail using java mail api” questions

Trying to send an email using java mail api. And I keep getting MailConnectException. I have tried multiple ways to solve it without success. Exception is thrown by this statement transport.conn...
I have written this java code for sending email but this code is throwing an exception (com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1; nested
my server is localhost com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 456; timeout -1; nested exception is: java.net.ConnectException: Connection time...
I am using Java Mail API Ver. 1.4.2 for sending out email in JSF 1.2-Spring based Web Application. I am using non secured SMTP connection for sending out a mail. This email configuration is working...
I was facing a issue while sending a mail to gmail account using spring mail. I referred most of the posts from stackoverflow and tried those. But no luck. Still am stuck with it. Please suggest w...
I want to send and receive mail through my java program using SendMail which is a Mail Transfer Agent in linux....How to integrate the API's of SendMail in my java program so that I can use it for
final int port = 587; String host = "mail.website.com"; final String user = "[email protected]"; final String password = "password"; String to = "[email protected]"; Properties props
I am using java, and wilfly 17 running on ubuntu 18 and would like to send an e-mail via localhost / a remote host, i.e. gmail, using this server. I have implemented the solution described in send...
I have written code for mail sending in java( I am using java mail api for this). Here uerId and Password(For authentication purpose) I am giving of my domain because I want my email server to be u...
I trying to send mail from domain but getting some error. Code: package SendingClass; import java.util.*; import java.util.Date; import java.util.Properties; import javax.mail.Authenticator;

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.