I want to embed an excel(xls/xlsx) file in an email body(not attachment). I tried the below code, but its coming as an attachment. When I try with an image, its coming in the body. Below is the code.
class SendEmail {
public SendEmail() {
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("Filepath");
attachment.setDisposition(EmailAttachment.INLINE);
attachment.setDescription("Excel File");
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.google.com");
email.setSmtpPort(465);
email.setAuthenticator((javax.mail.Authenticator) new DefaultAuthenticator("Username", "Password"));
email.setSSL(true);
try {
email.addTo("ToAddress");
email.setFrom("FromAddress");
email.setSubject("Attached Mail Test");
// add the attachment
email.attach(attachment);
email.setTLS(true);
// send the email
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SendEmail();
}
You would need to parse the xls/xlsx file ( Apache POI is a good library for that) and render it to HTML (and set the content type of the mail to text/html). You can't rely on the assumption that another email client can handle Microsoft documents, while images are pretty common formats and supported by most clients. The image is displayed because Gmail takes the attachment and puts it in the mail (which is then displayed as HTML mail).
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.