【问题标题】:How to set content type for emails sent using spring's JavaMailSenderImpl如何为使用 Spring 的 JavaMailSenderImpl 发送的电子邮件设置内容类型
【发布时间】:2011-03-17 13:13:06
【问题描述】:

问题来自主题。我正在使用 spring 3.0.3.RELEASE 中的 JavaMailSenderImpl 和 velocity 1.6.4 来从模板准备邮件。

当我从我的 webapp 收件人发送带有克罗地亚语字符的电子邮件时,我会收到“?”在正常克罗地亚字符的立场。如果我打开邮件的调试模式, 从日志中我可以看到 Content-type 设置为:

Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

如何设置:

Content-Type: text/html; charset=utf-8?

我在开发这个 web 应用程序时使用 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;
            }
}

你还有什么建议吗?

问候, 蒂霍

【问题讨论】:

    标签: spring email jakarta-mail velocity


    【解决方案1】:

    我解决了我的问题,感谢 this post.

    根据帖子我把这个放在mailSender bean配置中:

    <property name="defaultEncoding" value="UTF-8"/>
    

    【讨论】:

    • 对于以 html 形式发送的罗马尼亚字符,我对这个非常疯狂。谢谢!
    • 这里是archive.org上的帖子:GMail via Spring JavaMailSenderImpl
    • @ma cılay 感谢您提供新链接,我会添加它来回答。
    【解决方案2】:

    您可以在 JavaMailSenderImpl spring bean 配置中将其设置为属性mail.mime.charset。请参阅此previous query 回答的示例。

    【讨论】:

    • 您好,感谢您的回答,但我已经设置了此属性,您可以在下一篇文章中看到。
    • 在我的情况下它不起作用,我想正确显示波兰字符:(我尝试了每个解决方案......
    【解决方案3】:

    2020 年的回答,因为 XML 配置现在不是很常用:

    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.getJavaMailProperties().put("mail.mime.charset", "UTF-8");
    

    【讨论】:

      【解决方案4】:

      另一种选择可能是:

      JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
      mailSender.setDefaultEncoding("UTF-8");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 2013-08-29
        • 1970-01-01
        • 2015-03-10
        • 1970-01-01
        • 2012-11-17
        相关资源
        最近更新 更多