向具有国际字符的收件人发送电子邮件

发布于 2024-10-06 20:09:45 字数 1392 浏览 2 评论 0原文

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

GaÙ[电子邮件受保护]

我已经进行了大量搜索,但我找到的信息是针对身体的,而不是针对接收者的。我是否需要以某种方式对其进行哑剧编码?如果需要,如何进行?

我得到的代码如下:

 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)

I need to send an email to a recipient with international characters in the email address. An example email would be:

GaÙ[email protected]

I've done quite a bit of searching but the information I'm finding is for the body and not the recipient. Do I need to mime encode this in some way and if so, how?

The code I've got is below:

 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);

Thanks,

Simon

Edit: Here's the exception

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2024-10-13 20:09:45

好吧,

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

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

您是否尝试过另一条线索上的建议,即您可以使用“punycode”?

Ok,

Reading the documentation for the MailMessage class in the .Net framework, I found that one of the constructors of the class receives a MailAddress object.

According to MSDN (http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx), the DisplayName property can contain non-ASCII characters, I would think that if they specify that explicitly is because the Name cannot have them.

Have you tried the suggestion on the other thread, the one saying that you could use "punycode"?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文