我使用sendgrid發送電子郵件,它使用下面的代碼 ,但它沒有附件正常工作。SendGrid電子郵件API,發送電子郵件附件
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("[email protected]");
String subject = "Hello World from the SendGrid Java Library!";
Email to = new Email("[email protected]");
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,以及由於某種原因沒有的javadoc,但有一個例子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("[email protected]");
String subject = "Hello World from the SendGrid Java Library!";
Email to = new Email("[email protected]");
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);
}
}
}
FYI:使用從sendgrid
你應該從你的代碼示例 – IcedDante
拔出鑰匙是感謝@IcedDante我已經把隨機的不是真正的一個我會重新隨機任何方式 – shareef