• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Junilu Lacar
  • Rob Spoor
  • Liutauras Vilda
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Himai Minh
  • Carey Brown
  • Frits Walraven

Attachment in email not working in server

Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Posts: 43024
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read ItDoesntWorkIsUseless and TellTheDetails.
sruthi reddy
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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("mail.smtp.host", 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
Posts: 43024
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic