I am trying to attach the HTML file to the Gmail body, but the image which I have attached in the HTML file is not getting displayed in Gmail (working fine in Outlook).I have tried putting the base64 code of image but still the same issue (In this case even in Outlook the image is blocked). I even tried with "Content ID generator".
Please see following code what I have tried), but unable to display image.
Can some one tell me how to solve this issue?
(Note: I have used the free marker template to generate the HTML code)
Map<String, String> inlineImages = new HashMap<String, String>();
inlineImages.put("image2", "C:\\Users\\xxxxxxx\\Desktop\\Capture1.jpg");
model.put("image", inlineImages);
String text = geFreeMarkerTemplateContent(model);
System.out.println("Template content : "+text);
helper.setText(text, true);
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(text, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodyPart);
MimeBodyPart imagePart = new MimeBodyPart();
imagePart.setHeader("Content-ID", "image2");
imagePart.setDisposition(MimeBodyPart.INLINE);
// attach the image file
imagePart.attachFile("C:\\Users\\xxxxx\\Desktop\\Capture1.jpg");
multipart.addBodyPart(imagePart);
mimeMessage.setContent(multipart);
Related
I use the JavaMail Spring implementation with Velocity template engine. Everything goes well when I send the mails, the pictures are displayed normally and the styles also apply.
The problem that I encounter is that at the reception the email client displays my email as containing attachments, which are actually the images that appear in my email.
I do not understand why the images sent with the mail are seen as pieces joined by the mail client. Somebody can help me please?
This is my code :
final MimeMessage message = mailSender.createMimeMessage();
final MimeMessageHelper helper = new MimeMessageHelper(message,true, "UTF-8");
try {
helper.setFrom(getSystemAddress());
if (replyTo != null) {
helper.setReplyTo(replyTo);
}
helper.setTo(to);
helper.setSubject(subject);
helper.setText(body, isHTML);
System.out.println("Helper message factory: "+helper);
FileSystemResource res = new FileSystemResource(new File(MailHandler.class.getResource("logo-email.png").getFile()));
FileSystemResource res1 = new FileSystemResource(new File(MailHandler.class.getResource("bg-header.PNG").getFile()));
helper.addInline("cid2", res1);
helper.addInline("cid1", res);
mailSender.send(message);
I have an email which has multipart/alternative part . Into this part there is a multipart/related part; Into this multipart/realted part I have an text/html and image/jpg. I'm trying to extract those two parts and adda tag to the html part message and compose it again. The html and image part should look the same. I have inserted only a tag into the html part. But when I try to compose the message again I somehow cannot manage. I don't know how to recreate this multipart nested into another multipart. I have this
into the original email and again into my htmlmessageBodyPart. But when my recreated e-mail is displayed the image is broken.
below is part of my sender class. It works ok with attachments, plain text and so on.Any help will be appreciated!
// create a multi-part to combine the parts
Multipart multipart = new MimeMultipart(
setSubtype(origMessage));
// create a new message part for body text
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(message);
messageBodyPart.setDisposition(MimePart.INLINE);
multipart.addBodyPart(messageBodyPart);
BodyPart htmlmessageBodyPart = new MimeBodyPart();
htmlmessageBodyPart.setContent(html, content);
htmlmessageBodyPart.setDisposition("null");
multipart.addBodyPart(htmlmessageBodyPart);
MimeBodyPart imageBodyPart = new MimeBodyPart();
imageBodyPart.setHeader("Content_ID",
"<part1.40705.09505#something.com>");
imageBodyPart.setDisposition(MimePart.INLINE);
imageBodyPart.setContent(html,"image/png");
imageBodyPart.attachFile(file);
multipart.addBodyPart(imageBodyPart);
msg.setContent(multipart1);
Transport.send(msg);
Your original message has an extra level of nested multiparts that's missing from your code.
// add text/plain part to multipart, then...
Multipart innerMultipart = new MimeMulpart("related");
// add html and image parts to innerMultipart
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(innerMultipart);
multipart.addBodyPart(mbp);
I'm trying to load a webview using HttpRequestMessage in Windows Phone 8.1. The problem is that the Content-Type header is missing in the content headers when checked in Fiddler.
byte[] postData = GetWebviewPostDataBytes();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, prepareUri(url));
var httpContent = new HttpBufferContent(postData.AsBuffer());
httpContent.Headers.Add("Content-Type", GetContentType());
request.Headers.Add("User-Agent", GetUserAgent());
request.Content = httpContent;
webView.NavigateWithHttpRequestMessage(request);
I found some links where this is posed as an internal bug. Can someone tell me a workaround to this?
The only solution I've found for this is to use a form to load your page.
I'm loading a local HTML page which I'm generating with my POST parameters. This page contains an hidden HTML form which I will submit once the page will be loaded.
The web page loading is then:
generate a local HTML page with a form and your parameters in hidden input fields
load this page in your webview
once the page is loaded, request a submit of the form using the webpage postContent() function
the webview will then navigate to the expected webpage with the content-type header set
This sample code is in WinJS but it can easily be transposed in C# since it is the same webview component.
var htmlContent = "<html><head><script type='text/javascript'>function postContent(){document.getElementById('postForm').submit();}</script></head><body><form id='postForm' action='{targetUrl}' method='post'>{inputFields}</form></body></html>";
var inputFields = "";
var iterator = payloadContent.first();
while(iterator.hasCurrent)
{
inputFields += "<input hidden='on' name='{n}' value='{v}'/>".replace("{n}", iterator.current.key).replace("{v}", iterator.current.value);
iterator.moveNext();
}
var htmlContent = htmlContent.replace("{targetUrl}", "YOUR URL HERE").replace("{inputFields}", inputFields);
this._webviewElement.navigateToString(htmlContent);
In the page loaded event you will then have to request the webview to execute the 'postContent()' javascript function to submit the form.
this._webviewElement.invokeScriptAsync("postContent");
Hope this helps.
i'm trying to display png image, i'm getting as a byte stream from java service(spring):
byte[] buffer1 = pfmAppFacade.getCheckImage(imageId, isFront);
List<MediaType> list = new ArrayList<>();
list.add(MediaType.IMAGE_PNG); //image/png
HttpHeaders headers = new HttpHeaders();
headers.setAccept(list);
headers.setContentType(MediaType.IMAGE_PNG);
ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(buffer1, headers, HttpStatus.OK);
return responseEntity;
The image displayed correctly in firefox and chrome ver 31, But in older chrome, safary and ie9 - i'm getting broken image icon displayed.
<img src="http://myserverIP/imgService/123432" />
If i trying to download image the file that i get is corrupted.
If anybody can show my some solution - will appreciate.
The solution that worked for me is to add .PNG at the end of url:
<img src="http://myserverIP/imgService/123432.png" />
I am converting a html document to a pdf document and send it with java mail.
I want to insert an image on the top of the pdf document with:
<img alt="Logo" class="logo" src="https://www.somesite.org/images/logo.png"/>
But it isnt shown in the pdf document.
The link is working correctly, i tried it in my browser.
When i put in a absolute path to the image on my server he finds the path, but the email programm doesnt have access to our server of course, except of the link i want to use...
Isnt it possible to use such links?
EDIT:
final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setValidating(false);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
builder.setEntityResolver(FSEntityResolver.instance());
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
String filename = wrapper.GetHtmlFilename(this.getClass().getName());
String html = wrapper.GetHtmlFile(filename, "UTF-8");
String result = wrapper.GetBody(html);
document = builder.parse(new ByteArrayInputStream(result.getBytes("UTF-8")), "UTF-8");
baos = new ByteArrayOutputStream();
renderer = new ITextRenderer();
renderer.setDocument(document, null);
renderer.layout();
renderer.createPDF(baos);