通过电子邮件将 Excel 文件(在内存中)作为 Google App Engine 上的附件发送
我用谷歌搜索并尝试了几种方法但都失败了。 问题是:我尝试创建一个 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()
可能不正确,但我尝试的其他方法也不起作用。
I googled and tried several approaches but failed.
Here is the question: I try to create a excel file (using JExcelApi) and email it as an attachment on google app engine.
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 */
}
}
I uploaded but never a mail with the attached excel was sent to my mailbox.outputStream.toByteArray()
may not be proper, but others I tried didn't work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。但是有人能解释一下吗?因为谷歌应用引擎的一些限制?谢谢。
Solve it.But anybody can explain it? Because some restriction of google app engine? Thanks.