The goal is to send an email with inline image. Everything is working well, except the image is not appearing in the email.
My approach is based on this Jersey-example of 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 <YOU#YOUR_DOMAIN_NAME>");
form.field("to", "baz#example.com");
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);
}
However, I need to use Spring's RestTemplate.
This is what I've got so far:
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);
The remaining part is to put the *.png file into the map. Not sure how to do that. Have tried reading all its bytes via ServletContextResource#getInputStream, but without success: Image is not appearing in the resulting e-mail.
Am I missing something here?
This turned out to be a case where everything was set up correctly, but only a small detail prevented it from working.
map.add("inline", new ServletContextResource(this.servletContext,
"/resources/images/email-banner.png"));
For Mailgun you need to use the map-key "inline". Also, the ServletContextResource has a method getFilename(), which is used to resolve against the image tag. Thus, the image tag should have the following content id:
<img src="cid:email-banner.png"/>
Related
I am trying to useDOLBY.IO's media API to transcode and save the output file to cloud. I have two URLs {URL1:input to dolby; URL2: to store output from dolby}. And both the URLs are signed URLs from the same cloud.
I tried using some java code to accomplish this but in the end I still can't get the result.
Here is the code:
#PostMapping("/transcode")
public String Video_Transcode1() throws IOException, JSONException {
OkHttpClient client = new OkHttpClient();
String data=generate_Access_token( );
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"inputs\":[{\"source\":\"https://vb-object-storage.ap-south-1.linodeobjects.com/The%20Hindu%20Daily%20News%20Analysis%20__%203rd%20July%202022%20__%20UPSC%20Current%20Affairs%20__%20Prelims%20%2722%20%26%20Mains%20%2722%28360%29.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220707T073322Z&X-Amz-SignedHeaders=host&X-Amz-Expires=604799&X-Amz-Credential=ZVADROBVHWLK1FOYT225%2F20220707%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Signature=0aa4b388ea3197dd8a03253f5f7313b4209b8acf5e0a4308dc5e543801d22c73\"}],\"outputs\":[{\"kind\":\"mp4\",\"destination\":\"https://vb-object-storage.ap-south-1.linodeobjects.com/The%20Hindu%20Daily%20News%20Analysis%20__%203rd%20July%202022%20__%20UPSC%20Current%20Affairs%20__%20Prelims%20%2722%20%26%20Mains%20%2722%28360%29.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220707T073322Z&X-Amz-SignedHeaders=host&X-Amz-Expires=604799&X-Amz-Credential=ZVADROBVHWLK1FOYT225%2F20220707%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Signature=0aa4b388ea3197dd8a03253f5f7313b4209b8acf5e0a4308dc5e543801d22c73\"}]}");
Request request = new Request.Builder()
.url("https://api.dolby.com/media/transcode")
.post(body)
.addHeader("Accept", "application/json")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization","Bearer "+data)
.build();
Response response = client.newCall(request).execute();
return response.toString();
}
Here the data is generated from another function (ie:Access token)
I have encoded two URLs as json here:
RequestBody body = RequestBody.create(mediaType, "{\"inputs\":[{\"source\":\"https://vb-object-storage.ap-south-1.linodeobjects.com/The%20Hindu%20Daily%20News%20Analysis%20__%203rd%20July%202022%20__%20UPSC%20Current%20Affairs%20__%20Prelims%20%2722%20%26%20Mains%20%2722%28360%29.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220707T073322Z&X-Amz-SignedHeaders=host&X-Amz-Expires=604799&X-Amz-Credential=ZVADROBVHWLK1FOYT225%2F20220707%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Signature=0aa4b388ea3197dd8a03253f5f7313b4209b8acf5e0a4308dc5e543801d22c73\"}],\"outputs\":[{\"kind\":\"mp4\",\"destination\":\"https://vb-object-storage.ap-south-1.linodeobjects.com/The%20Hindu%20Daily%20News%20Analysis%20__%203rd%20July%202022%20__%20UPSC%20Current%20Affairs%20__%20Prelims%20%2722%20%26%20Mains%20%2722%28360%29.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220707T073322Z&X-Amz-SignedHeaders=host&X-Amz-Expires=604799&X-Amz-Credential=ZVADROBVHWLK1FOYT225%2F20220707%2Fap-south-1%2Fs3%2Faws4_request&X-Amz-Signature=0aa4b388ea3197dd8a03253f5f7313b4209b8acf5e0a4308dc5e543801d22c73\"}]}");
Is there any look around to bring the solution.
it looks like both signed URLs might be GET urls?
For the Dolby.io Transcode API, your inputs should be GET signed urls and your outputs should be PUT signed URLs.
Additionally, it also looks like you are using the same input path/filename and output path/filename:
https://vb-object-storage.ap-south-1.linodeobjects.com/The%20Hindu%20Daily%20News%20Analysis%20__%203rd%20July%202022%20__%20UPSC%20Current%20Affairs%20__%20Prelims%20%2722%20%26%20Mains%20%2722%28360%29.mp4
You will want to use different paths for input and output, something like:
https://vb-object-storage.ap-south-1.linodeobjects.com/output/outputfile.mp4
(note the "output" added to the path and the change of the output filename)
Hi i have written a rest service to delete multiple files and i am trying to access the rest service.
My input to rest service will be List values which will be id of files that needs to be deleted.
For that i have written the below code
List<Long> ids=new ArrayList<Long>();
ids.add(4l);
ids.add(5l);
boolean status = false;
String jsonResponse = null;
DefaultHttpClient httpClient = null;
HttpResponse response = null;
try {
httpClient = new DefaultHttpClient();
StringBuilder url = new StringBuilder("http://localhost:8080/api/files");
URIBuilder builder = new URIBuilder();
URI uri = builder.build();
HttpDelete deleteRequest = new HttpDelete(uri);
//how to set list as Requestbody and send
response = httpClient.execute(deleteRequest);
}
Here i dont know how to set List as entity in request body and send. Can someone help me on this
A HTTP DELETE request should delete the resource identified by the URI. The body is not relevant. To delete a single resource:
DELETE /api/files/123
If you want to delete more than one file in a single request, model a collection of files as a resource. This could be done by listing more than one ID in the URL:
GET /api/files/123,456,789
This could return a JSON array with the details of the three files.
To delete these three files, execute a DELETE request agains the same URL:
DELETE /api/files/123,456,789
Good day,
I write server code (java, spring) that serves GET request.
The response should be in multipart-form format and should include 2 parts: string (json object) and file data.
The code is:
FileSystemResource resource = new FileSystemResource(targetFile);
String info = getInfo(targetFile);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
form.add("info", info);
form.add("file", resource);
MediaType multiPart =
MediaType.parseMediaType(MediaType.MULTIPART_FORM_DATA_VALUE);
ResponseEntity <MultiValueMap<String, Object>> responseEntity =
ResponseEntity.ok().contentType(multiPart).body(form);
return responseEntity;
The code works OK, but I didn't find a way to define content type of parts.
For first part it should be application/json, and for second part it depends on file type.
How to define this?
Did you try this?
HttpHeaders headers = new HttpHeaders();
headers.setContentDisposition(ContentDisposition.builder("inline; filename=\"" + resource.getFilename() + "\"").build());
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setContentLength(resource.contentLength());
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
Best Regards.
I am using rest template for creating HTTP post request . But request contain by default added Accept-char header with huge Content . Please suggest how to exclude addition of that header. Below is the code i am using:
HttpHeader header= new HttpHeader();
RestTemplate template = new RestTemplate();
header.setContentType("application/xml");
String body=" content of body ";
HttpEntity<string> request=new HttpEntity<string> (body,header);
template.postForObject(URL,request,String.class);
This should remove 'Accept-Charset' header:
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
stringHttpMessageConverter.setWriteAcceptCharset(false);
restTemplate.getMessageConverters().add(0, stringHttpMessageConverter);
following is the code I have written to PUT a image to a jersey server.
byte[] img = image(file);// coverts file into byte strea
FormDataMultiPart form = new FormDataMultiPart();
form.field("fileName", file);
FormDataBodyPart fdp = new FormDataBodyPart("fileUpload",
new ByteArrayInputStream(img),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
form.bodyPart(fdp);
ClientResponse rs = wr.type(MediaType.APPLICATION_FORM_URLENCODED).put(
ClientResponse.class, form);
When I am running the code, i am able to send the byte stream to the server, but its returning error that it is not able to find the filename i am providing in form.field, so returning a 400 bad request error?
i am not able to understand what i am missing here?
I was able to fix the my problem. I was not adding multipart. This link helped me
Following is what i did to replace the body-part definition
FormDataBodyPart f = new FormDataBodyPart(FormDataContentDisposition
.name("fileUpload").fileName(file).build(),
new ByteArrayInputStream(img),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
MultiPart multiPart = new FormDataMultiPart().bodyPart(f);
ClientResponse rs = wr.type(MediaType.MULTIPART_FORM_DATA).put(
ClientResponse.class, multiPart);