【问题标题】:Sending emails to recipients with international characters向具有国际字符的收件人发送电子邮件
【发布时间】:2011-05-23 09:18:57
【问题描述】:

我需要向电子邮件地址中包含国际字符的收件人发送电子邮件。一个示例电子邮件是:

GaÙl@asterixthe.com

我已经进行了很多搜索,但我找到的信息是针对正文而不是收件人的。我是否需要以某种方式对此进行 mime 编码,如果需要,如何?

我得到的代码如下:

 MailMessage mailMessage = 
    new MailMessage(email.SenderEmailAddress, email.RecipientEmailAddress, "Why hello there!", emailMessage)
    {
        IsBodyHtml = true,
        BodyEncoding = new System.Text.UTF8Encoding()
    };


 SmtpClient smtpClient = new SmtpClient("localhost")
 {
    Port = 25,
 };
 smtpClient.Send(mailMessage);

谢谢,

西蒙

编辑:这是例外情况

System.FormatException: The specified string is not in the form required for an
e-mail address.
   at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset,
String& displayName)
   at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset)
   at System.Net.Mail.MailAddressCollection.ParseValue(String addresses)
   at System.Net.Mail.MailAddressCollection.Add(String addresses)
   at System.Net.Mail.Message..ctor(String from, String to)
   at System.Net.Mail.MailMessage..ctor(String from, String to)
   at System.Net.Mail.MailMessage..ctor(String from, String to, String subject,
String body)

【问题讨论】:

  • 如果你这样发送邮件,而不用指定编码会怎样?
  • 它会引发异常 - 我会更新问题。塔:)
  • 我认为这是重复的:stackoverflow.com/questions/760150/… 也许这会对你有所帮助

标签: c# email encoding


【解决方案1】:

好的,

阅读 .Net 框架中 MailMessage 类的文档,我发现该类的构造函数之一接收到一个 MailAddress 对象。

根据 MSDN (http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx),DisplayName 属性可以包含非 ASCII 字符,我认为如果他们明确指定是因为名称不能拥有它们。

您是否尝试过其他线程上的建议,即您可以使用“punycode”的建议?

【讨论】:

  • 我尝试使用 MailMessage 但没有成功。我想知道这是否是必须为这个特定应用程序使用 .NET 2 的限制。至于微不足道的代码 - 我会再看一遍。谢谢。
  • 接受了这个答案,即使我仍然坚持。我认为第 3 方组件可能会这样做,但 .NET 2 不会,我很高兴将其作为标准草案。
猜你喜欢
  • 1970-01-01
  • 2016-03-06
  • 2021-07-19
  • 2012-05-18
  • 2023-03-13
  • 2010-12-15
  • 1970-01-01
相关资源
最近更新 更多