[英]I want to embed an excel(xls/xlsx) file in an email body(not attachment)
I want to embed an excel(xls/xlsx) file in an email body(not attachment). 我想在电子邮件正文(而非附件)中嵌入excel(xls / xlsx)文件。 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). 您将需要解析xls / xlsx文件( Apache POI是一个很好的库)并将其呈现为HTML(并将邮件的内容类型设置为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. 您不能依赖于另一个电子邮件客户端可以处理Microsoft文档的假设,而图像是非常常见的格式,并且大多数客户端都支持图像。 The image is displayed because Gmail takes the attachment and puts it in the mail (which is then displayed as HTML mail). 之所以显示该图片,是因为Gmail接收了该附件并将其放入邮件(然后显示为HTML邮件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.