我正在使用 apache commons email 1.4 发送带有附件 pdf 的 html 电子邮件。但是,我收到的电子邮件是
<p>B600003292</p>
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_407006327.1513053130072"
------=_Part_0_407006327.1513053130072
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
<html><p>asdfb B600003292</p></html>
------=_Part_0_407006327.1513053130072
Content-Type: application/octet-stream; name=eee1_12122017043204284.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=eee1_12122017043204284.pdf
JVBERi0xLjQKJeLjz9MKMyAwIG9iaiA8PC9MZW5ndGggMTM0L0ZpbHRlci9GbGF0ZURlY29kZT4+
c3RyZWFtCnicK+RyCuEyUDC3NFIISeFyDeEK5CrkMtAzMDY3VSjnMlLwAkpmcRkaKPhyRccaKKRw
mZoomBsZKORymZpagFk5UJaegQmIbYLKBMlncIVz5QHNAcGidJCF+m6GCoZAG9O4DMGihgpAxWaW
pnoWhgohuVwaxYkpaSCsGZIFc1MgFwCOPyOjCmVuZHN0cmVhbQplbmRvYmoKMSAwIG9iajw8L1Bh
cmVudCA0IDAgUi9Db250ZW50cyAzIDAgUi9UeXBlL1BhZ2UvUmVzb3VyY2VzPDwvUHJvY1NldCBb
L1BERiAvVGV4dCAvSW1hZ2VCIC9JbWFnZUMgL0ltYWdlSV0vRm9udDw8L0YxIDIgMCBSPj4+Pi9N
ZWRpYUJveFswIDAgNjEyIDc5Ml0+PgplbmRvYmoKMiAwIG9iajw8L0Jhc2VGb250L1RpbWVzLVJv
bWFuL1R5cGUvRm9udC9FbmNvZGluZy9XaW5BbnNpRW5jb2RpbmcvU3VidHlwZS9UeXBlMT4+CmVu
ZG9iago0IDAgb2JqPDwvVHlwZS9QYWdlcy9Db3VudCAxL0tpZHNbMSAwIFJdPj4KZW5kb2JqCjUg
MCBvYmo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgNCAwIFI+PgplbmRvYmoKNiAwIG9iajw8L1Byb2R1
Y2VyKGlUZXh0IDIuMS4wIFwoYnkgbG93YWdpZS5jb21cKSkvTW9kRGF0ZShEOjIwMTcxMjExMjMz
MjEwLTA1JzAwJykvQ3JlYXRpb25EYXRlKEQ6MjAxNzEyMTEyMzMyMTAtMDUnMDAnKT4+CmVuZG9i
agp4cmVmCjAgNwowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAyMTYgMDAwMDAgbiAKMDAwMDAw
MDM3MiAwMDAwMCBuIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDA0NjEgMDAwMDAgbiAKMDAw
MDAwMDUxMSAwMDAwMCBuIAowMDAwMDAwNTU1IDAwMDAwIG4gCnRyYWlsZXIKPDwvUm9vdCA1IDAg
Ui9JRCBbPGI0NzlhYjhiZWY0ZmM4Y2RhMWVjZDZjMzM2NTM0NDFhPjxlMjU1M2M4Y2UyMGM3ODk2
NzkxYmZjZGQzYjc5YzJhZj5dL0luZm8gNiAwIFIvU2l6ZSA3Pj4Kc3RhcnR4cmVmCjY4NgolJUVP
Rgo=
------=_Part_0_407006327.1513053130072--
我在上述电子邮件中有三个问题。
1)<p>B600003292</p>
应该是主语的一部分,应该是“ title is <p>B600003292</p>
”。但是,现在,主题是“title is”,并且如您所见<p>B600003292</p>
,显示为正文的第一行。如何使主题为 html 格式?
2) 它有太多意想不到的内容被写入电子邮件正文,而我想要的实际正文内容“ <html><p>asdfb B600003292</p></html>
”是这种无意义的一部分,也没有被转移到 html 格式的正文中。
3) 没有附件。我相信底部的加密代码是附件。不知道为什么它被显示为正文内容而不是 pdf。
下面是我的代码。
public void send(EmailType emailType) throws EmailException, MalformedURLException {
org.apache.commons.mail.Email email;
if (emailType == EmailType.SimpleEmail) {
email = new SimpleEmail();
} else if (emailType == EmailType.MultiPartEmail) {
email = new MultiPartEmail();
loadEmailAttachments(email);
} else {
email = new HtmlEmail();
loadEmailAttachments(email);
}
email.setHostName(host);
email.setSmtpPort(Integer.valueOf(port));
//email.setAuthenticator(new DefaultAuthenticator(null, null));//.setAuthentication(new ());
email.setCharset("utf-8");
email.setFrom(from.getEmailAddress(), from.getDisplayName());
for (TemplateEngineEmailer receiver : to) {
email.addTo(receiver.getEmailAddress(), receiver.getDisplayName());
}
email.setSubject(subject);
if (emailType == EmailType.HtmlEmail) {
((HtmlEmail) email).setHtmlMsg(body);
} else {
email.setMsg(body);
}
email.send();
}
private void loadEmailAttachments(org.apache.commons.mail.Email email) throws EmailException, MalformedURLException {
List<EmailAttachment> attachments = getEmailAttachment();
if (attachments != null && !attachments.isEmpty()) {
for (EmailAttachment attachment : attachments) {
((HtmlEmail) email).attach(attachment);
}
}
}
private List<EmailAttachment> getEmailAttachment() throws MalformedURLException {
List<EmailAttachment> emailAttachments = new ArrayList<>();
for (TemplateEngineAttachment attachment : attachments) {
EmailAttachment emailAttachment = new EmailAttachment();
if (attachment.isLocalFile()) {
emailAttachment.setPath(attachment.getPath());
} else {
emailAttachment.setURL(new URL(attachment.getPath()));
}
emailAttachment.setDisposition(EmailAttachment.ATTACHMENT);
emailAttachment.setDescription(attachment.getDescription());
emailAttachment.setName(attachment.getName());
emailAttachments.add(emailAttachment);
}
return emailAttachments;
}