Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Could Not Connect To SMTP Host Connection Failed -- Software caused connection abort: connect

Thread view: 
kishore@novelteam.com - 16 Nov 2005 13:55 GMT
Hi All,

Great to have this discussion group which is helping to clear the
issues.

Thanks in advance to all.

I am having strange exception when trying to send email through java. I
am using my mail server hostname to connect it. My mail server is SMTP
server.
When I tried to ping the IP Address it is fine. I tried to execute the
same in another machine. The result is same.

The exception I am getting is this way.

javax.mail.MessagingException: Could not connect to SMTP host:
<hostname>, port: 25;
 nested exception is:
    java.net.SocketException: Software caused connection abort: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1008)
    at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:197)
    at javax.mail.Service.connect(Service.java:233)
    at javax.mail.Service.connect(Service.java:134)
    at javax.mail.Service.connect(Service.java:86)
    at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:104)

I googled about this issue, but I couldn't get the exact idea and
solution to overcome this exception. My code looks like this.

package sendingmail;

import java.util.Properties;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import java.util.Date;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;

public class SendingMailMultipart
{
   private static String HostName = "<HostName>";
   private static String FromAddress = "fromname@gmail.com";
   private static String ToAddress = "toname@yahoo.co.in";

   public SendingMailMultipart()
   {
       SendEmail();
   }

   protected void SendEmail()
   {
      Properties props = new Properties();
      //props.put("mail.transport.protocol", "smtp");
      props.put("mail.smtp.host",HostName);
      //props.put("mail.debug","true");

      Session session  = Session.getInstance(props);
      try
      {
          Transport trans = session.getTransport("smtp");
          trans.connect();

          Message msg = new MimeMessage(session);
          msg.setFrom(new InternetAddress(FromAddress));
          InternetAddress[] Address = {new
InternetAddress(ToAddress)};
          msg.setRecipients(Message.RecipientType.TO,Address);

msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(ToAddress,true));

msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(ToAddress,false));
          msg.setSubject("Test Mail Using Java Mail Multipart");
          msg.setSentDate(new Date());

          setContentOfMsg(msg);
          msg.saveChanges();
          trans.sendMessage(msg,Address);

          setMultiPartContent(msg);
          msg.saveChanges();
          trans.sendMessage(msg,Address);

          setFileAttachedToMail(msg,"D:\\Kishore\\NOTEPADS\\My
Details.txt");
          msg.saveChanges();
          trans.sendMessage(msg,Address);
      }
      catch(Exception ex)
      {
          ex.printStackTrace();
      }
   }

   private void setContentOfMsg(Message msg)
   {
       try
       {
         String Content = "Hi This Is sending you mail through Java.";
         msg.setText(Content);
         msg.setContent(Content,"text/plain");
       }
       catch(MessagingException ex)
       {
           ex.printStackTrace();
       }
   }

   private void setMultiPartContent(Message msg)
   {
       try
       {
           MimeBodyPart MBP1 = new MimeBodyPart();
           MBP1.setText("Hai Hai Hai Hai Hai Hai Hai Hai Hai Hai Hai
Hai Hai Hai Hai Hai " +
                       "This is part one of a test multipart e-mail");

           MimeBodyPart MBP2 = new MimeBodyPart();
           MBP2.setText("Second Part","us-ascii");

           Multipart MP = new MimeMultipart();
           MP.addBodyPart(MBP1);
           MP.addBodyPart(MBP2);

           msg.setContent(MP);
       }
       catch(MessagingException ex)
       {
           ex.printStackTrace();
       }
   }

   private void setFileAttachedToMail(Message msg,String FileName)
   {
       try
       {
           MimeBodyPart MBP1 = new MimeBodyPart();
           MBP1.setText("This is the first part in FileAttachment
MimeBodyPart1");

           MimeBodyPart MBP2 = new MimeBodyPart();
           FileDataSource FDS = new FileDataSource(FileName);
           MBP2.setDataHandler(new DataHandler(FDS));
           MBP2.setFileName(FDS.getName());

           Multipart MP = new MimeMultipart();
           MP.addBodyPart(MBP1);
           MP.addBodyPart(MBP2);

           msg.setContent(MP);
       }
       catch(MessagingException ex)
       {
           ex.printStackTrace();
       }
   }

   public static void main(String[] args)
   {
       SendingMailMultipart sendingmailmultipart = new
SendingMailMultipart();
   }
}

Can any one kindly suggest what is that going wrong in this snippet
code, if you have come across the same scenario. Please help out in
this regards.

regards,
kishore.
Thomas Fritsch - 16 Nov 2005 15:04 GMT
> I am having strange exception when trying to send email through java. I
> am using my mail server hostname to connect it. My mail server is SMTP
> server.
> When I tried to ping the IP Address it is fine. I tried to execute the
> same in another machine. The result is same.
Be aware that Ping does not contact port 25 (the SMTP port). Are you
really sure, that an SMTP server is running on your remote host? Is a
firewall involved which might blocks your traffic?
You can check all this manually by using telnet, instead of running your
Java program. See <http://www.activexperts.com/activemail/telnet/> for how.

> The exception I am getting is this way.
>
> javax.mail.MessagingException: Could not connect to SMTP host:
> <hostname>, port: 25;
>   nested exception is:
>     java.net.SocketException: Software caused connection abort: connect

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Roedy Green - 16 Nov 2005 15:33 GMT
On Wed, 16 Nov 2005 15:04:03 GMT, Thomas Fritsch
<i.dont.like.spam@invalid.com> wrote, quoted or indirectly quoted
someone who said :

>Be aware that Ping does not contact port 25 (the SMTP port). Are you
>really sure, that an SMTP server is running on your remote host? Is a
>firewall involved which might blocks your traffic?

make sure an ordinary mail program like Eudora works first.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 16 Nov 2005 15:32 GMT
>javax.mail.MessagingException: Could not connect to SMTP host:
><hostname>, port: 25;

you should have some code like this:

 Properties props = System.getProperties();
        if ( CustConfig.needPasswordToSend )
           {
           props.setProperty ( "mail.smtp.auth", "true" );
           }
       // Get a Session object
        session = Session.getDefaultInstance( props, null );
        session.setDebug( CustConfig.DEBUGGING );

        // Get a Store object, all mail on server
        store = session.getStore( receiveProtocol );

        // Connect

        store.connect( receiveHost, receivePort, receiveLoginID,
receivePassword );

// or

Transport transport = session.getTransport( sendProtocol );
        transport.connect( sendHost, sendPort, sendLoginID,
sendPassword );
   

 
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.