tags:

views:

156

answers:

4

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 last name, < password >= user's password

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

+2  A: 

This involves the use of basic variables. Have a look at Sending Email From Your Application Using Java Mail and use variables to set the users first and last name as well as their password as part of the body of the message.

Oren
A: 

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.

Shakedown
A: 

(moved into question)

Please don't post comments or updates as an **answer**. Use `add comment` link to post a comment or use `edit` link to update a question. I've moved it into the question, you should delete this "answer" :)
BalusC
Sorry, i'm new. :)
A: 

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.

BalusC
Thank you for the answers, but i was looking for something like this: http://www.bredelet.com/Denis/FreeMarker%20first%20steps.htmlorhttp://www.javaworld.com/javaworld/jw-12-2001/jw-1228-velocity.html
My answer is also something like that? Or do you want to use names in expressions and some more fanciness? Have a look at [JEXL](http://commons.apache.org/jexl/) then.
BalusC