SendGrid emailing API , send email attachment

前端 未结 3 1161
你的背包
你的背包 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:35

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

提交回复
热议问题