Outlook 中的 Javax Mail 无标题附件
Javax Mail Untitled attachment in Outlook
我尝试制作一个 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;
}
}
我在封装 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);
在这次更改之后,一切正常。
我尝试制作一个 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;
}
}
我在封装 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);
在这次更改之后,一切正常。