インラインイメージを使用して電子メールを送信することです。イメージが電子メールに表示されないことを除いて、すべてがうまくいきます。Mailgun API:SpringのRestTemplateでインラインイメージを送信する
私のアプローチは、このジャージーの例であるMailgun's User Guideに基づいています。
public static ClientResponse SendInlineImage() {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("api",
"YOUR_API_KEY"));
WebResource webResource =
client.resource("https://api.mailgun.net/v3/YOUR_DOMAIN_NAME" +
"/messages");
FormDataMultiPart form = new FormDataMultiPart();
form.field("from", "Excited User <[email protected]_DOMAIN_NAME>");
form.field("to", "[email protected]");
form.field("subject", "Hello");
form.field("text", "Testing some Mailgun awesomness!");
form.field("html", "<html>Inline image here: <img src=\"cid:test.jpg\"></html>");
File jpgFile = new File("files/test.jpg");
form.bodyPart(new FileDataBodyPart("inline",jpgFile,
MediaType.APPLICATION_OCTET_STREAM_TYPE));
return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).
post(ClientResponse.class, form);
}
しかし、私はSpringのRestTemplateを使用する必要があります。
RestTemplate template = new RestTemplate();
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
// ... put all strings in map (from, to, subject, html)
HttpHeaders headers = new HttpHeaders();
// ... put auth credentials on header, and content type multipart/form-data
template.exchange(MAILGUN_API_BASE_URL + "/messages", HttpMethod.POST,
new HttpEntity<>(map, headers), String.class);
残りの部分は、マップに*.png
ファイルを置くことです:
これは私がこれまで持っているものです。それをどうするかわからない。 ServletContextResource#getInputStream経由ですべてのバイトを読み込もうとしましたが、成功しませんでした。結果の電子メールにイメージが表示されません。
ここに何か不足していますか?