简体   繁体   English

带有Java动态内容的电子邮件正文

[英]Email body with dynamic content in Java

I need to find a way how to send e-mails with dynamic content from my application in Java. 我需要找到一种方法,如何从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? 因此,有人可以给我提建议还是给我一些教程的链接吗?

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. 查看使用JavaMail API发送电子邮件,并使用变量将用户的名字和姓氏以及其密码设置为邮件正文的一部分。

Use java.text.MessageFormat . 使用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#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. 这仅需要严格的参数排序。

You could build the message with a StringBuilder and insert the name, lastName, and password variables directly. 您可以使用StringBuilder构建消息,然后直接插入名称,lastName和password变量。 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. 您可以使用apache velocity发送具有动态内容的电子邮件。 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. 模板引擎将替换动态内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM