'Sending InputStreamSource as attachment with mailGun API in java
I am trying to send an email with attachments, My attachments are stored in the AWS, I download them and store them in HashMap<String,InputStreamSource>: Below is my code :
when I send the Email the mail doesnot get delivered, I don't any error message as well. Please suggest if there is any work around.
public void sendSimpleMessageWithAttachment(String from, String to, String subject, String content, boolean isHtml, Map<String, InputStreamSource> attachments) throws UnirestException {
// Prepare message using a Spring helper
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, CharEncoding.UTF_8);
attachments.forEach((k, v) -> {
try {
message.addAttachment(k, v);
} catch (MessagingException e) {
log.warn("Attachment could not be added to the email '{}', '{}'", subject, k);
}
});
HttpResponse<JsonNode> request = Unirest.post("https://api.mailgun.net/v3/" + "DOMAIN.com" + "/messages.mime")
.basicAuth("api", "key-XXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.header("content-type", "multipart/form-data")
.field("from",from)
.field("to", to)
.field("subject", subject)
.field("html", content)
.field("message",message)
.asJson();
log.info("Response : Message" +String.valueOf(request.getBody()));
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.warn("Email could not be sent to user '{}'", to, e);
} else {
log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
}
}
}
Solution 1:[1]
You can get your file in byte[]
from aws then you can use:
HttpResponse<JsonNode> request = Unirest.post("https://api.mailgun.net/v3/" + "DOMAIN.com" + "/messages.mime")
.basicAuth("api", "key-XXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.header("content-type", "multipart/form-data")
.field("from",from)
.field("to", to)
.field("subject", subject)
.field("html", content)
.field("attachment", new ByteArrayResource(byteArrayVariableName))
.asJson();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |