开发者

Email body with dynamic content in Java

开发者 https://www.devze.com 2023-01-02 18:52 出处:网络
I need to find a way how to send e-mails with dynamic content from my application in Java. In example:

I need to find a way how to send e-mails with dynamic content from my application in Java. In example:

Dear < Name > < Last name >, this is your new password < password >.

So when i send the mail the tags will change their values: < Name >= user's name, < Last name >=user's las开发者_如何学Ct name, < password >= user's password

So can somebody give me an advice or send me a link of some tutorial?


Use java.text.MessageFormat.

String template = "Dear {0} {1}, this is your new password {2}.";
String message = MessageFormat.format(template, "Jeff", "Atwood", "killskeet");

Alternatively, use String#format():

String template = "Dear %s %s, this is your new password %s.";
String message = String.format(template, "Jeff", "Atwood", "killskeet");

This only requires strict ordering of parameters.


This involves the use of basic variables. Have a look at Sending Email using JavaMail API and use variables to set the users first and last name as well as their password as part of the body of the message.


You could build the message with a StringBuilder and insert the name, lastName, and password variables directly. Or you could write your message in a text file along with the variables (as you've written in your post) and parse the text file.


You can use apache velocity to send emails with dynamic content. No strict ordering of parameters is required. you just need place holders and set them in velocity context. The template engine will replace the dynamic content.

0

精彩评论

暂无评论...
验证码 换一张
取 消