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.
我需要发送的一些电子邮件需要根据发送给的客户进行定制。一旦将电子邮件文本注入控制器并发送,就需要填写电子邮件中的某些信息(名字、姓氏、电话号码等)。
下面的bean中显示了一个示例
<bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="from@no-spam.com" />
<property name="to" value="to@no-spam.com" />
<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>
这将是一个自定义的电子邮件消息,我将定义并注入到我的控制器中。然后,我的控制器中的代码将根据从客户那里收集的输入填写值,因此控制器将具有类似于以下的代码
//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**
我遇到的问题是,当我尝试替换诸如%FIRST_NAME%、%PHONE%%ADRESS%> ,它没有替换它。我不确定这是因为我使用了replace()错误,还是因为它因为注入了值而对它进行了不同的处理。我还尝试过使用replaceAll(),但这也不起作用。如果有人对此有更好的想法,请告诉我。
非常感谢。