c# – Send mail via .rf domain

Question:

To send mail, I used a fairly simple method of sending.

public bool SendEmail(string UserName, string UserPassword, string emailTo, string subject, string body, bool isBodyHtml)
    {
        if (string.IsNullOrEmpty(emailTo))
        {
            return false;
        }
        using (System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(SERVERSMTP, PORTNOSMTP))
        {

            smtpClient.EnableSsl = false;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;                
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential(UserName, UserPassword);
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress(UserName);
                message.Subject = subject == null ? "" : subject;                    
                message.Body = body == null ? "" : body;                    
                message.IsBodyHtml = isBodyHtml;
                message.To.Add(new MailAddress(emailTo));
                try
                {
                    smtpClient.Send(message);
                    return true;
                }
                catch (Exception exception)
                {
                    //string s1 = exception.Message + "\n" + exception.InnerException + "\n" + exception.Data;
                    throw new FaultException(exception.Message);
                }
            }
        }
    }

Everything works great. But it is necessary to send mail from the .rf domain, and then a problem arose. Unable to send, throws an error

System.Net.WebException: Невозможно разрешить удаленное имя "мойсайт.рф" 

tried to add to code

 smtpClient.DeliveryFormat = SmtpDeliveryFormat.International;

didn't help either, what to do? where is it allowed? Read about:

<uri>
<idn enabled="All"/>
<iriParsing enabled="true"/>

But I can't figure out how to use it in my case?

Answer:

You need to convert the domain to Punycode.

using System.Globalization;

            string unicode = @"россия.рф";
            IdnMapping mapping = new IdnMapping();
            string ascii = mapping.GetAscii(unicode);
            Console.WriteLine(ascii);
            string convertedBackToUnicode = mapping.GetUnicode(ascii);
            Console.WriteLine(convertedBackToUnicode);
Scroll to Top