我是軟件開發的新手,一直在嘗試用 Java 開發一個應用程序,以使用 SendGrid 發送多內容電子郵件(純文本和 html 文件),但我只獲取 HTML 文件。請幫我解決這個問題。這是我的代碼。Email from = new Email("[email protected]");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("[email protected]");personalization.addTo(to);Email to2 = new Email();to2.setEmail("[email protected]");personalization.addTo(to2);Email Cc = new Email();Cc.setEmail("[email protected]");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;}
1 回答

嗶嗶one
TA貢獻1854條經驗 獲得超8個贊
您使用 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);
添加回答
舉報
0/150
提交
取消