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