aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes saving attachments with javamail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "saving attachments with javamail" Watch "saving attachments with javamail" New topic
Author

saving attachments with javamail

vidhya subramaniam
Ranch Hand

Joined: Jul 14, 2001
Posts: 91
Hello,
I am tring the save the attachments from the messages in the Inbox folder using the following code.
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class GetParts {
public static void main (String args[])
throws Exception {
String host ="omnimail1.omnifax.xerox.com";
String username ="edi";
String password ="ediedi";
// Get session
Session session = Session.getInstance(
new Properties(), null);
// Get the store
Store store = session.getStore("imap");
store.connect(host,username,password);
// Get folder
Folder folder = store.getFolder("INBOX/AIG");
folder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));
// Get directory
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ": "
+ message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
System.out.println(
"Do you want to get the content?Yes to read/quit to end]");
String line = reader.readLine();
if ("YES".equals(line)) {
Object content = message[i].getContent();
if (content instanceof Multipart) {
handleMultipart((Multipart)content);
} else {
handlePart(message[i]);
}
} else if ("QUIT".equals(line)) {
break;
}
}
// Close connection
folder.close(false);
store.close();
}
public static void handleMultipart(Multipart multipart)
throws MessagingException, IOException {
for (int i=0, n=multipart.getCount(); i<n; i++) {
handlePart(multipart.getBodyPart(i));
}
}
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) &&
(contentType.toLowerCase().substring(
0, 10).equals("text/plain"))) {
part.writeTo(System.out);
} else { // Don't think this will happen
System.out.println("Other body: " + contentType);
part.writeTo(System.out);
}
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("Attachment: " + part.getFileName() +
" : " + contentType);
saveFile(part.getFileName(), part.getInputStream());
} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
System.out.println("Inline: " +
part.getFileName() +
" : " + contentType);
saveFile(part.getFileName(), part.getInputStream());
} else { // Should never happen
System.out.println("Other: " + disposition);
}
}
public static void saveFile(String filename,
InputStream input) {
try{
if (filename != null) {
filename = File.createTempFile("xx", ".out").getName();
}
// Do no overwrite existing file
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 = bis.read()) != -1) {
bos.write(aByte);
}
bos.flush();
bos.close();
bis.close();
}
catch(IOException exp){
//
}
}}

My default directory is /tmp. So when I say
createTempFile("xx",".out") wont a file xx.out be created in /tmp? or where exactly the file goes?
can someone please help?
Thanks,
Vidhya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: saving attachments with javamail