SendGrid emailing API , send email attachment

前端 未结 3 1159
你的背包
你的背包 2021-01-01 20:15

Am using sendgrid to send emails and it works fine using the following code but its without attachment.

package sendgrid;

import com.sendgrid.Content;
impor         


        
3条回答
  •  独厮守ぢ
    2021-01-01 20:32

    When i executed the code i got the following message in logs in netbeans

     202
     
     {X-Frame-Options=DENY, Server=nginx, Connection=keep-alive,
     X-Message-Id=vqVw2RtUShSVQ_ymVEVqaw, Content-Length=0, Date=Tue, 26
     Jul 2016 20:05:54 GMT, Content-Type=text/plain; charset=utf-8}
    

    The trick to solve the issue is to encode the attachment using commons apache codec commons-codec-1.8.jar and its encodeAsString method from package

    org.apache.commons.codec.binary.Base64

    Attachments attachments3 = new Attachments();
    Base64 x = new Base64();
    String imageDataString = x.encodeAsString(fileData);
    attachments3.setContent(imageDataString);
    attachments3.setType("image/png");//"application/pdf"
    attachments3.setFilename("x.png");
    attachments3.setDisposition("attachment");
    attachments3.setContentId("Banner");
    mail.addAttachments(attachments3);
    

    Even the content-length was retruned as 0 in response it worked.

提交回复
热议问题