File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes Attachment in email not working in server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Attachment in email not working in server" Watch "Attachment in email not working in server" New topic

Attachment in email not working in server

sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
I am working on a application where i need to attach some files to and email and send them.I have coded in my local machine and it was working perfectly fine but when i deployed the application to the server, its not working , i think its throwing some exception.Can any one suggest me wt to do.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Please read ItDoesntWorkIsUseless and TellTheDetails.
sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
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 = "";

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

props.put("", smtp);
Session session = Session.getDefaultInstance(props, null);

Message msg = new MimeMessage(session);

InternetAddress addressFrom = new InternetAddress(from);

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.setText(message);

//Adding Attachments:
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart = new MimeBodyPart();

/* Multipart multipart1 = new MimeMultipart();
BodyPart messageBodyPart1 = new MimeBodyPart();
// messageBodyPart.setText(message);
messageBodyPart1 = new MimeBodyPart(); */

//first attachment
if ((file1!=null) && (file1.trim().length() > 0) ) {
DataSource source = new FileDataSource(file1);
messageBodyPart.setDataHandler(new DataHandler(source));

//Second attachment
if ((file2!=null) && (file2.trim().length() > 0) ) {
BodyPart messageBodyPart1 = new MimeBodyPart();
DataSource source2 = new FileDataSource(file2);
messageBodyPart1.setDataHandler(new DataHandler(source2));

//Third attachment
if ((file3!=null) && (file3.trim().length() > 0) ) {
BodyPart messageBodyPart2 = new MimeBodyPart();
DataSource source3 = new FileDataSource(file3);
messageBodyPart2.setDataHandler(new DataHandler(source3));

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
i am not sure what exception its giving

Then you had better find out, hadn't you?

if ((cc == "") || (cc == null) || (cc ==" "))

Strings aren't compared using the "==" comparator, they're compared using the "equals" method. Note that the test for null should come first, so that the calls to "equals" don't fail with a NullPointerException.

If you're using a "real" mail server -as opposed to something that runs on your local machine- it's likely that you will need to use authentication in order to access it.

In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is.
[ October 09, 2008: Message edited by: Ulf Dittmer ]
I agree. Here's the link:
subject: Attachment in email not working in server
It's not a secret anymore!