0

I am trying to send email using the javamail api and it looks like I have to install a SMTP server to get it working.I enabled the default SMTP server on IIS 6 on windows 8. I followed all the steps mentioned in the following blog for setting it up http://pdhewaju.com.np/blog/how-to-install-smtp-on-windows-8-developer-preview/. But still I am getting the following exceptions when I run the code sample given on JavaMail API's wiki page:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at EmailTester.main(EmailTester.java:47)
    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(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:321)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 7 more

The code is as follows:

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

public class EmailTester
{
   public static void main(String [] args)
   {    
      // Recipient's email ID needs to be mentioned.
      String to = "mymail@gmail.com";//My email address

      // Sender's email ID needs to be mentioned
      String from = "blah@blah.com";// Should this be a real email address?

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      properties.put("mail.debug", "true");
      // Get the default Session object.
      Session session = Session.getInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

So I have multiple questions: 1) Is installing SMTP server on IIS a good idea for use with JavaMail API? Am I doing things completely wrong?

2) Should I use third party SMTP server like Apache James?

Pranava Sheoran
  • 529
  • 4
  • 27

2 Answers2

2

No you don't need to install a STMP server to get it working, unless the PC can't get to the internet.

On the line String host = "localhost"; you need to change localhost to an address of a SMTP server on the internet.

Look in your email client's settings and use the address you have there, for development.

However you might need to change it when your application is live because if your production environment is on a different network from the live one, then they might have a different SMTP server. And SMTP servers usually don't like processing email from foreign networks. Its called RELAYING and they prohibit that in order to control SPAM.

Jurgen
  • 2,138
  • 12
  • 18
  • Just curious, why would he have to change settings when app goes live ? Am I missing anything ? – Caadi0 Dec 29 '13 at 17:05
  • 1
    If your production environment is on a different network from the live one, then they might have a different SMTP server. And SMTP servers usually don't like processing email from foreign networks. Its called RELAYING and they prohibit that in order to control SPAM. – Jurgen Dec 29 '13 at 17:11
  • Ahh nice, didn't know that, mind adding that to the answer to make it more informative :) – Caadi0 Dec 29 '13 at 17:14
0

No, you don't have to install a local SMTP server to send emails using Javamail.

You get a Mail Connect error because of these reasons

Use this example to send email using Gmail via Javamail

Also String from = "blah@blah.com"; should contain address through which you are sending emails

If you are sending emails using local host like in this case, it should be like String host = "127.0.0.1\" and remaining configurations, but like I said this is absolutely unnecessary.

Community
  • 1
  • 1
Caadi0
  • 504
  • 8
  • 23
  • It works now for gmail. But for sending email from a non gmail account (my company's email account), I will have to install a SMTP server? – Pranava Sheoran Dec 29 '13 at 16:46
  • No, see answer by Jurgen. – Jurgen Dec 29 '13 at 16:48
  • No, you don't have to install a SMTP server, if your company has a email service that already sends outgoing emails then it already has a SMTP server. Just connect to it. See @Jurgen's answer for more info. – Caadi0 Dec 29 '13 at 17:03