如何设置使用 spring 的 JavaMailSenderImpl 发送的电子邮件的内容类型
问题来自主题。我使用 spring 3.0.3.RELEASE 中的 JavaMailSenderImpl 和velocity 1.6.4 从模板准备邮件。
当我从我的网络应用程序发送带有克罗地亚语字符的电子邮件时,收件人会收到“?”代表正常的克罗地亚字符。如果我打开邮件的调试模式, 从日志中我可以看到 Content-type 设置为:
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
我如何将其设置为:
Content-Type: text/html; charset=utf-8?
我在开发此网络应用程序时使用 gmail 进行邮件发送。
这是我在 spring 的 servlet xml conf 文件中的设置:
<bean id="userAuthorizationManager" class="com.mypackage.manage.SimpleUserAuthorizationManagerImpl">
<property name="mailSender" ref="mailSender" />
<property name="velocityEngine" ref="velocityEngine" />
<property name="from" value="address" />
<property name="authorizationAddress" value="some text" />
<property name="subject" value="some text" />
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="username" value="user" />
<property name="password" value="pass" />
<property name="protocol" value="smtps" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.connectiontimeout">5000</prop>
<prop key="mail.smtp.sendpartial">true</prop>
<prop key="mail.smtp.userset">true</prop>
<prop key="mail.mime.charset">UTF-8</prop>
<prop key="mail.smtp.isSecure">true</prop>
<prop key="mail.smtp.requiresAuthentication">true</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
这是我来自邮件管理器的代码:
private void sendConfirmationEmail(final User user, final int requestId) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(user.getEmailAddress());
message.setFrom(from);
message.setSubject(subject);
Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
model.put("authorizationAddress", authorizationAddress);
model.put("requestId", requestId);
String text = VelocityEngineUtils
.mergeTemplateIntoString(
velocityEngine,
"com/mypackage/mail/registration-confirmation.vm",
model);
message.setText(text, true);
}
};
PendingMail pendingMail = new PendingMail(preparator);
pool.submit(pendingMail);
}
private class PendingMail implements Callable<Object> {
MimeMessagePreparator preparator;
protected PendingMail(MimeMessagePreparator preparator) {
this.preparator = preparator;
}
public Object call() {
mailSender.send(preparator);
return null;
}
}
您还有其他建议吗?
问候, 蒂霍
question is from subject. I'm using JavaMailSenderImpl from spring 3.0.3.RELEASE and velocity 1.6.4 for mail preparation from template.
When I send email with Croatian characters from my webapp recipient receives "?" in stand of normal Croatian characters. If I turn debug mode for mails,
from log I can see that Content-type is set to:
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
How can I set it to:
Content-Type: text/html; charset=utf-8?
I'm using gmail for mail sending while I'm developing this webapp.
Here are my settings in spring's servlet xml conf file:
<bean id="userAuthorizationManager" class="com.mypackage.manage.SimpleUserAuthorizationManagerImpl">
<property name="mailSender" ref="mailSender" />
<property name="velocityEngine" ref="velocityEngine" />
<property name="from" value="address" />
<property name="authorizationAddress" value="some text" />
<property name="subject" value="some text" />
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="username" value="user" />
<property name="password" value="pass" />
<property name="protocol" value="smtps" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.connectiontimeout">5000</prop>
<prop key="mail.smtp.sendpartial">true</prop>
<prop key="mail.smtp.userset">true</prop>
<prop key="mail.mime.charset">UTF-8</prop>
<prop key="mail.smtp.isSecure">true</prop>
<prop key="mail.smtp.requiresAuthentication">true</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
Here is my code from mail manager:
private void sendConfirmationEmail(final User user, final int requestId) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(user.getEmailAddress());
message.setFrom(from);
message.setSubject(subject);
Map<String, Object> model = new HashMap<String, Object>();
model.put("user", user);
model.put("authorizationAddress", authorizationAddress);
model.put("requestId", requestId);
String text = VelocityEngineUtils
.mergeTemplateIntoString(
velocityEngine,
"com/mypackage/mail/registration-confirmation.vm",
model);
message.setText(text, true);
}
};
PendingMail pendingMail = new PendingMail(preparator);
pool.submit(pendingMail);
}
private class PendingMail implements Callable<Object> {
MimeMessagePreparator preparator;
protected PendingMail(MimeMessagePreparator preparator) {
this.preparator = preparator;
}
public Object call() {
mailSender.send(preparator);
return null;
}
}
Have you any other suggestion?
Regards,
Tiho
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我解决了我的问题,感谢
这篇文章。
根据帖子,我将其放入 mailSender bean 配置中:
I resolved my problem thanks to
this post.
According to the post I put this in the mailSender bean configuration:
您可以将其设置为 JavaMailSenderImpl spring bean 配置中的属性
mail.mime.charset
。请参阅此上一个查询中回答的示例。You can set this as a property
mail.mime.charset
in the JavaMailSenderImpl spring bean config. See an example answered at this previous query.2020年的答案,因为XML配置现在不太使用:
2020 answer, as XML configuration is not very used now:
另一种选择可能是:
Another option could be: