java使用javamail api在outllok中打开包含电子邮件作为附件的附加电子邮件
我能够使用javamail api在outlook中打开附加的电子邮件。 但我有一个问题,当这封附加的电子邮件还包含一个eml作为附件
在outlook中,如下图所示,主邮件(mail1左侧)包含mail2作为附件,当打开mail2时,我们将重新:完成作为附件(右侧)。
在我的应用程序中,我在特定模板中显示带有附件的mail1,当单击附件mail2时,我试图使用以下代码用outlook打开它
try {
//in case mMail.getPart(iPart) is attachment
if (mMail.getPart(iPart).isMimeType("Message/*")) {
MimePart mimePart = mMail.getPart(iPart);
mMail = new Mail(); // class where we set the server and a new MimeMessage
mMail.setAllHeaders(mimePart); //we add all headers
mMail.addAllBodyParts(mimePart);// check below
sCD = mimePart.getDisposition();
sCT = mimePart.getContentType();
if(sCD.startsWith(Part.ATTACHMENT)){
String filename = mimePart.getFileName();
filename = MimeUtility.decodeText(filename);
sCD = Part.ATTACHMENT + ";filename=" + filename;
}
iPart = -1;
bMsg = true;
}
}
catch (Exception e) {
e.printStackTrace();
}
//这里继续打开邮件的代码
public void addAllBodyParts(Part part) throws Exception {
Object content = part.getContent();
if (content instanceof String) {
mmMsg.setContent(content, part.getContentType());
} else if (content instanceof Multipart) {
Multipart innerMultiPart = (Multipart) content;
int count = innerMultiPart.getCount();
for (int i = 0; i < count; i++) {
BodyPart innerBodyPart = innerMultiPart.getBodyPart(i);
String sCT = innerBodyPart.getContentType();
if (sCT != null) {
String disposition = innerBodyPart.getDisposition();
if (disposition != null && (disposition.equals(Part.ATTACHMENT))) {
Multipart multipart = new MimeMultipart();
//multipart.addBodyPart(innerBodyPart);
DataHandler handler = innerBodyPart.getDataHandler();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(handler);
messageBodyPart.setFileName(innerBodyPart.getFileName());
messageBodyPart.setContent(innerBodyPart.getContent(), innerBodyPart.getContentType());
multipart.addBodyPart(messageBodyPart);
//innerMultiPart.addBodyPart(innerBodyPart);
mmMsg.setContent(multipart);
}else{
if(content instanceof MimeBodyPart){
MimeBodyPart mbp = (MimeBodyPart)content;
if (mbp.isMimeType("text/plain")) {
mmMsg.setContent(mbp.getContent(), sCT);
}
}else{
addAllBodyParts(innerBodyPart);
}
}
}else{
addAllBodyParts(innerBodyPart);
}
}
} else if (content instanceof MimeMessage){
MimeMessage msg = (MimeMessage) content;
addAllBodyParts(msg);
}
}
当mail2打开时,我得到了以下信息:(左侧,正文是空的,右侧,标题是空的,正文中的文本是错误的)
有人能帮忙找出错误所在吗。 谢谢!
# 1 楼答案
我不太确定你想用addAllBodyParts实现什么。这意味着你只是在创建一条与原始邮件内容相同的新邮件。现在还不清楚在创建新邮件后,您要如何处理它,但为什么不使用原始(附件)邮件对象呢
我不知道如何让Outlook显示一条消息,但可以使用writeTo方法将消息写入文件