繁体   English   中英

使用带 Java 的 SendGrid 发送电子邮件

[英]Sending Email using SendGrid with java

我是软件开发的新手,一直在尝试用 Java 开发一个应用程序,以使用 SendGrid 发送多内容电子邮件(纯文本和 html 文件),但我只获取 HTML 文件。 请帮我解决这个问题。 这是我的代码。

Email from = new Email("sebatti20@gmail.com");
String subject = "Sending with SendGrid is Fun";
Content content = new Content();
content.setType("text/plain");
content.setValue("This is a simple text");
content.setType("text/html");
content.setValue("This is an HTML text");
Personalization personalization = new Personalization();
Email to = new Email();
to.setEmail("sabbyelavumkal@gmail.com");
personalization.addTo(to);
Email to2 = new Email();
to2.setEmail("jojimathew.mec@gmail.com");
personalization.addTo(to2);
Email Cc = new Email();
Cc.setEmail("sebastianthomas.mec@gmail.com");
personalization.addCc(Cc);
Mail mail = new Mail();
mail.setFrom(from);
mail.setSubject(subject);
mail.addContent(content);
mail.addPersonalization(personalization);
endGrid sg = new SendGrid("SENDGRID API");
Request request = new Request();
try {
    request.setMethod(Method.POST);
    request.setEndpoint("mail/send");
    request.setBody(mail.build());
    Response response = sg.api(request);
    System.out.println(response.getStatusCode());
    System.out.println(response.getBody());
    System.out.println(response.getHeaders());
} catch (IOException ex) {
    throw ex;
}

您使用 HTML 覆盖文本内容。
如果你想要两者,你应该这样写:

Content plainContent = new Content("text/plain", "This is a simple text");
Content htmlContent = new Content("text/html", "This is an HTML text");
mail.addContent(plainContent);
mail.addContent(htmlContent);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM