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

我是软件开发的新手,一直在尝试用 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;

}


江户川乱折腾
浏览 479回答 1
1回答

哔哔one

您使用 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);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java