Results 1 to 6 of 6
  1. #1
    siddharth is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25

    Hi All,
    Actually I am trying email sending via a java program ( servlet file) using Apache Tomcat server software and eclipse.Whenever i try to run the program ,i am getting an exception all the time i.e javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect.

    Someone please help me to resolve this.
    Thanks in advance.

    Here is my code...

    Java Code:
    package server_sendemail_file;
    
    import java.io.IOException;
    import java.util.Properties;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import java.io.PrintWriter;
    
    
    public class SendEmail extends HttpServlet 
    {
    	private static final long serialVersionUID = 1L;
           
        
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    	{
    		String to = "sid19039@gmail.com";
    		String from = "sid199215@gmail.com";
    		String host = "localhost";
    		Properties prop = System.getProperties();
    		prop.setProperty("mail.smtp.host", host);
    		Session session = Session.getDefaultInstance(prop);
    		
    		PrintWriter out = response.getWriter();
    		
    		try
    		{
    			MimeMessage mes = new MimeMessage(session);
    			mes.setFrom(new InternetAddress(from));
    			mes.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
    			mes.setSubject("this is the subject line!");
    			mes.setText("this is the actual message");
    			Transport.send(mes);
    			out.println("message sent successfully...");
    		}
    		catch(MessagingException s)
    		{
    			out.print(s);
    			s.printStackTrace();
    		}
    	}
    
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 2

    Do you have a mailserver running on localhost? Doesn't look like it. And IF you do, is it listening on port 25 and do you need to authenticate in order send mail? My guess is, you should probably be connecting to some other host.

  3. #3
    siddharth is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 2

    Quote Originally Posted by masijade View Post
    Do you have a mailserver running on localhost? Doesn't look like it. And IF you do, is it listening on port 25 and do you need to authenticate in order send mail? My guess is, you should probably be connecting to some other host.
    Thanks for your reply. yeah I don't have any mailserver running but when i complete the installation then how would i check whether it is listening on port 25?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 2

    By trying it out? If you get a connection failure, there is nothing listening there. If you get an authentication error, your user credentials are no good.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 2

    The other question here is SHOULD the mailserver be on localhost, or should you be trying to connect to a DIFFERENT mailhost, such as your gmail host considering the provided from address.

    Otherwise, the port, per default is 25, if you want a different port you would have to explicitly enter it when you install a mailserver, or explicitly configure it after installing it.

  6. #6
    siddharth is offline Member
    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    Default Re: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 2

    hey! after installing mail server(SmarterMail web server ) ,i got "response:-1" with exception: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: -1, what does this mean?

Similar Threads

  1. Replies: 2
    Last Post: 08-24-2011, 05:32 PM
  2. Problems sending SMTP SSL with exchange, port 25
    By iamjuan in forum Advanced Java
    Replies: 1
    Last Post: 08-28-2009, 05:11 AM
  3. Java Mail "SMTP host" Exception
    By Poyntz in forum Networking
    Replies: 15
    Last Post: 01-06-2009, 08:52 PM
  4. Replies: 0
    Last Post: 10-31-2008, 08:07 PM
  5. A Client to Send SMTP Mail
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •