开发者

Send Email in ASP.NET to recipient with Danish characters

I am trying to send an email in ASP.NET using the email classes MailMessage and SMTPClient. The code works just fine but when the recipient address has Danish characters e.g. Niels.Østergaard@company.dk, the Send() method throws an exception complaining about the formatting of the address. How can this issue be resolved开发者_运维问答? The piece of code is as follows:

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


I've had some success using http://msdn.microsoft.com/en-us/library/system.globalization.idnmapping.aspx to deal with that problem.

Something like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜