简体   繁体   中英

Apache commons-mail HtmlEmail with attachment: html is attached as noname.html in Gmail

I use the apache commons-mail (v1.3.2) to send an order confirmation e-mail with a PDF as attachment. The e-mail displays without problems in outlook (both web and desktop) but when I send to my gmail account, the content of the mail is empty and the Html content is attached in a separate file "noname.html".

My code:

       // Create mail instance using commons-mail and the hybris MailUtility class.
        HtmlEmail htmlEmail = (HtmlEmail) MailUtils.getPreConfiguredEmail(); // creates a mail instance with set mail
        htmlEmail.setCharset("UTF-8");
        htmlEmail.setHtmlMsg("this is <b>html text</b>);

        // Part two is attachment
            DataSource ds = new ByteArrayDataSource(mailAttachment.getData(), "application/pdf");
            htmlEmail.attach(ds, "attach.pdf", "generalconditions", EmailAttachment.ATTACHMENT);
        }

        //send mail
        htmlEmail.send();

At first, this issue occured also in outlook but I fixed this by switching from commons-mail v1.1 to v1.3.2. Still not fixed for gmail though...

You should use

EmailAttachment attachment = new EmailAttachment();
attachment.setPath(pdfFile.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);

Then you attach it to the email like this :

htmlEmail.attach(attachment);

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