0

I am using the JavaMail – GMail via TLS approach from below link to send mail from java application.

when I am trying to do that I am getting the below Exception so can someone tell me what I need to do for making successful connection in order to send an email.

javamail-api-sending-email-via-gmail-smtp-example link

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 905, response: -1
    at com.pr.SendMailTLS.main(SendMailTLS.java:48)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 905, response: -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1215)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)
    at javax.mail.Service.connect(Service.java:258)
    at javax.mail.Service.connect(Service.java:137)
    at javax.mail.Service.connect(Service.java:86)
    at javax.mail.Transport.send0(Transport.java:150)
    at javax.mail.Transport.send(Transport.java:80)
    at com.pr.SendMailTLS.main(SendMailTLS.java:43)
Dasrath
  • 366
  • 2
  • 11
Yavvari Pradeep
  • 301
  • 5
  • 19
  • As @SlipperySeal see gmail provide port for TLS 587 see at https://support.google.com/mail/answer/13287?hl=en – Bhuwan Prasad Upadhyay Dec 11 '15 at 05:36
  • Take a look http://stackoverflow.com/questions/26087018/sending-emails-through-java-javax-mail-messagingexception-could-not-connect-t/26092701#26092701 – Anptk Dec 11 '15 at 05:45

3 Answers3

2

you need to make a note of this:

Outgoing Mail (SMTP) Server
requires TLS or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465 

Since your using TLS, use the port - 587.

in your java code:

    final String username = "yoruusername@gmail.com";
    final String password = "yorupasswors";

    //set the following configs as follows

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

Also go to this gmail settings link. :

  • under "Password", disable to 2-step verification.
  • Under "Account permission", enable "Access for less secure apps"
JGCW
  • 1,509
  • 1
  • 13
  • 25
0

SendMail.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SendMail extends HttpServlet 
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException 

        {
    resp.setContentType("text/html");
    PrintWriter out=resp.getWriter();


    String to=req.getParameter("to");
    String subject=req.getParameter("sub");
    String message=req.getParameter("msg");

    Mailer.send(to,subject,message);

    out.print("message has been send successfully");

    out.close();
        }
}
  • Please explain your code as to what its doing and what is the problem with the code written by OP. Also consider updating the same answer rather than posting another answer – Shashank Kadne Dec 11 '15 at 05:54
  • Here we have mail.jar and activation.jar – Vikram Aditya Dec 11 '15 at 06:08
  • Here we have mail.jar and activation.jar Properties class is use to put all the properties for smtp protocol. and set socket factory port 465. create Session object for password authentication. use MIME object for send message through transport layer. – Vikram Aditya Dec 11 '15 at 06:14
0

Mailer.java

enter code here
 import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;


    public class Mailer {
    public static void send(String to,String sub,String msg)
    {
        String host="smtp.gmail.com";
        final String user="abc@gmail.com";
        final String pass="123456";

        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");

        Session session = Session.getDefaultInstance(props,  
                 new javax.mail.Authenticator() {  
                  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(sub);  
             message.setText(msg);  


            Transport.send(message);
            System.out.println("done");

        } catch (MessagingException e) 
        {
            e.printStackTrace();
        }
    }
    }