Freemarker embed image on ftl - java

I am trying to embed an image on a Freemarker ftl template to send as an email, I've based on this question Feemarker writing images to html, I did the exact same thing as this question said, but the email is being generated like this
What may be causing this error, and how to fix it?
My template looks like this
<img alt="My image" src="${imgAsBase64}" />
The image is a Chart, and I get the Base64 String, which I called imageBase64Str, via a Primefaces JavaScript function that generates the Base64 of the chart image, I pass it to the the bean and pass the parameter to the template like this
String encoded = imageBase64Str.split(",")[1];
byte[] decoded = Base64.decodeBase64(encoded);
String imgDataAsBase64 = new String(decoded);
String imgAsBase64 = "data:image/png;base64," + imgDataAsBase64;
emailParams.put("imgAsBase64", imgAsBase64);

String encoded = imageBase64Str.split(",")[1]; is suspicious. Looks like you are changing the base 64 string generated in some different way. Is the image actually a png or it's in another format? I think that if you remove that split and just do emailParams.put("imgAsBase64", imageBase64Str); it may work.
However you need to consider that this solution won't work for many email clients. According to this link https://www.campaignmonitor.com/blog/email-marketing/2013/02/embedded-images-in-html-email/ Base64 embedded images are not supported on a few major email clients, web and standalone, including Gmail and Outlook. Given that they are the most common email clients you don't want to deliver a solution that doesn't work on them or most of your users are gonna be unhappy.
IMO your best bet is to host the images in a server and use fully qualified URLs in your freemarker template.
An alternative is using the attachment and reference them in the html source as explained here: https://stackoverflow.com/a/36870709/2546299 but it require changes on the way the emails are sent (need to add the attachments) so it may not be suitable for your case.

Related

Creating an email in Java

I would like to "create" an email using Java.
Here's what I mean:
Based on information I already have, I would like to make an email message open in Microsoft Outlook with the fields To, CC, Subject, Message Body, and attachments already populated (all now stored as strings, the directories for attachments are stored as strings as well).
The message needs to open in Outlook for the user to verify the contents and give the opportunity for adding more CC, slight adjustments to subject and message body.
From what I gather, it seems that the "best" way of achieving this is first creating a file on disk that Outlook can read, which contains my message, then opening it with outlook using something similar to the code below.
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("OUTLOOK Directory + CMD switches for opening files");
Ideally, I would like to use a simple framework for simply creating such a file using the strings I already have for the different fields (or achieving the same thing through a a non-simple framework).
If there's no "good" way of achieving the above, I'd settle for a method of just attaching my attachment file to a given Outlook template (.oft) file.
(3. I'll resort to my current solution of simply having the template open in Outlook, the attachment in Explorer, and prompt the user to drag the file into Outlook.)
I've looked at HSMF in Apache POI (I'm Apache POI for other parts of my program), but it appears to be rather experimental at this point, and I've been unable to find much documentation for it.
Does anyone have any suggestions on where to look?
Use the Desktop API with the URI constructor that will quote legal characters. This example code will open your default mail client with the headers populated.
public static void main(String[] args) throws Exception {
URI msg = new URI("mailto", "you#foo.com&cc=team#bar.com&subject=How to create email in Java?body=Use JavaMail.", (String) null);
Desktop.getDesktop().mail(msg);
}
The only limitation is that there is an upper limit to the length of the URI that the O/S can handle. On windows, the 'start' command also understands the syntax which is explained in RFC 2368.

What URL do I use to open a String object in a web browser

If I have a HTML String object, using Selenium in Java, how can I get the browser to open that String as a HTML page? I have seen this done before but I don't remember the format that the URL needs to be.
For this example, let's say the string is :
<h2>This is a <i>test</i></h2>
I looked through this page and couldn't find the answer but I might be overlooking it. For example I tried this URL and it didn't work for me:
data:<h2>This is a <i>test</i></h2>
Here is a link for documentation http://en.wikipedia.org/wiki/Data_URI_scheme. You need to specify MIME-type of data. Try data:text/html,<h2>This is a <i>test</i></h2>

How do i send HTML email containing dynamic link via JAVA

I am trying to send a dynamic link via email with the following code.
Message messageSSL = new MimeMessage(session);
int hash=1000;
String content="click here";
messageSSL.setContent(content, "text/html");
However, i have failed to generate a dynamic link.The output in the mail is in the plain text format.
Output (In the mail):
click here
Even though, the following code works and generates a link called "click here".
String content="click here";
Thanks!!
I think the problem is with backward slash. We should be using forward slash in urls. Please change and try it.
The Apache Commons Email library has some useful classes that take care of the low-level details for stuff like making HTML email work properly. Check it out:
http://commons.apache.org/proper/commons-email/
can you please enclose the link by html tag and try once.
String content="<html><body>click here </body></html>";
i am using same library and working fine for me.
please check below thread
How Can I put a HTML link Inside an email body?

How to embed images to the html body of an email using the java library Jodd Email?

At the main page of the Jodd email library http://jodd.org/doc/email.html
there is a very specific example on how to use the library to embed an image (and not just simply attach it as a file) to an email you are about to send.
Unfortunately the resulting Content-Type of the part of the email that contains the image is:
Content-Type: application/octet-stream
But in order to display it correctly we need this Content-Type:
Content-Type: image/png
if you have a png image for instance.
But I cannot seem to find how to configure this inside the Jodd email library..
This is what I am seeking for. Thank you :)
If you followed the example from Jodd site then you embedded your files using method embedFile(). This method is a 'shortcut' method for:
attach(new FileAttachment(file));
where attach() is the central, generic method for attaching content. FileAttachment rely on javax.mail for setting the content type, probably based on extension.
Therefore, to set content type manually, use generic attach() method. For example, embedding file like this:
.embedFile("d:\\c.xxx")
would set content type to "application/octet-stream" as it is not recognized for xxx extension. Instead, you can use the following:
.attach(new ByteArrayAttachment(
FileUtil.readBytes("d:\\c.xxx"), "image/png", "c.png", "c.png"))
where you can manually set the content type regardless the file name. If you don't want to load file bytes, you can pass InputStream instead etc.
Another solution (if you want to keep using embedFile) is to check your mime type settings.
Note: since there are many combinations how to attach content (bytes, input stream, file, inline...), attach methods will be refactored in Jodd 3.4.1. in order to provide more developer friendly api. Stay tuned ;)

Set [Automatically wrap text] in Java mail

i have an account register function, after user inputted personal data, an confirm email will be sent to that customer with a generated link. The problem is that: because the link is too long, it is broken into two lines (The second line is from character 76) and the second line does not belong the the first line (User cannot click on the whole link). I think this problem may come from the word wrap or something like that
In Outlook Express, under menu->Tools->Options->Send->HTML setting, we can set number of characters that the email content should be wrapped in each line by changing the value. Is there any way to set this function using core Java Mail?
Thank you in advance.
Word wrapping is done by the viewer (i.e. Outlook Express) not when sending email. I would guess that you are sending plain text emails and relying on the viewers to try and identify that it contains links. Try sending HTML mail and using ''
No, JavaMail is a library allowing you to send/receive email through Java. It is not an application like Outlook/Outlook Express or Thunderbird for that matter.
That said, you can write code that does the formatting before it invokes JavaMail to send the email out.
First, you can't set a setting in java mail to change a client's formatting.
Second, while my solution might not be the best answer to the question. It should help with the problem you are having.
Before adding your link into the body of the mail make sure you;
Put the link on a new line. "\n" ;)
Make a little method using URL shortening API like bitlyj for bit.ly to shorten the URL. Add the shortened link and walla!
msg.setContent("This is an example of adding a shortened URL\n"
+ shortLink("http://www.longlink.com")
+ "\n", "text/plain");
public String shortLink(String link) {
Url url = as("Username", "APIKey").call(shorten(link));
return url.getShortUrl();
}
Using this approach you shouldn't have any issues with word wrap stuff.

Categories