I am trying to send email with some attachments and it was working fine in my local machine.But when i deployed the application in the server, its not working . i am not sure what exception its giving but this is the code i am using to send mail
String file1 = (String)request.getParameter("file1");
String file2 = (String)request.getParameter("file2");
String file3 = (String)request.getParameter("file3");
try {
Properties props = new Properties();
String smtp = "";
String semail = "";
try
{
System.out.println("getting context");
javax.naming.Context ctx = new javax.naming.InitialContext();
smtp = (String) ctx.lookup("java:comp/env/smtp");
semail = (String) ctx.lookup("java:comp/env/semail");
} catch (Exception e) {
System.err.println("Error in getting SMTP address "+e);
e.printStackTrace();
}
System.out.println("SMTP="+smtp);
props.put("mail.smtp.host", smtp);
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress addressTo = new InternetAddress(to);
msg.setRecipient(Message.RecipientType.TO, addressTo);
// InternetAddress[] addressTo = new InternetAddress[recipients.length];
// for (int i = 1; i < 2; i++)
// {
if ((cc == "") || (cc == null) || (cc ==" ")) {
//InternetAddress addressCC = new InternetAddress(cc);
//msg.setRecipient(Message.RecipientType.CC, addressCC);
}else {
InternetAddress addressCC = new InternetAddress(cc);
msg.setRecipient(Message.RecipientType.CC, addressCC);
}
//addressTo[i] = new InternetAddress(recipients[i]);
// }
// msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "email from FHAContact Application");
// Setting the Subject and Content Type
msg.setSubject(subject);
// msg.setText(message);
//Adding Attachments:
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
/* Multipart multipart1 = new MimeMultipart();
BodyPart messageBodyPart1 = new MimeBodyPart();
// messageBodyPart.setText(message);
multipart.addBodyPart(messageBodyPart1);
messageBodyPart1 = new MimeBodyPart(); */
//first attachment
if ((file1!=null) && (file1.trim().length() > 0) ) {
DataSource source = new FileDataSource(file1);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
}
//Second attachment
if ((file2!=null) && (file2.trim().length() > 0) ) {
BodyPart messageBodyPart1 = new MimeBodyPart();
DataSource source2 = new FileDataSource(file2);
messageBodyPart1.setDataHandler(new DataHandler(source2));
messageBodyPart1.setFileName(source2.getName());
multipart.addBodyPart(messageBodyPart1);
}
//Third attachment
if ((file3!=null) && (file3.trim().length() > 0) ) {
BodyPart messageBodyPart2 = new MimeBodyPart();
DataSource source3 = new FileDataSource(file3);
messageBodyPart2.setDataHandler(new DataHandler(source3));
messageBodyPart2.setFileName(source3.getName());
multipart.addBodyPart(messageBodyPart2);
}
msg.setContent(multipart);
Transport.send(msg);