简体   繁体   English

我想将excel(xls / xlsx)文件嵌入电子邮件正文(而非附件)中

[英]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.

相关问题 将 Excel 文件 (xls,xlsx) 转换为 PDF - Convert an excel file (xls,xlsx) for PDF 如何在Java 1.3中将.xlsx格式的Excel文件转换为.xls? - How can i convert excel file of format .xlsx to .xls in Java 1.3? 无法发送HTML邮件正文以及xls文件作为附件 - Not able to send html mail body along with xls file as attachment 如何在Java中转换Excel文件格式XLS和XLSX反之亦然 - How to convert the excel file format xls and xlsx vice versa in java 如何在 android 工作室中从本地读取 excel 文件 xls 和 xlsx 我还需要使用带有行和列的集合执行搜索 - How to read excel file xls and xlsx from local in android studio also I need to perform search with set with row and cols 如何使用Java读取Spark中的xls和xlsx文件? - how can I read xls and xlsx file in spark with java? 如何在不下载附件文件的情况下获取电子邮件正文 - How do I can fetch the email body without downloading the attachment file 更新.xls或.xlsx文件中的数据 - Update data in .xls or .xlsx file 在talend中将XLS文件转换为XLSX - Convert XLS file to XLSX in talend 我想使用 java(XSSF 格式)从扩展名为“.xlsx”的 Excel 文件中复制特定列 - I want to copy specific columns from Excel file with extension, “.xlsx” using java (XSSF format)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM