Please help me for reading outlook attachment from Javaa
843830Feb 14 2003 — edited Jun 27 2005Dear All,
I am developing a Java Email client. This Email client is working for "IMAP" server. This is working fine for recieving all the emails other than Outlook express attachment. If any mail coming with attachement from outlook express my program doesn't able to find the attachment content. It says that there is no attachment. This is first time I am posting a topic even I had answered some topics. Now I am expecting your help for to solve this. Please help me if anybody faced the same problem.
Your inputs are really welcome.
Here is my example for your reference. My ultimate aim is to retrieve email attachment.
Please help me..........
( This soruce taken from some other site )
public static void main( String []argc ) {
try {
Properties props = new Properties();
Session session = Session.getInstance(props, null);
Store store = session.getStore("imap");
store.connect(host, username, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));
Message message[] = folder.getMessages();
int iCount;
iCount = message.length-1;
for (int i=0 ; i < 5 ; i++ ) {
System.out.println( message[ iCount ].getFrom()[0] );
Object content = message[ iCount ].getContent() ;
if(content instanceof Multipart) {
handleMultipart((MimeMultipart ) content);
} else {
handlePart(message);
}
}
folder.close(false);
store.close();
} catch ( Exception e ) {
}
}
public static void handleMultipart( MimeMultipart multipart) throws MessagingException, IOException ,Exception
{
for (int i=0, n=multipart.getCount(); i<n; i++)
{
handlePart( multipart.getBodyPart(i) );
}
}
public static void handlePart(Part part) throws MessagingException, Exception
{
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (disposition == null)
{
if ( (contentType.length() >= 10) && (contentType.toLowerCase().substring( 0, 10).equals("text/plain"))) {
} else { // Don't think this will happen
saveFile(part.getFileName(), part.getInputStream());
}
} else {// if (disposition.equalsIgnoreCase(Part.ATTACHMENT))
saveFile(part.getFileName(), part.getInputStream());
}
}
public static void saveFile(String filename, InputStream input) throws IOException
{
if (filename == null) {
filename = File.createTempFile("QWWWxx", ".out").getName();
}
// Do no overwrite existing file
File file = new File(filename);
for (int i=0; file.exists(); i++) {
file = new File(filename+i);
}
System.out.println( "Saving file "+filename );
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();
}