使用丹麦语字符将ASP.NET中的电子邮件发送给收件人

时间:2011-02-22 07:09:56

标签: c# asp.net

我正在尝试使用电子邮件类MailMessage和SMTPClient在ASP.NET中发送电子邮件。代码工作得很好,但当收件人地址有丹麦字符时,例如Niels.Østergaard@company.dk,Send()方法抛出一个抱怨地址格式的异常。如何解决这个问题?这段代码如下:

MailMessage mail = new MailMessage();

//set the addresses
MailAddress fromAddress = new MailAddress(from);

foreach (string s in to)
{
    mail.To.Add(new MailAddress(s));
}

mail.From = fromAddress;
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = false;

// Include attachments if any
if (files != null)
{
    foreach (string file in files)
    {
        mail.Attachments.Add(new Attachment(string.Format("{0}\\{1}", 
            HttpContext.Current.Server.MapPath("~/Temp"),  file)));
    }
}

// These settings should be retrieved from web.config
SmtpClient smtp = new SmtpClient();
smtp.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtp.PickupDirectoryLocation = @"C:\Temp\Mail";
smtp.Send(mail);

1 个答案:

答案 0 :(得分:0)

我使用http://msdn.microsoft.com/en-us/library/system.globalization.idnmapping.aspx处理这个问题取得了一些成功。

这样的事情:

        var m = new IdnMapping();
        var parts = "Niels.Østergaard@company.dk".Split('@');
        var adr = new MailAddress(string.Concat(
            m.GetAscii(parts[0]),
            "@",
            m.GetAscii(parts[1])));