I have a service that sends email and everything works fine there, but I need to send 2 types of emails text/html
and text/plain
. I add this to my code:
Content plainContent = new Content("text/plain", "This is plain content");
mail.addContent(htmlContent);
mail.addContent(plainContent);
and the letters stopped coming
@Override
public void send(Message message) throws IOException {
Personalization personalization = new Personalization();
message.getSendTo().forEach(address -> personalization.addTo(new Email(address.getEmail(), address.getName())));
Content htmlContent = new Content(message.getContentType(), message.getContent());
Mail mail = new Mail();
mail.setFrom(new Email(message.getSendFrom().getEmail(), message.getSendFrom().getName()));
mail.addPersonalization(personalization);
mail.setSubject(message.getSubject());
mail.addContent(htmlContent);
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sendgrid.api(request);
if (response.getStatusCode() != 202) {
throw new RuntimeException(response.getBody());
}
}
I found the answer to my question:
{"errors":[{"message":"If present, text/plain must be first, followed by text/html, followed by any other content.","field":"content" ,"help":null}]}
It is necessary to simply swap setContent
.
First mail.addContent(plainContent);
and then mail.addContent(htmlContent);