Bharath kumar
Bharath kumar

Reputation: 11

send mail using gmail account in java

I have tried to send a simple e-mail to any gmail account using Gmail SMTP. getting the below error.

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

My code is

package common;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.SMTPMessage;

public class SimpleMail{

    /**
           Outgoing Mail (SMTP) Server
           requires TLS or SSL: smtp.gmail.com (use authentication)
           Use Authentication: Yes
           Port for SSL: 465
         */
        public static void main(String[] args) {

             String to="mymailid@gmail.com";
             String subject="New Mail";

             String msg="test test";

            final String user="gmailuser";
            final String pass="gmailpassowd";
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587"); //this is optional
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");



            Session session = Session.getInstance(props,new javax.mail.Authenticator() {

                    @Override
            protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user,pass);
           }
           });

            try {
           MimeMessage message = new MimeMessage(session);
           message.setFrom(new InternetAddress(user));
           message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
           message.setSubject(subject);
           message.setText(msg);

             Transport.send(message);
                System.out.println("Mail sent..");
       }catch(Exception e)
       {
           System.out.println(e);
       }
    }



}

Upvotes: 1

Views: 360

Answers (2)

Davide Gualano
Davide Gualano

Reputation: 12993

I have sent emails through the Gmail smtp in java using the Apache Commons Email library.
The documentation has a nice and simple example:

Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();

Upvotes: 1

Steve Chaloner
Steve Chaloner

Reputation: 8202

Check your ports. From Google's support:

If you tried configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL).

Source: https://support.google.com/mail/answer/78775?hl=en

So, try using port 25 and see what happens.

Upvotes: 1

Related Questions