SendGrid emailing API , send email attachment

帅比萌擦擦* 提交于 2019-12-05 01:40:23
shareef

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.jarand itsencodeAsString` 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.

This is the way you can send attachments using SendGrid API.

Mail mail = createEmail();
    Attachments attachments = new Attachments();
    Base64 x = new Base64();
    String encodedString = x.encodeAsString(loadPdfFromClasspath());
    attachments.setContent(encodedString);
    attachments.setDisposition("attachment");
    attachments.setFilename("xyx.pdf");
    attachments.setType("application/pdf");
    mail.addAttachments(attachments);


try {
        request.method = com.sendgrid.Method.POST;
        request.endpoint = "mail/send";
        request.body = mail.build();
        // Uncomment once connectivity with sendgrid is resolved
        Response response = sg.api(request);

}catch (IOException ex) {
        throw ex;
    }

It works for me (the latest maven version SendGrid Java » 4.4.1):

        import com.sendgrid.helpers.mail.objects.Attachments;
        import com.sendgrid.helpers.mail.objects.Content;
        import com.sendgrid.helpers.mail.objects.Email;
        import com.sendgrid.helpers.mail.Mail;

        ......

        Content content = new Content("text/html", body);
        Mail mail = new Mail(from, subject, to, content);
        Path file = Paths.get(filePath);
        Attachments attachments = new Attachments();
        attachments.setFilename(file.getFileName().toString());
        attachments.setType("application/pdf");
        attachments.setDisposition("attachment");

        byte[] attachmentContentBytes = Files.readAllBytes(file);
        String attachmentContent = Base64.getMimeEncoder().encodeToString(attachmentContentBytes);
        attachments.setContent(attachmentContent);
        mail.addAttachments(attachments);

        SendGrid sg = new SendGrid(apiKey);
        Request request = new Request();

        request.setMethod(Method.POST);
        request.setEndpoint("mail/send");
        request.setBody(mail.build());
        Response response = sg.api(request);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!