JayTee
JayTee

Reputation: 1222

Gmail API: Using mailadress containing umlauts results in SMTPEception: The client or server is only configured for addresses with ASCII local-parts

Trying to send an mail with the gmail api to a mail adress that contains umlauts. e.g. bü[email protected]. Getting an SmtpException then:

maildress: GeneralFailure: System.Net.Mail.SmtpException: The client or server is only configured for E-mail addresses with ASCII local-parts: bü[email protected].
bei System.Net.Mail.MailAddress.GetUser(Boolean allowUnicode)
bei System.Net.Mail.MailAddress.GetAddress(Boolean allowUnicode)
bei System.Net.Mail.MailAddress.GetSmtpAddress(Boolean allowUnicode)
bei System.Net.Mail.SmtpClient.ValidateUnicodeRequirement(MailMessage message, MailAddressCollection recipients, Boolean allowUnicode)
bei System.Net.Mail.SmtpClient.Send(MailMessage message)

I know that when I usethe .net smtp client directly you can set SmtpDeliveryFormat.International to be able to. But how to set it when I use the gmail api?

private GmailService _service;

public void SendMail(MailMessage message)
{
    message.SubjectEncoding = Encoding.UTF8;
    message.BodyEncoding = Encoding.UTF8;
    message.IsBodyHtml = true;

    MimeKit.MimeMessage mimeMessage = MimeKit.MimeMessage.CreateFromMailMessage(message);
    Message sendMmessage = new Message {Raw = Base64UrlEncode(mimeMessage)};

    _service.Users.Messages.Send(sendMmessage, _fromMailAdress).Execute();
}

Since I use a google business account I can't use the SMTPClient directly, because google shut down the usage of less secure apps.

Upvotes: 0

Views: 630

Answers (1)

JayTee
JayTee

Reputation: 1222

I solved it using the IdnMapping class

IdnMapping idn = new IdnMapping();

MailMessage message = new MailMessage
{
    From = new MailAddress(_fromMailAdress),
};
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
message.To.Add(idn.GetAscii(toMailAdress));
message.Subject = subject;
message.Body = content;

[email protected] would resolve into [email protected]

Upvotes: 1

Related Questions