reading body part of a mime multipart
Solution 1
Yes, you have to iterate through each BodyPart to know it's type and then get the content accordingly. Here's what I used to get the content of a message. But still I am not able to get the right content for some messages.
Edited
Works better after implementing the code suggested by Bill.
Object msgContent = messages[i].getContent();
String content = "";
/* Check if content is pure text/html or in parts */
if (msgContent instanceof Multipart) {
Multipart multipart = (Multipart) msgContent;
Log.e("BodyPart", "MultiPartCount: "+multipart.getCount());
for (int j = 0; j < multipart.getCount(); j++) {
BodyPart bodyPart = multipart.getBodyPart(j);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equalsIgnoreCase("ATTACHMENT"))) {
System.out.println("Mail have some attachment");
DataHandler handler = bodyPart.getDataHandler();
System.out.println("file name : " + handler.getName());
}
else {
content = getText(bodyPart); // the changed code
}
}
}
else
content= messages[i].getContent().toString();
Solution 2
This solution worked much better for me. I merely wanted to log my email message for development/testing purposes. Use the MimeMessage.writeTo(OutputStream) method.
void logMimeMessage(MimeMessage msg) throws MessagingException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
msg.writeTo(out);
} catch (IOException e) {
logger.catching(new MyException("Cannot log MimeMessage",e));
}
logger.error(out.toString());
}
Thanks to the comment by @zzzzz above, who linked to this answer JavaMail - Parsing email content, can't seem to get it to work! (Message.getContent())
J0rd4n500
Updated on May 04, 2020Comments
-
J0rd4n500 almost 4 years
ok so I use .getcontent and receive javax.mail.internet.MimeMultipart@fd13b5 etc.
I know i need something like this in my code but i dont know what exactly is needed.
if (p.isMimeType("text/plain")) { pr("This is plain text"); pr("---------------------------"); if (!showStructure && !saveAttachments) System.out.println((String)p.getContent()); } else if (p.isMimeType("multipart/*")) { pr("This is a Multipart"); pr("---------------------------"); Multipart mp = (Multipart)p.getContent(); level++; int count = mp.getCount(); for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i)); level--; } else if (p.isMimeType("message/rfc822")) { pr("This is a Nested Message"); pr("---------------------------"); level++; dumpPart((Part)p.getContent()); level--;
at the moment i am trying to put all the information in to astring which is then shown up on a GUI at the moment i have it all working fine bar the body content which is showing as. javax.mail.internet.MimeMultipart@fd13b5. any help would be much appreciated as im quite stuck.
package EmailTable; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.util.List; import java.util.Properties; import javax.activation.DataHandler; import javax.mail.BodyPart; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Email implements ActionListener { private mail mail; private List mails; private String password; private String user; private String getText; private boolean textIsHtml = false; public Email(List mails,String password,String user) throws MessagingException, IOException { password = "password"; user = "user"; this.mails = mails; String host = "10..10.10.10"; Properties properties = System.getProperties(); Session session = Session.getDefaultInstance(properties); Store store = session.getStore("pop3"); store.connect(host, user, password); Folder folder = store.getFolder("inbox"); folder.open(Folder.READ_ONLY); Message[] messages = folder.getMessages(); int length = messages.length-1; for (int i = length; i > length-30; i--) { mail = new mail(); mail.setEmail(messages[i].getFrom()[0]); String to = InternetAddress.toString( messages[i].getRecipients(Message.RecipientType.TO)); if (to != null) { mail.setEmail2(to); } mail.setSubject(messages[i].getSubject()); mail.setDate(messages[i].getSentDate()); mail.setMessage(messages[i]); mail.setContent(((MimeMessage)messages[i]).getContent()); Email.this.mails.add(mail); } } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }
-
J0rd4n500 over 11 yearsI've looked at that and i didnt understand what half of it meant like were did P come from etc.
-
ThePCWizard over 11 yearsp is the Part/bodypart object that you would have to pass to the method, to the get content out of it. See the my edited answer, specially the
else
statement -
prnjn over 5 years@ThePCWizard Can you tell me for checking if attachment is present do we need to check Content-Id also. i.e if Content-Id is not blank then the body part is an attachment ? like in this code-> if ( (null != disp1 && !"".equals(disp1)) && (disp1.equalsIgnoreCase(javax.mail.Part.ATTACHMENT) || disp1.equalsIgnoreCase(javax.mail.Part.INLINE)) ) { isAttachment = true; }else if (!"".equals(contentId)){ isAttachment = true; }