java - Issue reading email body when email has attachments? -


i found nice code read email inbox email server, code works fine, have couple issues:

  1. i need capture message body of each email, when email has attachment, receive string javax.mail.internet.mimemultipart@100363, or com.sun.mail.util.base64decoderstream@14e8cee, instead of body message. if email plain text , have no attachments, works fine. seems not 1 problem, have been reading on web can't find solution.

  2. i ignore attachments when size exceed 4 mb.

below class, appreciate comments. it's important novice programmer in java. in advance !

import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.util.properties; import javax.mail.address; import javax.mail.bodypart; import javax.mail.flags; import javax.mail.folder; import javax.mail.message; import javax.mail.messagingexception; import javax.mail.multipart; import javax.mail.nosuchproviderexception; import javax.mail.part; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.store; import javax.mail.flags.flag; import javax.mail.search.flagterm;  public class readmailsample {     properties properties = null;     private session session = null;     private store store = null;     private folder inbox = null;     private string username;      private string password;     string downloaddirectory = "d:/attachment/";      public readmailsample() {         username = "myemail@domain.com";// provide user name         password = "mypassword";// provide password     }      public void readmails() {         properties = new properties();         properties.setproperty("mail.host", "imap.secureserver.net");         properties.setproperty("mail.port", "110");         properties.setproperty("mail.transport.protocol", "imap");         session = session.getinstance(properties,                 new javax.mail.authenticator() {                     protected passwordauthentication getpasswordauthentication() {                         return new passwordauthentication(username, password);                     }                 });         try {             store = session.getstore("imap");             store.connect();             inbox = store.getfolder("inbox");             inbox.open(folder.read_write);             message messages[] = inbox.search(new flagterm(                     new flags(flag.seen), false));             ;             system.out.println("number of mails = " + messages.length);             (int = 0; < messages.length; i++) {                 message message = messages[i];                 address[] = message.getfrom();                 system.out.println("--> ** e-mail no. "+ + " **");                 system.out.println("-------------------------------");                 system.out.println("date : " + message.getsentdate());                 system.out.println("from : " + from[0]);                 system.out.println("subject: " + message.getsubject());                 system.out.println("content :");                 processmessagebody(message);                 system.out.println("--------------------------------");              }             inbox.close(true);             store.close();         } catch (nosuchproviderexception e) {             e.printstacktrace();         } catch (messagingexception e) {             e.printstacktrace();         }     }      public void processmessagebody(message message) {         try {             object content = message.getcontent();             // check string             // check multipart             if (content instanceof string) {                 system.out.println(content);             } else if (content instanceof multipart) {                 system.out.println(content);                 multipart multipart = (multipart) content;                 procesmultipart(multipart);             } else if (content instanceof inputstream) {                 inputstream instream = (inputstream) content;                 int ch;                 while ((ch = instream.read()) != -1) {                     system.out.write(ch);             }             }          } catch (ioexception e) {             e.printstacktrace();         } catch (messagingexception e) {             e.printstacktrace();         }     }      public void procesmultipart(multipart content) {          try {              (int = 0; < content.getcount(); i++) {                 bodypart bodypart = content.getbodypart(i);                 object o;                  o = bodypart.getcontent();                 if (o instanceof string) {                     system.out.println("text = " + o);                 } else if (null != bodypart.getdisposition()                         && bodypart.getdisposition().equalsignorecase(                                 part.attachment)) {                     string filename = bodypart.getfilename();                     system.out.println("text = " + o);                     system.out.println("filename = " + filename);                     inputstream instream = bodypart.getinputstream();                     fileoutputstream outstream = new fileoutputstream(new file(                             downloaddirectory + filename));                     byte[] tempbuffer = new byte[4096];// 4 kb                     int numread;                     while ((numread = instream.read(tempbuffer)) != -1) {                         outstream.write(tempbuffer);                     }                     instream.close();                     outstream.close();                 }                 // else?              }         } catch (ioexception e) {             e.printstacktrace();         } catch (messagingexception e) {             e.printstacktrace();         }      }      public static void main(string[] args) {         readmailsample sample = new readmailsample();         sample.readmails();     } } 

  1. from javamail faq:

  2. define "ignore". did see getsize method?


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -