Visionz
Visionz

Reputation: 1

ASP.NET Core 6 using email addresses with umlauts ä ü ö throws exception: Requested action not taken: mailbox unavailable

I'm getting this error using the common EmailSender service with email address like test@pläne.de:

An unhandled exception occurred while processing the request. SmtpException: Mailbox unavailable. The server response was: Requested action not taken: mailbox unavailable System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, string response)

I tried with IdnMapping etc. but had no success. Has anybody had a similar issue and found a solution?

Code snippet:

public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
    var smtp = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("Smtp");

    string fromMail = smtp["FromAddress"];
    string fromPassword = smtp["Password"];
    // string userName = smtp["UserName"];

    MailMessage message = new MailMessage();

    // idn mapping
    IdnMapping idn = new IdnMapping();
    message.From = new MailAddress(fromMail);
    // message.From = new MailAddress(idn.GetAscii(fromMail));

    message.Subject = subject;
    message.To.Add(new MailAddress(email));

    message.Body = "<html><body> " + htmlMessage + " </body></html>";
    message.IsBodyHtml = true;

    var smtpClient = new SmtpClient(smtp["Server"])
        {
            Port = Convert.ToInt32(smtp["Port"]),
            //Credentials = new NetworkCredential(userName, fromPassword),
            Credentials = new NetworkCredential(fromMail, fromPassword),
            //Credentials = new NetworkCredential(idn.GetAscii(fromMail), fromPassword),
            EnableSsl = true,
        };

    return smtpClient.SendMailAsync(message);
}

Thanks!

Upvotes: 0

Views: 226

Answers (1)

Chen
Chen

Reputation: 5164

umlauts ä ü ö is an invalid character. And there doesn't seem to be a ready-made solution to this problem.

You can create some sort of translation table, for example:

ö = oe

ä = ae

ü = ue

Maybe this will help you solve the problem.

Upvotes: 0

Related Questions