File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes How can I download emails with attachments from Gmail with java programing..(error occur) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "How can I download emails with attachments from Gmail with java programing..(error occur)" Watch "How can I download emails with attachments from Gmail with java programing..(error occur)" New topic
Author

How can I download emails with attachments from Gmail with java programing..(error occur)

sureshreddy govindu
Greenhorn

Joined: Jun 08, 2011
Posts: 29
i am trying this way for downloading attachments from gmail through java program. exception occur class cast exception

How can I download all emails with attachments from Gmail?

package mail;

import java.io.BufferedWriter;
import java.io.File;

import javax.mail.AuthenticationFailedException;
import javax.mail.Folder;
import javax.mail.FolderClosedException;
import javax.mail.FolderNotFoundException;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.ReadOnlyFolderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.StoreClosedException;
import javax.mail.internet.InternetAddress;

public class ReadEmails {

//Constructor Call
public ReadEmails() {
processMail();
}

//Responsible for printing Data to Console
private void printData(String data) {
System.out.println(data);
}

public void processMail() {
Session session = null;
Store store = null;
Folder folder = null;
Message message = null;
Message[] messages = null;
Object messagecontentObject = null;
String sender = null;
String subject = null;
Multipart multipart = null;
Part part = null;
String contentType = null;

try {
printData("--------------processing mails started-----------------");
session = Session.getDefaultInstance(System.getProperties(), null);

printData("getting the session for accessing email.");
store = session.getStore("imaps");

store.connect("imap.gmail.com","username@gmail.com","password");
printData("Connection established with POP3 server.");

// Get a handle on the default folder
folder = store.getDefaultFolder();

printData("Getting the Inbox folder.");

// Retrieve the "Inbox"
folder = folder.getFolder("inbox");

//Reading the Email Index in Read / Write Mode
folder.open(Folder.READ_WRITE);

// Retrieve the messages
messages = folder.getMessages();

// Loop over all of the messages
for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) {
// Retrieve the next message to be read
message = messages[messageNumber];

// Retrieve the message content
messagecontentObject = message.getContent();

// Determine email type
if (messagecontentObject instanceof Multipart) {
printData("Found Email with Attachment");
sender = ((InternetAddress) message.getFrom()[0]).getPersonal();

// If the "personal" information has no entry, check the address for the sender information
printData("If the personal information has no entry, check the address for the sender information.");

if (sender == null) {
sender = ((InternetAddress) message.getFrom()[0]).getAddress();
printData("sender in NULL. Printing Address:" + sender);
}
printData("Sender -." + sender);

// Get the subject information
subject = message.getSubject();

printData("subject=" + subject);

// Retrieve the Multipart object from the message
multipart = (Multipart) message.getContent();

printData("Retrieve the Multipart object from the message");

// Loop over the parts of the email
for (int i = 1; i < multipart.getCount(); i++) {
// Retrieve the next part
part = multipart.getBodyPart(i);

// Get the content type
contentType = part.getContentType();

// Display the content type
printData("Content: " + contentType);

if (contentType.startsWith("text/plain")) {
printData("---------reading content type text/plain mail -------------");
} else {
// Retrieve the file name
String fileName = part.getFileName();
String root="D:/attachments/"+fileName;
File f=new File(root);
boolean flag=f.createNewFile();

Object o=part.getContent();


f= (File) part.getContent();
//System.out.println(flag+"--------------");
printData("retrive the fileNam22222222222222222222222e="+ fileName);
}
}
} else {
printData("Found Mail Without Attachment");
sender = ((InternetAddress) message.getFrom()[0]).getPersonal();

// If the "personal" information has no entry, check the address for the sender information
printData("If the personal information has no entry, check the address for the sender information.");

if (sender == null) {
sender = ((InternetAddress) message.getFrom()[0]).getAddress();
printData("sender in NULL. Printing Address:" + sender);
}

// Get the subject information
subject = message.getSubject();
printData("subject=" + subject);
}
}

// Close the folder
folder.close(true);

// Close the message store
store.close();
} catch(AuthenticationFailedException e) {
printData("aNot able to process the mail reading.");
e.printStackTrace();
} catch(FolderClosedException e) {
printData("bNot able to process the mail reading.");
e.printStackTrace();
} catch(FolderNotFoundException e) {
printData("cNot able to process the mail reading.");
e.printStackTrace();
} catch(NoSuchProviderException e) {
printData("dNot able to process the mail reading.");
e.printStackTrace();
} catch(ReadOnlyFolderException e) {
printData("eNot able to process the mail reading.");
e.printStackTrace();
} catch(StoreClosedException e) {
printData("fNot able to process the mail reading.");
e.printStackTrace();
} catch (Exception e) {
printData("gNot able to process the mail reading.");
e.printStackTrace();
}
}

//Main Function for The readEmail Class
public static void main(String args[]) {
//Creating new readEmail Object
ReadEmails readMail = new ReadEmails();

//Calling processMail Function to read from POP3 Account
readMail.processMail();
}
}

Exception is below:

subject=iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Retrieve the Multipart object from the message
Content: APPLICATION/OCTET-STREAM; name=Split.java
gNot able to process the mail reading.
--------------processing mails started-----------------
getting the session for accessing email.
java.lang.ClassCastException: com.sun.mail.util.BASE64DecoderStream cannot be cast to java.io.File
at mail.ReadEmails.processMail(ReadEmails.java:124)
at mail.ReadEmails.<init>(ReadEmails.java:24)
at mail.ReadEmails.main(ReadEmails.java:179)
Connection established with POP3 server.
Getting the Inbox folder.
Found Email with Attachment
If the personal information has no entry, check the address for the sender information.
Sender -.suresh reddy
subject=iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Retrieve the Multipart object from the message
Content: APPLICATION/OCTET-STREAM; name=Split.java
gNot able to process the mail reading.
java.lang.ClassCastException: com.sun.mail.util.BASE64DecoderStream cannot be cast to java.io.File
at mail.ReadEmails.processMail(ReadEmails.java:124)
at mail.ReadEmails.main(ReadEmails.java:182)


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I download emails with attachments from Gmail with java programing..(error occur)