我需要寄一封带有正文的信:
Lector {LectorName} had created a new course
----------------------------------------------------
Name: {CourseName}
Category: {CourseCategory}
Description: {CourseDescription}
Links: {CourseLinks}
----------------------------------------------------
Please, review this course at {CourseApproveLink}
我在页面上制作了免费标记
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p> ${LectorName} had created a new course</p>
<p>----------------------------------------------------</p>
<p>Name: ${Course.title}</p>
<p>Category: ${Course.category.name}</p>
<p>Description: ${Course.descr}</p>
<p>Links: ${Course.links}</p>
<p>----------------------------------------------------</p>
<p>Please, review this course at ${CourseApproveLink}</p>
</body>
</html>
发信方法中如何填写并传递值? 这是我的代码。我的方法 sendMail 和 Bean“mailSender”与我的设置。 有必要这样做 new MimeMessage(session) 吗? 如何将设置从 bean 获取到会话中?
@Service("mailService")
public class MailService {
@Autowired
private MailSender mailSender;
@Autowired
private SimpleMailMessage alertMailMessage;
@Resource(name = "freemarkerEmailConfig")
private Configuration emailConfiguration;
public void sendMail(String from, String to, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
public void sendAlertMail(String alert) {
SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);
mailMessage.setText(alert);
mailSender.send(mailMessage);
}
}
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.mail.ru" />
<property name="port" value="465" />
<property name="username" value="[email protected]" />
<property name="password" value="***********" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.ssl.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="alertMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<value>[email protected]</value>
</property>
<property name="to">
<value>[email protected]</value>
</property>
<property name="subject"
value="Alert - Exception occurred. Please investigate" />
</bean>
您需要将映射传递给使用 freemarker 模板发送电子邮件的方法。在你的情况下,地图看起来像:
Map<Object, Object> map = new HashMap<Object, Object>();
map.put("LectorName", "...");
map.put("Course", course);
map.put("CourseApproveLink", "...");
Freemarker 将根据您传入的键解析变量名称。
如果使用 Spring,则在 applicationContext.xml 中配置模板目录,如下所示:
<!-- FreeMarker Configuration -->
<bean id="freemarkerEmailConfig" class="freemarker.template.Configuration">
<property name="directoryForTemplateLoading" value="templates/email" />
<property name="objectWrapper">
<bean class="freemarker.template.DefaultObjectWrapper"/>
</property>
</bean>
将您的模板放在
templates/email
文件夹下(相对于您的网络应用程序)。将 applicationContext 中定义的 freemarkerEmailConfig
bean 注入到您的服务类中:
@Resource(name = "freemarkerEmailConfig")
private Configuration emailConfiguration;
现在在您的服务类中,您可以使用 emailConfiguration 来检索模板,然后使用上面的地图对其进行处理,如下所示:
Template template = emailConfiguration.getEmailTemplate(templateName);
String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
FreeMarkerTemplateUtils 是 Spring 的一个类。现在
text
将包含 html,其中所有变量都替换为地图中的值。只需发送带有 text
作为 html 内容的电子邮件即可:
MimeMessage msg = mailSender.createMimeMessage();
msg.setFrom(new InternetAddress(EMAIL_SENDER_ADDRESS, EMAIL_SENDER_PERSONAL));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setContent(text, "text/html; charset=UTF-8");
for (EmailInternetAddress emailInternetAddress :emailInternetAddresses) {
msg.addRecipient(emailInternetAddress.getRecipientType(),
emailInternetAddress.getInternetAddress());
}
mailSender.send(msg);
FreeMarker 的 Configuration 类中没有名为 getEmailTemplate() 的方法。仅存在具有各种重载选项的 getTemplate(....) 方法,但不仅仅用于电子邮件处理。
我遇到的问题与 freeMarker 生成的电子邮件内容问题
中提到的问题相同对此有什么建议吗?