gpt4 book ai didi

java - SendGrid emailing API , 发送邮件附件

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:54:04 24 4
gpt4 key购买 nike

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

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.com");
String subject = "Hello World from the SendGrid Java Library!";

Email to = new Email("shareef@gmail.com");
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 源代码和网络文档 API,由于某种原因没有 javadocs 但有一个例子 GitHub sendgrid所以我一直在努力,直到它起作用,我缩小了一些异常和响应代码的范围,起初我得到了未经授权的禁止,并且响应 202 变得更好,意味着有效和排队(check here)无论如何这是我发送的代码一封电子邮件和附件,但当您打开附件时,它的大小为零,并说无法打开或预览文件!

 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.com");
String subject = "Hello World from the SendGrid Java Library!";

Email to = new Email("shareef@gmail.com");
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.apache.commons.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 key

最佳答案

当我执行代码时,我在 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}

解决这个问题的诀窍是使用 commons apache 编解码器对附件进行编码 commons-codec-1.8.jar及其包中的 encodeAsString 方法

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

即使内容长度在响应中被重新设置为 0 它起作用了

关于java - SendGrid emailing API , 发送邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com