Reputation: 301
I have the following code to read the Gmail email using Java. If the email is having very simple plain text message, then the code is working fine and I am able to see the content of the (body) being displayed properly.
But, for some emails I see the following message while I try to
display body (CONTENT:javax.mail.internet.MimeMultipart@2038ae61)
. I am finding difficult to fix this issue. Please help me.
Code :
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMultipart;
public class Experiment {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "[email protected]",
"password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
// showContent(msg);
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());
} catch (Exception mex) {
mex.printStackTrace();
}
}
Upvotes: 0
Views: 5370
Reputation: 200
This is a complete file attachments reading solution:
final Map<String, byte[]> attachments = new HashMap<>();
try {
if ( mailMessage.getContentType().contains("multipart") && mailMessage.getContent() instanceof Multipart ) {
final Multipart multiPart = (Multipart) mailMessage.getContent();
for ( int i = 0; i < multiPart.getCount(); i++ ) {
final MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
if ( Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()) ) {
final String fileName = MimeUtility.decodeText(part.getFileName());
final byte[] bytes = IOUtils.toByteArray(part.getInputStream());
attachments.put(fileName, bytes); }
}
}
} catch (final IOException | javax.mail.MessagingException e) {
throw new IllegalStateException(e);
}
attachments.forEach(( f, c ) -> {
System.out.println("filename: " + f + " content:" + new String(c));
});
Upvotes: 0
Reputation: 672
This as works for me
String contentType = msg.getContentType();
String messageContent="";
if (contentType.contains("multipart")) {
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
messageContent = part.getContent().toString();
}
}
else if (contentType.contains("text/plain")
|| contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}
}
System.out.println(" Message: " + messageContent);
Upvotes: 6