简体   繁体   中英

how to sent gmail mail with CSV file attachment over google app engine

i am trying to send an e-mail with a csv attachment over google app engine The recipient didnt receive the mail. When I checked the logs in Google App Engine, no errors were reported either. What could be going wrong? Can someone please tell me if it is possible to send csv files over mail as attachments using Goog app engine? If yes, can you please tell me how to do it?

      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);
}

This is an example of sending a CSV attachment. The CSV is in the blobstore. It is in Python using the GAE Mail and blobstore API's. Translating this to Java will not be difficult.

    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()  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM