简体   繁体   中英

Apache commons-email 1.4 send HtmlEmail without translating html tag and also not see the attachment

I am using apache commons email 1.4 to send html email with attached pdf. However, the email I got is

<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--

I have three issues in above email.

1) <p>B600003292</p> should be part of the subject, which should be " title is <p>B600003292</p> ". However, right now, the subject is "title is", and as you can see <p>B600003292</p> was shown as the 1st line of the body. How can I make the subject to be html formatted?

2) It has so much unexpected content being written to the email body and the actual body content I want, " <html><p>asdfb B600003292</p></html> ", is part of this non-sense and also not being transferred to html formatted body.

3) There is no attachment. I believe the encrypted code at the bottom is the attachment. Not sure why it is being shown as body content instead of a pdf.

Below is my code.

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;
}

It turns out the cause ruins everything is the html tag <p> in the subject. Once I remove it, everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM