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 File attachments using Java Mail API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "File attachments using Java Mail API" Watch "File attachments using Java Mail API" New topic

File attachments using Java Mail API

triveni sangam

Joined: Jan 12, 2001
Posts: 11
Can anybody help me to send attachments using Java Mail API. I tried this before (i dont have the code with me thou) The attachment dose go but not in a redable fashion.
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
Hi! Well check if u r setting de content type properly to text/html.. ne way I got a lil code dat needs some mod... ne way I have it fro an imap server.. please mail me bak asap if u r interested to
ne way meanwile ill try n get de code runnin smooth n post it to u..
Gautham Kasinath
U r not Alone

"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at :
Josh Johnson

Joined: Jan 15, 2001
Posts: 20
I also need to know how to attach a file to a MimeMessage. I have no idea how to do it. I'm guessing that you have to make a MimeBodyPart that contains the file and add it to the message, but there doesn't seem to be any method in the API to put a file in a MimeBodyPart.
Any help would be appreciated.
sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
hi all,
u can attach a message using MimeBodyPart by doing individual Bodypart as many times as required.
MimeBodyPart mbp1= new MimeBodyPart();
MimeBodyPart mbp2= new MimeBodyPart();
Multipart mp=new MimeMultipart();

i hope this will help u.
But i have a doubt how do i get my mails from my yahoo inbox to my machine.when i tried to connect the system through and protcol as http,it throws a exception called
please help.
u r help will be much appreciated.
Thanks in advance,

"Winners don't do different things<br /> They do things differently"
Josh Johnson

Joined: Jan 15, 2001
Posts: 20
Okay. I see how you add MimeBodyParts to a MimeMultiPart, but where does the file you want to attach fit in to the picture? Can someone post a few lines of code that show how you would attach a file called "readme.txt" to a MimeMessage?
Josh Johnson

Joined: Jan 15, 2001
Posts: 20

I found the answer to my question, and I wanted to share with others.
In the code below, msgText1 is the text that appears in the message. readme.txt is the file that is attached.
MimeMessage msg = new MimeMessage(session);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource("readme.txt");
mbp2.setDataHandler(new DataHandler(fds));
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
// add the Multipart to the message
Lakshmi, Narayana

Joined: Aug 24, 2001
Posts: 1
asdf asdf asd asdf asdf asdf asd sda sda
raghavender rao
Ranch Hand

Joined: Jul 20, 2001
Posts: 45
hi ther,
i wd suggest u to use com.oreilly package for file attachments...its working fine with java mail


Rao<BR>Sun Certified Programmer for the Java��� 2 Platform
I agree. Here's the link:
subject: File attachments using Java Mail API
It's not a secret anymore!