Fill preformatted text file to send email - MessageFormat alternative - java

I Used MessageFormat to format the contents of a file with parameters and get so a formated string with correct parameters.
(I used it to format email body.finally I had one file per email body, the application needs to send a lot of different emails, so I got a lot of preformatted body files)
So far, I had six parameters.
Problem: Things are changing and now I have more than 6 parameters today ....
I realize that MessageFormat is limited to 6 parameters!
What can I do? Is there an alternative to MessageFormat? or the only solution is to put each email line in properties ( and hope not to have more than 6 per line parameters !)
Thanks,
Christophe.

Use a templating library. Freemarker for example.

since you've tagged this as 'spring' you could use the Apache Velocity templating engine (VelocityEngineFactoryBean), wire it into your class as a VelocityEngine.
You could then use VelocityEngineUtils.mergeTemplateIntoString() passing the name of the template file (stored in your classpath)

Related

Freemarker embed image on ftl

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.

struts email application .Dynamic Html content for the email body

In a struts2 application I want email notification is to be sent when ever a user is logged in .
I want the mail body to be Html content, with the data entered by the User in run time.
Can any one suggest suggest the best way to do it !
Foe now iam using mail.jar and iam able to send the static content easily.
But facing difficulty
1. maintaining the long HTMl code string .
2. How to substitute the values in the string with the dynamic values
Please can somebody help me with a proper solution. Or Best practice to follow in Email application !
Thanks in Advance !
You can put the email body in a properties file and read it from there.
For substitution, you can define "placeholder" in your String like {FIRSTNAME} {LASTNAME} etc., and then do a replaceAll for the dynamic portions in your code.
What u need to look into is "freemarker". Its a library and u can achieve your purpose with it.
You can use an WYSIWYG (What You See Is What You Get) Editor like CKEditor or TinyMCE, letting the user writing the html without even knowing html.
Then, after the user submitted the body, you take the output that is pure html,
and inject it into the mail body.
End of story...
Just remember to set mimetype to text/html and not to text/plain , or you will see the tags instead of their representation...

Reading messages dynamically in Java1.6

I've multiple messages file (messages_en.properties, messages_ch.properties)
These files are having some static html text & need some dynamic input param such as username so that it'll say Dear {0}, thanks for subscription....
Now i need to substitute username there after reading those contents from appropriate file.
How can I do that in Java? Is there any framework sample code available?
See the I18N trail. Nutshell version from that tutorial, using newer API methods:
ResourceBundle messages = ResourceBundle.getBundle("MessageBundle", Locale.getDefault());
String output = MessageFormat.format(messages.getString("msg.key"), "Mike");
Depending on your actual usecase there may be some shortcuts (e.g., web frameworks often include direct support for localization via tag libraries, some libraries wrap up some busywork, etc.)
Check MessageFormat out:
String result = MessageFormat.format(
"Dear {0} , thanks for subscription....", username);
You can combine it with ResourceBundle getString method to read the message from your properties files through its key and output the formatted, dynamically filled message.

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.

Getting binary file and text information

Currently, I have a servlet act as web service.
When I pass in parameters using POST, it will return me an executable binary file (application/octet-stream). However, beside binary file, I would also like to get additional information (in text format) about this binary file.
Is it possible to achieve this by using only single POST request? But, how is it possible, to switch from application/octet-stream to text/plain within single POST response?
It is not possible to change the MIME type within a single response.
However, i think t is possible to put your additional information into the response header using the HttpServletResponse.addHeader method.
You could return a multipart MIME response (multipart/mixed; boundary=XXX instead of application/octet-stream) with the binary part encoded in Base64.
I'm not sure if the JavaMail API can be used to construct the content, but it's worth a look.
Within a single POST it isn't possible. But two ideas:
Show your text file as another web page that starts the download of your executable
Bundle both files into a zip archive

Categories