【问题标题】:how to sent gmail mail with CSV file attachment over google app engine如何通过谷歌应用引擎发送带有 CSV 文件附件的 gmail 邮件
【发布时间】:2012-04-02 09:53:50
【问题描述】:

我正在尝试通过谷歌应用引擎发送带有 csv 附件的电子邮件收件人没有收到邮件。当我检查 Google App Engine 中的日志时,也没有报告任何错误。可能出了什么问题?有人可以告诉我是否可以使用 Goog 应用引擎通过邮件将 csv 文件作为附件发送?如果是的话,能告诉我怎么做吗?

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

try {
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress("xxx@gmail.com"," Admin"));
         msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailto, "Mr. User"));
    msg.setSubject("Expence Tracker with Attachment");

    String htmlBody=msgbody;   

    byte[] attachmentData= attach.getBytes(); 
         Multipart mp = new MimeMultipart();

    MimeBodyPart htmlPart = new MimeBodyPart();
    htmlPart.setContent(htmlBody, "text/html");
    mp.addBodyPart(htmlPart);

    MimeBodyPart attachment = new MimeBodyPart();
    attachment.setFileName("myfile.csv");
    attachment.setContent(attachmentData, "text/comma-separated-values");
    mp.addBodyPart(attachment);

    msg.setContent(mp);

    //resp.getWriter().println("Mail  Details :To- "+emailto);

} catch (AddressException e) {

    resp.getWriter().println("Mail  Details :Error "+e);
} catch (MessagingException e) {
    resp.getWriter().println("Mail  Details :Error "+e);
}

【问题讨论】:

  • 我没有看到任何发送您创建的消息的方法,它在您的代码的另一部分吗?如果有,能不能也分享一下?

标签: java google-app-engine jakarta-ee


【解决方案1】:

这是发送 CSV 附件的示例。 CSV 位于 blobstore 中。 它是在 Python 中使用 GAE 邮件和 blobstore API 的。将其翻译成 Java 并不困难。

    blob_key = files.blobstore.get_blob_key(file_name)
    blob_info = blobstore.BlobInfo.get(blob_key)        
    blob_reader = blobstore.BlobReader(blob_key)                        
    message = mail.EmailMessage(sender = 'noreply@example.com, 
                                subject = 'CSV attached')        
    message.body = 'Download attached CSV'                       # only a text body
    message.attachments = [blob_info.filename,blob_reader.read()]
    message.send()  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2012-08-18
    • 2012-06-13
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多