当我尝试从Linux平台上的SMTP服务器读取电子邮件时,它会攻击Multipart Class强制转换异常但它在Windows平台上正常工作。请帮助我解决Linux平台上的主要问题。感谢
Email Content Type : multipart/mixed;
Email Attachment : boundary="doBdxOr1WFK/0oBG/sJf8iChfky6UG3OUxjg7g=="
Exception : javax.mail.util.SharedByteArrayInputStream cannot be cast to javax.mail.Multipart
Method Name : getContentFromMultipart
public class ReadEmail {
private static final String CONTENT_TYPE_TEXT_PLAIN = "text/plain";
private static final String CONTENT_TYPE_TEXT_HTML = "text/html";
private static final String CONTENT_TYPE_TEXT_CALENDAR = "text/calendar";
private static final String CONTENT_TYPE_MULTIPART_ALTERNATIVE = "multipart/alternative";
private static final String CONTENT_TYPE_MULTIPART_RELATED = "multipart/related";
private static final String CONTENT_TYPE_MULTIPART_MIXED = "multipart/mixed";
private boolean plainTextEmail;
private String contentType;
private String getContent(Message msg) throws Exception {
String emailContent = "";
try {
// if email content contains multiple content types/attachments
if (msg.getContent() instanceof Multipart) {
emailContent = this.getContentFromMultipart(msg);
} else if (msg.getContent() instanceof String) {
// if email content is only text based or html based
if (msg.getContentType().contains(this.CONTENT_TYPE_TEXT_PLAIN)) {
plainTextEmail = true;
contentType = this.CONTENT_TYPE_TEXT_PLAIN;
emailContent += msg.getContent().toString();
} else if (msg.getContentType().contains(this.CONTENT_TYPE_TEXT_HTML)) {
plainTextEmail = false;
contentType = this.CONTENT_TYPE_TEXT_HTML;
emailContent += msg.getContent().toString();
}
return emailContent;
}
// if email content is in the form of input string (Bain Capital
// link based emails)
else if (msg.getContent() instanceof InputStream) {
logger.log(msg.getContentType());
if (msg.getContentType().contains(this.CONTENT_TYPE_TEXT_CALENDAR)) {
contentType = this.CONTENT_TYPE_TEXT_CALENDAR;
}
if (msg.getContentType().contains("multipart/mixed")) {
contentType = "multipart/mixed"; // this.CONTENT_TYPE_MULTIPART_MIXED;
if (msg.isMimeType("multipart/*")) {
emailContent += emailContent = this.getContentFromMultipart(msg, this);
}
}
InputStream inputStream = (InputStream) msg.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
emailContent = out.toString();
inputStream.close();
reader.close();
}
} catch (Exception ex) {
emailContent = "exception";
}
return emailContent;
}
private String getContentFromMultipart(Message msg) throws IOException, MessagingException {
Object objectContent = msg.getContent();
Multipart mp = (Multipart) objectContent;
int count = mp.getCount();
String emailContent = "";
for (int i = 0; i < count; i++) {
if (mp.getBodyPart(i).getContentType().contains(this.CONTENT_TYPE_TEXT_HTML)) {
plainTextEmail = false;
contentType = this.CONTENT_TYPE_TEXT_HTML;
emailContent += mp.getBodyPart(i).getContent().toString();
} else if (mp.getBodyPart(i).getContentType().contains(this.CONTENT_TYPE_TEXT_PLAIN)) {
plainTextEmail = true;
contentType = this.CONTENT_TYPE_TEXT_PLAIN;
emailContent += mp.getBodyPart(i).getContent().toString();
} else if (mp.getBodyPart(i).getContentType().contains(this.CONTENT_TYPE_MULTIPART_ALTERNATIVE) || mp.getBodyPart(i).getContentType().contains(this.CONTENT_TYPE_MULTIPART_RELATED)
|| mp.getBodyPart(i).getContentType().contains(this.CONTENT_TYPE_MULTIPART_MIXED)) {
emailContent += parseMultipartEmailContent(mp.getBodyPart(i));
}
}
return emailContent;
}
}