错误:阅读电子邮件内容时出现 javax.mail.internet.MimeMultipart
Posted
技术标签:
【中文标题】错误:阅读电子邮件内容时出现 javax.mail.internet.MimeMultipart【英文标题】:error : javax.mail.internet.MimeMultipart while reading the email content 【发布时间】:2015-08-26 22:19:27 【问题描述】:我有以下代码来使用 Java 阅读 Gmail 电子邮件。如果电子邮件包含非常简单的纯文本消息,那么代码工作正常,我能够看到(正文)的内容正确显示。
但是,对于某些电子邮件,我在尝试时会看到以下消息
显示正文(内容:javax.mail.internet.MimeMultipart@2038ae61)
。我发现很难解决这个问题。请帮帮我。
代码:
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", "username@gmail.com",
"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();
【问题讨论】:
this 回答可能会提供一些见解 您好,感谢您指出帖子。显然,该帖子指向解释电子邮件发送的帖子。我的问题是在阅读电子邮件内容时。 【参考方案1】:这对我有用
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);
【讨论】:
【参考方案2】:这是一个完整的文件附件阅读解决方案:
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));
);
【讨论】:
以上是关于错误:阅读电子邮件内容时出现 javax.mail.internet.MimeMultipart的主要内容,如果未能解决你的问题,请参考以下文章
Java Mail:在没有 SSL 的端口 25 上发送电子邮件时出现 SSLHandshakeException
尝试连接到 gmail IMAP 服务器时出现 javax.mail.MessagingException
通过 SMTP 使用 AWS SES 发送电子邮件,错误 421