4 Replies Latest reply: Jul 27, 2012 11:58 AM by DrClap RSS

    Java Mail----getting "could not connect smtp server" error---plz help


      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title> Java Mail </title>
      <form action="sendMail.jsp" method="POST">
      <table border="0" align="center" cellpadding="5">
      <thead><tr> <td colspan="3" align="center">
      <b> Send Mail </b> </td> </tr> </thead>
      <td> To </td> <td> : </td>
      <td> <input type="text" name="to" value="" /> </td>
      <td> Subject </td> <td> : </td>
      <td> <input type="text" name="subject" value="" /> </td>
      <td> Message </td> <td> : </td>
      <td> <textarea name="message" rows="8" cols="30">
      <td colspan="3" align="center">
      <input type="submit" value="Send Mail" />

      <input type="reset" value="Reset" />


      * To change this template, choose Tools | Templates
      * and open the template in the editor.

      package jMail;

      import javax.mail.*;
      import javax.mail.internet.*;
      import java.util.*;

      * @author samsam
      public class Mail {
      private String to;
      private String from;
      private String message;
      private String subject;
      private String smtpServ;

      * @return the to
      public String getTo() {
      return to;

      * @param to the to to set
      public void setTo(String to) {
      this.to = to;

      * @return the from
      public String getFrom() {
      return from;

      * @param from the from to set
      public void setFrom(String from) {
      this.from = from;

      * @return the message
      public String getMessage() {
      return message;

      * @param message the message to set
      public void setMessage(String message) {
      this.message = message;

      * @return the subject
      public String getSubject() {
      return subject;

      * @param subject the subject to set
      public void setSubject(String subject) {
      this.subject = subject;

      * @return the smtpServ
      public String getSmtpServ() {
      return smtpServ;

      * @param smtpServ the smtpServ to set
      public void setSmtpServ(String smtpServ) {
      this.smtpServ = smtpServ;

      public int sendMail(){
      Properties props = new Properties();
      // Debug or not true/false
      props.put("mail.debug", "false");

      // the email account on gmail
      props.put("mail.smtp.user", to);

      // gmail host details
      props.put("mail.smtp.host", smtpServ);
      props.put("mail.smtp.port", "993");

      // enable authentication of STARTTLS
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.debug", "true");
      props.put("mail.smtp.socketFactory.port", "993");
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");
      props.put("mail.smtp.starttls.enable", "true");
      Authenticator auth = new SMTPAuthenticator();
      Session session = Session.getInstance(props, auth);

      //-- create connection --
      Transport transport = session.getTransport("smtp");
      System.out.println(to+" "+from);

      // -- Create a new message --
      Message msg = new MimeMessage(session);
      // -- Set the FROM and TO fields --
      msg.setFrom(new InternetAddress(from));
      msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      // -- Set some other header information --
      //msg.setHeader("MyMail", "Mr. XYZ" );
      //msg.setSentDate(new Date());
      // -- Send the message --

      System.out.println("Message sent to"+to+" OK." );
      return 0;
      catch (Exception ex)
      System.out.println("EXCEPTION "+ex);
      return -1;

      // Also include an inner class that is used for authentication purposes

      private class SMTPAuthenticator extends javax.mail.Authenticator {
      // @Override
      public PasswordAuthentication getPasswordAuthentication() {
      String username = "samaresh13hit@gmail.com"; // specify your email id here (sender's email id)
      String password = "gasam13210311"; // specify your password here
      return new PasswordAuthentication(username, password);




      Document : sendMail
      Created on : Jul 25, 2012, 3:37:23 PM
      Author : samsam

      <%@page contentType="text/html" pageEncoding="UTF-8"%>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>JSP Page</title>
      <jsp:useBean id="mail" scope="session" class="jMail.Mail" />
      <jsp:setProperty name="mail" property="to" param="to" />
      <jsp:setProperty name="mail" property="from" value="samaresh13hit@gmail.com" />
      <jsp:setProperty name="mail" property="smtpServ" value="smtp.gmail.com" />
      <jsp:setProperty name="mail" property="subject" param="subject" />
      <jsp:setProperty name="mail" property="message" param="message" />
      <h1>Hello World!</h1>

      String to = mail.getTo();
      int result;
      result = mail.sendMail();
      if(result == 0){
      out.println(" Mail Successfully Sent to "+to);
      out.println(" Mail NOT Sent to "+to);


      Getting could not connect to smtpserver in Mail.java..plz help

      Edited by: 949083 on Jul 26, 2012 11:02 PM

      Edited by: 949083 on Jul 26, 2012 11:46 PM
        • 1. Re: Java Mail----getting "could not connect smtp server" error---plz help
          Welcome to the forum!

          When you post code use code tags, \
          , before and after the code to preserve formatting.
          Thanks for the code but you need to actually ask a question, tell us what you have tried and what error you are getting.
          The code you posted won't run; there is no main method so we have no way of knowing what code you are trying execute, in what order, etc.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
          • 3. Re: Java Mail----getting "could not connect smtp server" error---plz help
            I suspect it is because smtp.gmail.com isn't listening on that port or a firewall is blocking access.

            I'd recommend you look at Stack Overflow to take a look at a simpler example to figure out what you are doing wrong.
            • 4. Re: Java Mail----getting "could not connect smtp server" error---plz help
              Yes, you've clearly grabbed something off the web, and what you have there is out of date. Have a look at the JavaMail FAQ where it explains specifically how to connect to GMail.