iam unable to send multiple attachments with a mail using JavaMail?

Hai to all,

Iam unable to send multiple attachments with a email,see

iam have succeeded in sending one attachment with a email.

when iam tring to add two or more attachments to a mail,

it is giving a Exception like this:

javax.mail.MessagingException: IOException while sending message;

nested exception is:

java.io.IOException: No content

at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:577)

at javax.mail.Transport.send0(Transport.java:151)

at javax.mail.Transport.send(Transport.java:80)

at AttachFilesModified.sendMail(AttachFilesModified.java:185)

at AttachFilesModified.main(AttachFilesModified.java:43)

this is my code snipnet:

*************************

BodyPart messageBodyPart = new MimeBodyPart();

Multipart multipart = new MimeMultipart();

if(body != null)

{

messageBodyPart.setText(body);

multipart.addBodyPart(messageBodyPart);

}

/*if(attachments != null)

{

for(int i = 0; i < attachments.length; i++)

{*/

String filename="D:\\nagaraju\\apachi\\axis-bin-1_3.zip";

//String s[]=filename.split("\\");

//System.out.println(s);

//String s1=s[1];

//String filename1=s[s.length-1];

messageBodyPart = new MimeBodyPart();

DataSource source = new FileDataSource(filename);

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setFileName(filename);

multipart.addBodyPart(messageBodyPart);

//second file attaching

/*String filename1="C:\\nagadoc.txt";

BodyPart messageBodyPart1=new MimeBodyPart();

DataSource source1=new FileDataSource(filename1);

messageBodyPart.setDataHandler(new DataHandler(source1));

messageBodyPart.setFileName(filename1);

multipart.addBodyPart(messageBodyPart1);

/*}

}*/

mess.setContent(multipart);

Address[] allRecips = mess.getAllRecipients();

if(toStdOut)

{

System.out.println("done.");

//System.out.println("Sending message (\"" + mess.getSubject().substring(0,10) + "...\") to :");

System.out.println("Sending message................");

for(int i = 0; i < allRecips.length; i++)

{

System.out.print(allRecips + ";");

}

System.out.println("...");

}

Transport.send(mess);

if(toStdOut)

{

System.out.println("done.");

}

return 0;

}

}

What's wrng with that code snipnet?

Nagaraju G.

[2593 byte] By [nagaraju_sun_screena] at [2007-10-2 5:38:47]
# 1

This works fine with me, try it or compare it if you want.

public void sendEmail( String from, String to,

String subject, String body) {

fMailServerConfig.put("mail.smtp.host", " <<mail server>>");

Session session = Session.getDefaultInstance( fMailServerConfig, null );

MimeMessage message = new MimeMessage( session );

try {

message.setFrom(new InternetAddress(from));

message.setRecipient(Message.RecipientType.TO,

new InternetAddress(to));

message.setSubject( subject);

message.setText( body);

//Adds Attechment:

Multipart multipart = new MimeMultipart();

BodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setText("Here are my attachments");

multipart.addBodyPart(messageBodyPart);

messageBodyPart = new MimeBodyPart();

//first attachment

DataSource source = new FileDataSource("C:\\img1.jpg");

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setFileName("C:\\Telnor1.jpg");

multipart.addBodyPart(messageBodyPart);

//Second attachment

DataSource source2 = new FileDataSource("C:\\img2.jpg");

messageBodyPart.setDataHandler(new DataHandler(source2));

messageBodyPart.setFileName("C:\\Telnor2.jpg");

multipart.addBodyPart(messageBodyPart);

//etc...

message.setContent(multipart);

Transport.send( message );

}catch (MessagingException e){

System.err.println("Cant send mail. " + e);

}

}

-******-

The error on your code might be:

*****

BodyPart messageBodyPart1=new MimeBodyPart();

DataSource source1=new FileDataSource(filename1);

messageBodyPart.setDataHandler(new DataHandler(source1));

messageBodyPart.setFileName(filename1);

multipart.addBodyPart(messageBodyPart1);

*****

You don't need to create a new BodyPart, and apart from that you'r seting values on "messageBodyPart" but adding "messageBodyPart1" to your multipart :P

Well see u and have a good one!

p.s. i know it's a little late from the day you posted, but at least it might help somebody else :D .

Omar_TJa at 2007-7-16 1:49:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi, Omar,

I ran into the same problem, need to send multiple email attachments. I tried your code, almost exact copy/paste. I attached two files to an email, but first file disappeared, while the second file got attached twice. I could not figure out why. Got any idea?

I will really appreicate your help. Thanks.

lavendera at 2007-7-16 1:49:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
That's because the code is wrong. You have to create twoMimeBodyPart objects, one for each part. You can't reusejust one.
bshannona at 2007-7-16 1:49:10 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...