File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

Attachment in email not working in server

sruthi reddy
Ranch Hand

Joined: Aug 03, 2008
Posts: 34
Hi
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.
Thanks
Sruthi
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
Please read ItDoesntWorkIsUseless and TellTheDetails.


Ping & DNS - my free Android networking tools app
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 = "";

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);
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
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: http://aspose.com/file-tools
 
subject: Attachment in email not working in server
 
Similar Threads
session timeout in firefox
Monitoring of server using JMX and SNMP
No more data to read from socket.
Java Mail Using Lotus Notes Componets
Getting confuse on Struts action call when I click back button