Reputation: 32321
I have got this key value pairs under my properties file
mailsubject = REDEX Application --- Credentials
mailtext = Dear User,\n \
Following Are the credentials for REDEX Mapping Application -- pwd
setFrom = [email protected]
This is where i am using them in my java file for sending email through java
try {
Message message = new MimeMessage(session);
message.setSubject(props_load.getProperty("mailsubject"));
message.setText(props_load.getProperty("mailtext") + " " +generatedpwd);
Transport.send(message);
result = "success";
} catch (MessagingException e) {
result = "fail";
logger.error("Exception Occured"+ "sendemalto" +sendemalto , e);
}
My question is ,
Is it possible to insert User name after the word , so that it looks like
Dear User Kiran , Following Are the credentials for REDEX Mapping Application
Upvotes: 2
Views: 775
Reputation: 2861
MessageFormat form = new MessageFormat("Dear {0}, following...");
String userName = "Kiran";
Object[] testArgs = {userName};
System.out.println( form.format(testArgs) );
Output: "Dear Kiran, following..."
http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html
Upvotes: 1
Reputation: 6278
Use either String.replace(), String.replaceFirst() or String.replaceAll(), depending on what you want to do.
String.replace() replaces a given text with another one. So you can just do this:
"Dear User, Following are...".replace("User","User "+username);
Or if you need more control use String.replaceFirst() or String.replaceAll() together with a regex. replaceFirst() only replaces the first occurrence, whereas replaceAll() replaces all occurrences:
"Dear User, Following are...".replaceFirst("^Dear User"), "Dear User "+username);
Regex gives you much more control but is quite a bit more complex, so you'd need to read up on it to use it properly.
Upvotes: 0
Reputation: 4712
How about defining a placeholder and replacing it with your username:
props_load.getProperty("mailtext").replace("{{username}}", username)
With your template being like this:
mailtext = Dear User {{username}},\n ...
Upvotes: 4