Javax Mail Outlook中的无标题附件

时间:2016-11-16 08:04:59

标签: java outlook javamail

我尝试制作一个java邮件发送应用程序,但是当我添加一个.docx附件时Outlook显示: " Untitled attachment xxxx.docx" 我的代码:

MimeMessage message = new MimeMessage(session); // Message
message.setHeader("Content-Type", "text/html; charset=UTF-8");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, address);
message.setSubject(subject, "UTF-8");
Multipart multipart = new MimeMultipart(); // body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, "text/html; charset=utf-8"); //html body text
multipart.addBodyPart(messageBodyPart); // body add html text
if(file != null) {
    for (File f : file) {
        MimeBodyPart attachPart = new MimeBodyPart();
        attachPart.attachFile(f.getAbsolutePath(), MimeTypeUtils.getContentTypeByFileName(f.getName()), "base64");
//                attachPart.setFileName(f.getName());
        multipart.addBodyPart(attachPart); // body add attachment
    }
}
message.setContent(multipart); // message add body content
Transport.send(message);

只有少量.docx文件存在问题,但它很烦人。 有人可以帮帮我吗? " getContentTypeByFileName":

public class MimeTypeUtils {
    private static final Map<String, String> fileExtensionMap;
    static {
        fileExtensionMap = new HashMap<>();
        fileExtensionMap.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    }
    public static String getContentTypeByFileName(String fileName) {
        FileNameMap mimeTypes = URLConnection.getFileNameMap();
        String contentType = "";
        contentType = mimeTypes.getContentTypeFor(fileName);
        if (contentType == null || contentType.isEmpty()) {
            String extension = FilenameUtils.getExtension(fileName);
            contentType = fileExtensionMap.get(extension);
        }
        return contentType;
    }
}

1 个答案:

答案 0 :(得分:0)

我在封装MimeMesage时遇到问题。我以前的解决方法:

MimeMessage { // the whole message
    Multipart{
        BodyPart // here is the body
        MimeBodyPart //this is the attachment
    }
}

但是正确的方法是:

MimeMessage {
    Multipart {
        MimeBodyPart {
            MimeBodyPart // plain text of body
            MimeBodyPart // html text of body
        }
        MimeBodyPart // attachment
        MimeBodyPart // attachment2 ...
    }
}

代码示例:

MimeMessage message = new MimeMessage(session);
message.addHeader("Content-Type", "multipart/mixed; charset=UTF-8");

Multipart multipart = new MimeMultipart("alternative");

MimeBodyPart messageBodyPartPlain = new MimeBodyPart();
messageBodyPartPlain.setContent("Plain body text", "text/plain; charset=utf-8");
multipart.addBodyPart(messageBodyPartPlain);

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("Html body text", "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);

MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(multipart);

Multipart mp2 = new MimeMultipart("mixed");
mp2.addBodyPart(mbp);

for (Attachment a : attachments) {
    if (a != null && a.getFile() != null) {
        MimeBodyPart mbp2 = new MimeBodyPart();
        FileDataSource fds = new FileDataSource(a.getFile()) {
            public String getContentType() {
                return MimeTypeUtils.getContentTypeByFileName(a.getName());
            }
        };
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(a.getName());
        mp2.addBodyPart(mbp2);
    }
}

message.setContent(mp2);

更改之后,一切正常。