gpt4 book ai didi

java - 通过电子邮件将 Excel 文件(在内存中)作为 Google App Engine 上的附件发送

转载 作者:行者123 更新时间:2023-12-01 15:43:12 25 4
gpt4 key购买 nike

我用谷歌搜索并尝试了几种方法但都失败了。问题是:我尝试创建一个 Excel 文件(使用 JExcelApi)并将其作为 Google 应用引擎上的附件通过电子邮件发送。

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

try {
//write the excel file
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
//first sheet
WritableSheet sheet = workbook.createSheet("Param", 0);
Label label11 = new Label(0, 0, "parameter is");
sheet.addCell(label11);
Label label12 = new Label(1, 0, "worker");
sheet.addCell(label12);

//second sheet
WritableSheet sheet2 = workbook.createSheet("Info", 1);
Label label21 = new Label(0, 0, "Info is");
sheet2.addCell(label21);
Label label22 = new Label(1, 0, "consumer");
sheet2.addCell(label22);

workbook.write();
workbook.close();

//email the excel file as an attachment
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("//my gmail", "Sr."));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("//my gmail", "Mr. "));
msg.setSubject("Your excel file is here");

Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("please review", "text/html");
mp.addBodyPart(htmlPart);

MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("report.xls");
attachment.setContent(outputStream.toByteArray(), "application/vnd.ms-excel");
mp.addBodyPart(attachment);

msg.setContent(mp);
Transport.send(msg);

} catch (RowsExceededException e) {
/* foo */
}
}

我已上传,但从未将带有 Excel 附件的邮件发送到我的邮箱。outputStream.toByteArray() 可能不正确,但我尝试的其他方法也不起作用。

最佳答案

解决了。但是有人能解释一下吗?因为谷歌应用引擎的一些限制?谢谢。

DataSource src = new ByteArrayDataSource(outputStream.toByteArray()
, "application/vnd.ms-excel");

attachment.setDataHandler(new DataHandler(src));

关于java - 通过电子邮件将 Excel 文件(在内存中)作为 Google App Engine 上的附件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7655984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com