This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes JavaMail saving Attachments 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 "JavaMail saving Attachments" Watch "JavaMail saving Attachments" New topic

JavaMail saving Attachments

vidhya subramaniam
Ranch Hand

Joined: Jul 14, 2001
Posts: 91
I am using javamail to save attachments in incoming messages. My code is pasted below. While saving it picks only the first character from the file and saves it with that name. It doesnt pick the whole file name. How do i use it to name the file as the name in my attachments.
here is my code:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.event.*;
import javax.activation.*;
import java.text.*;
import java.util.Calendar;
public class aiginvoice_1 {
public static void main (String args[])
throws Exception {
String host ="";
String username ="edi";
String password ="ediedi";
// Get session
Session session = Session.getInstance(
new Properties(), null);
// Get the store
Store store = session.getStore("imap");
// Get folder
Folder folder = store.getFolder("INBOX/AIG/EQ");;
BufferedReader reader = new BufferedReader (
new InputStreamReader(;
// Get directory
Message message[] = folder.getMessages();
for (int i = message.length-1,n=message.length;n>i&&i!=-1;i--) {
Date e = message[i].getSentDate();
System.out.println("Message Inbox date :"+e);

Address[] a;
if ((a=message[i].getFrom())!=null) {
for (int j=0;j<a.length;j++)
System.out.println("From : " +a[j].toString());
Calendar cal = Calendar.getInstance();
SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy-MM-dd 06:00:00" );
cal.add(Calendar.DATE,-15); //set to 17 days earlier date (15-31)
fmt1.setCalendar(cal); //set to date after 6am
String s2 =fmt1.format(cal.getTime()); //returns yesterday's date in string format
System.out.println("Yesterday's Date in string form before conversion :"+s2 );
SimpleDateFormat fmt2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d2 = fmt2.parse(s2); //to convert the date in string to date form
System.out.println("Yesterday's date in date type is: "+d2);
if (e.after(d2)) {

Object content = message[i].getContent();
if (content instanceof Multipart) {
System.out.println("Multipart" + i);
if ((a=message[i].getFrom()) !=null) {
for (int j=0;j<a.length;j++) {
System.out.println("From : " +a[j].toString());
String t = ((a[j].toString()).substring(3,13));
String m = "AIG - TEST";
if (t.equals(m)==false)
} //end of for (int j=0)
} //end of if(a=msg[i])
else {
System.out.println("ElseMultipart" + i);
} // end of (if e.after d2)
System.out.println("Old message");
} // end of for loop
// Close connection
} // end of main
public static void handleMultipart(Multipart multipart)
throws MessagingException, IOException {
for (int i=0, n=multipart.getCount(); i<n; i++) {
} //end of handleMulitipart()

public static void handlePart(Part part)
throws MessagingException, IOException {
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (disposition == null) { // When just body
System.out.println("Null: " + contentType);
// Check if plain
/* if ((contentType.length() >= 10) &&
0, 10).equals("text/plain"))) {
} else { // Don't think this will happen
System.out.println("Other body: " + contentType);
} //end of if null
else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("Attachment VS: " + part.getFileName() +
" : " + contentType);
saveFile(part.getFileName(), part.getInputStream());

} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
System.out.println("Inline: " +
part.getFileName() +
" : " + contentType);
} else { // Should never happen
System.out.println("Other: " + disposition);
} // end of handlePart()

public static void saveFile(String filename,
InputStream input) {
if (filename == null) {
filename = File.createTempFile("VSX", ".out").getName();
// Do no overwrite existing file
filename = "/work/vsubra/edi/saxon/" + filename;
File file = new File(filename);
for (int i=0; file.exists(); i++) {
file = new File(filename+i);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = != -1) {
// bos.flush();
// bis.close();
} // end of try()
catch(IOException exp){
System.out.println("IOException:" + exp);
} //end of saveFile()
}//end of class aigemail

Please advise...
ankit choudhary

Joined: Jun 13, 2011
Posts: 1
i am trying to make this javamail of getting attachments but,,
there is little bit of error is coming:=
it is giving me disposition as NULL..
if you or any body have any solution for this please do send...........
I agree. Here's the link:
subject: JavaMail saving Attachments
It's not a secret anymore!