Создать цепочку писем в SendGrid (Java API)
Есть потребность, чтобы приходящие сервисные письма собирались в цепочки. В MailGun, это делалось добавлением в хедер вот так
MailBuilder mailBuilder = Mail.using(configuration)
.subject(subject)
.html(html);
mailBuilder.parameter("h:Message-ID", "<" + msgId + "@" + configuration.domain() + ">");
mailBuilder.parameter("h:In-Reply-To", "<" + replyMsgId + "@" + configuration.domain() + ">");
mailBuilder.parameter("h:References", "<" + replyMsgId + "@" + configuration.domain() + ">");
но в SendGrid подобный подход не работает, письма приходят но в цепочки не собираются. документацию к api перечитал вдоль и поперек но ничего по данному вопросу не нашел.
на всякий случай вот код,если покажете как и куда воткнуть параметры для сбора цепочек, буду крайне благодарен!
@Service
public class SendGridService {
private static final Logger LOG = LoggerFactory.getLogger(MailgunService.class);
private String apiKey;
private Address from;
private Address replyTo;
@Autowired
private AsyncExecutor asyncExecutor;
@Autowired
private AppProperties appProperties;
@PostConstruct
public void init() {
configure();
}
public void configure() {
apiKey = appProperties.getSendgrid().getApiKey();
from = new Address(appProperties.getSendgrid().getFrom().getName(), appProperties.getSendgrid().getFrom().getEmail());
replyTo = new Address(appProperties.getSendgrid().getReplyTo().getName(), appProperties.getSendgrid().getReplyTo().getEmail());
}
public CompletableFuture<Response> sendEmail(String jsonString, List<FileWithMeta> attachments) {
Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap();
return CompletableFuture.supplyAsync(() ->
{
MDC.setContextMap(copyOfContextMap);
Response result = null;
try {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, (certificate, authType) -> true).build();
CloseableHttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
SendGrid sg = new SendGrid(apiKey, new Client(client));
Mail mail = new Mail();
// Добавляем файлы
attachments.forEach(file -> {
String base64String = Base64.getEncoder().encodeToString(file.getFile());
Attachments attachment = new Attachments();
attachment.setContent(base64String);
attachment.setType(file.getMimeType());
attachment.setFilename(file.getFileName() + "." + file.getExtension());
attachment.setDisposition("attachment");
mail.addAttachments(attachment);
}
);
ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.reader();
JsonNode node = reader.readTree(jsonString);
ObjectNode objectNode = (ObjectNode) node;
objectNode.put("from", reader.readTree(mapper.writeValueAsString(from)));
objectNode.put("reply_to", reader.readTree(mapper.writeValueAsString(replyTo)));
if (mail.getAttachments() != null) {
objectNode.put("attachments", reader.readTree(mapper.writeValueAsString(mail.getAttachments())));
}
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(node.toString());
result = sg.api(request);
} catch (Throwable e) {
throw new RuntimeException(e);
}
return result;
}, asyncExecutor.getExecutor());
}
}