SendGrid电子邮件API,发送电子邮件附件

编程入门 行业动态 更新时间:2024-10-22 17:33:29
本文介绍了SendGrid电子邮件API,发送电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用sendgrid发送电子邮件,并且使用以下代码可以正常工作但没有附件.

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

package sendgrid; import com.sendgrid.Content; import com.sendgrid.Email; import com.sendgrid.Mail; import com.sendgrid.Method; import com.sendgrid.Request; import com.sendgrid.Response; import com.sendgrid.SendGrid; import java.io.IOException; public class SendEmail { public static void main(String[] args) throws IOException { Email from = new Email("test@example"); String subject = "Hello World from the SendGrid Java Library!"; Email to = new Email("shareef@gmail"); Content content = new Content("text/plain", "Hello, Email!"); Mail mail = new Mail(from, subject, to, content); SendGrid sg = new SendGrid("SG.rIEh84OgQBybYEJcOMie1wd.AZqqdWNYXbOqTarUJcG-iSg0UtHJtCto4oe6tVzn6es"); Request request = new Request(); try { request.method = Method.POST; request.endpoint = "mail/send"; request.body = mail.build(); Response response = sg.api(request); System.out.println(response.statusCode); System.out.println(response.body); System.out.println(response.headers); } catch (IOException ex) { throw ex; } } }

但是我需要发送附件,因此我搜索了github源和Web文档API,由于某种原因,没有javadocs,但是有一个示例 GitHub sendgrid ,所以我一直在尝试直到可行,我缩小了一些例外,响应代码,起初我是被禁止的未经授权,最好是响应202,表示有效且已排队(在此处检查),无论如何,这是我的代码发送电子邮件和附件的方法,但是当您打开附件时,附件的大小为零,并说无法打开或预览文件!

But what i need is to send attachments with it so i searched github source and the web documentation API , and for some reason there is no javadocs but there was an example GitHub sendgrid so am trying until it works , i narrowed down some exceptions and response code , at first i was getting unauthorized the forbidden and it got better to response 202 , means valid and queued (check here) any way here is my code that dose send an email and with attachments but when you open the attachment its zero size and says cannot open or preview the file !

package sendgrid; import com.sendgrid.Attachments; import com.sendgrid.Content; import com.sendgrid.Email; import com.sendgrid.Mail; import com.sendgrid.MailSettings; import com.sendgrid.Method; import com.sendgrid.Request; import com.sendgrid.SendGrid; import com.sendgrid.Setting; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class SendEmailAttachmentV2 { public static void main(String[] args) throws IOException { sendmail(); } // Fully populated Mail object public static void sendmail() throws IOException { com.sendgrid.Response response1; Email from = new Email("shareef@gmail"); String subject = "Hello World from the SendGrid Java Library!"; Email to = new Email("shareef@gmail"); Content content = new Content("text/plain", "Hello, Email!"); Mail mail = new Mail(from, subject, to, content); File file = new File("C:\\x.png"); byte[] fileData = null; try { fileData = org.apachemons.io.IOUtils.toByteArray(new FileInputStream(file)); } catch (IOException ex) { } Attachments attachments3 = new Attachments(); attachments3.setContent(new String(fileData, 0, (int) file.length(), "UTF-8")); attachments3.setType("image/png");//"application/pdf" attachments3.setFilename("x.png"); attachments3.setDisposition("attachment"); attachments3.setContentId("Banner"); mail.addAttachments(attachments3); MailSettings mailSettings = new MailSettings(); Setting sandBoxMode = new Setting(); sandBoxMode.setEnable(true); mailSettings.setSandboxMode(sandBoxMode); SendGrid sg = new SendGrid("SG.1Hg78VK0TJ6kexUnByZUYg.LAa5A4GufssZ9lpPQdV6PcZCY6SZ9Xq6LvqfMRG0wesKw"); Request request1 = new Request(); try { request1.method = Method.POST; request1.endpoint = "mail/send"; request1.body = mail.build(); response1 = sg.api(request1); System.out.println(response1.statusCode); System.out.println(response1.body); System.out.println(response1.headers); } catch (IOException ex) { System.out.println(ex); } } }

仅供参考:使用从sendgrid控制台生成的生成的API密钥

FYI: use your generated API key that generated from the console of sendgrid

推荐答案

执行代码时,我在netbeans的日志中收到以下消息

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}

解决此问题的技巧是使用Common apache编解码器对附件进行编码包中的commons-codec-1.8.jar 及其 encodeAsString 方法

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.apachemons.codec.binary.Base64

org.apachemons.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);

即使响应,有效,内容长度也被重新设置为0.

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

更多推荐

SendGrid电子邮件API,发送电子邮件附件

本文发布于:2023-11-06 06:35:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送电子邮件   附件   电子邮件   SendGrid   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!