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

K

kishore

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 = "(e-mail address removed)";
private static String ToAddress = "(e-mail address removed)";


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.
 
T

Thomas Fritsch

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
 
R

Roedy Green

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

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top