发送电子邮件时遇到主题编码问题 [英] Problem with subject encoding when sending an email

查看:179
本文介绍了发送电子邮件时遇到主题编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送了一封电子邮件,但收到的邮件正确,但主题的编码不正确。我发送invitación,但我收到invitaci?n。消息的内容是确定的。



消息的内容来自Velocity Template的转换,而Subject被设置为String变量。



我搜索了一下,我发现有些人说MimeUtility.encodeText()可以解决这个问题,但是我没有成功。



我该如何解决这个问题?这是我到目前为止的代码。

  String subject =Invitación; 
String msgBody = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,/ vmTemplates / template.vm,UTF-8,model);

属性props = new Properties();
Session session = Session.getDefaultInstance(props,null);

尝试{
String encodingOptions =text / html; charset = UTF-8;
Message msg = new MimeMessage(session);
msg.setHeader(Content-Type,encodingOptions);
msg.setFrom(new javax.mail.internet.InternetAddress(emailFrom));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(emailTo));

msg.setSubject(subject);
msg.setContent(msgBody,encodingOptions);
Transport.send(msg);
$ b $ catch(AddressException e){
...
} catch(MessagingException e){
...
}

谢谢 JavaMail可能有太多的抽象,你在这里成为受害者。当你使用

  Message msg = new MimeMessage(session); 

您正在创建一个 MimeMessage code>对象,但将其视为 Message 对象。 Message 只有一个 setSubject(String subject)方法,它使用平台默认字符集来编码主题。如果平台默认不能对其进行编码,则会在结果标头中获得字符。然而, MimeMessage 有一个 setSubject(String subject,String charset)方法,它允许你指定字符集想用来编码主题。因此,只需将您的代码切换到

  MimeMessage msg = new MimeMessage(session); 
msg.setHeader(Content-Type,encodingOptions);
msg.setFrom(new javax.mail.internet.InternetAddress(emailFrom));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(emailTo));

msg.setSubject(主题,UTF-8);

并且它应该有效。

I'm sending an email and I'm receiving it correctly but the encoding of the subject is not correct. I'm sending "invitación" but I'm receiving "invitaci?n". The content of the message is OK.

The content of the message is coming from a transformation of a Velocity Template while the subject is set in a String variable.

I've googled around and I've seen that some people says that MimeUtility.encodeText() could solve the problem, but I have had no success with it.

How can I solve the problem? This is the code I have so far.

String subject = "Invitación";
String msgBody = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/vmTemplates/template.vm", "UTF-8", model);

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

try {
    String encodingOptions = "text/html; charset=UTF-8";
    Message msg = new MimeMessage(session);
    msg.setHeader("Content-Type", encodingOptions);
    msg.setFrom(new javax.mail.internet.InternetAddress(emailFrom));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));

    msg.setSubject(subject);
    msg.setContent(msgBody, encodingOptions);
    Transport.send(msg);

    } catch (AddressException e) {
        ...
    } catch (MessagingException e) {
        ...
    } 

Thanks

解决方案

JavaMail has perhaps a little too much abstraction, and you're falling victim to it here. When you use

Message msg = new MimeMessage(session);

you're creating a MimeMessage object but treating it as a Message object. Message has only a setSubject(String subject) method, which uses the platform default charset to encode the subject. If the platform default can't encode it, you get ? characters in the resulting header. MimeMessage, however, has a setSubject(String subject, String charset) method which will allow you to specify the charset you want to use to encode the subject. So just switch your code to

MimeMessage msg = new MimeMessage(session);
msg.setHeader("Content-Type", encodingOptions);
msg.setFrom(new javax.mail.internet.InternetAddress(emailFrom));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(emailTo));

msg.setSubject(subject, "UTF-8");

and it should work.

这篇关于发送电子邮件时遇到主题编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆