I am using Spring MVC for my web application and I am using my applicationContext.xml file to configure my emails which I am injecting into my controllers in my spring-servlet.xml file.
Some of the emails I need to send will need to be tailored to the customer that they are being sent to. Certain information in the email (First name, Last name, phone number,etc) will need to be filled in once the email text has been injected into controller and is being sent.
An example of this is is shown in the bean below
<bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="[email protected]" />
<property name="to" value="[email protected]" />
<property name="subject" value="Testing Subject" />
<property name="text">
<value>
Dear %FIRST_NAME%
Blah Blah Blah Blah Blah...
We Understand that we can reach you at the following information
Phone:%PHONE%
Address:%ADDRESS%
</value>
</property>
</bean>
This would be a custom email message that I would define and inject into my controller. The code in my controller would then fill in the values based on the input collected from the customer so the controller would have code similar to the following
//SimpleMailMessage property is injected into controller
private SimpleMailMessage simpleMailMessage;
//Getters and Setters for simpleMailMessage;
MimeMessage message = mailSender.createMimeMessage();
try{
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(simpleMailMessage.getFrom());
helper.setTo(simpleMailMessage.getTo());
helper.setSubject(simpleMailMessage.getSubject());
String text = simpleMailMessage.getText();
text.replace("%FIRST_NAME%",model.getFirstName());
text.replace("%PHONE%",model.getPhone());
text.replace("%ADDRESS%",model.getAddress());
helper.setText(simpleMailMessage.getText());
}
catch (MessagingException e) {
throw new MailParseException(e);
}
mailSender.send(message);**strong text**
The problem I am having is that when I try to replace the values such as %FIRST_NAME%,%PHONE% and %ADDRESS%>, it is not replacing it. I am not sure if this is because I am using replace() wrong, or if it is because it is treating it differently because the value is injected. I have also tried to use replaceAll() and that is not working either. If anybody has better ideas how to do this, please let me know.
Thank you