场景:用户使用她的电子邮件地址提交联系表。我收到的电子邮件应该是“来自”联系表格中填写的电子邮件。我得到的是我在 spring 上下文中配置的 gmail (foo@gmail.com) 帐户,尽管setFrom在代码中正确设置如下:
public void sendContactNotification(final ContactForm contactForm) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
@SuppressWarnings({ "rawtypes", "unchecked" })
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(contact@foo.com);
message.setFrom(contactForm.getEmail());
message.setSubject("New contact message " + contactForm.getTopic());
message.setReplyTo(contactForm.getEmail());
message.setSentDate(new Date());
Map model = new HashMap();
model.put("newMessage", contactForm);
String text = VelocityEngineUtils.mergeTemplateIntoString(
velocityEngine, "velocity/new_contact_message.vm", "UTF-8", model);
message.setText(text, true);
}
};
this.mailSender.send(preparator);
}
在 Spring 上下文中,发送方配置如下:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="foo@gmail.com" />
<property name="password" value="password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
谢谢,阿德里安