Email Encoding Issues

I have the following line that I need to use for a verification email address. This is a Spanish translation that causes a problem when viewing in the web client. The following is shown.

model.FromAddresses = new EmailAddress { Address = fromAddressComponents[0], DisplayName = fromAddressComponents[1] }; 

The value taken from the resource string shown below

  <data name="BookingConfirmation_FromAddress" xml:space="preserve"> <value>Confirmació n@test.com </value> </data> 

In the mail client, the value becomes (must be Confirmació n@test.com )

 =?utf-8?Q?Confirmaci=C3=B3n 

Do you see how to avoid this? I know this because - but I'm not sure how to avoid this problem!

Thanks James

UPDATE

Full code below:

 public void Send(MailAddress to, MailAddress from, string subject, string body) { var message = new MailMessage { BodyEncoding = new System.Text.UTF8Encoding(true), From = from, Subject = subject, Body = body, IsBodyHtml = true }; message.To.Add(to.Address); var smtp = new SmtpClient() { Timeout = 100000 }; try { smtp.Send(message); } finally { if (smtp.DeliveryMethod == SmtpDeliveryMethod.Network) smtp.Dispose(); } } // New Version // -------------------------- public override void Handle(SendEmailEmailCommand emailCommand) { if(!_config.SendEmail) { return; } var to = new MailAddress(emailCommand.To.Address, emailCommand.To.DisplayName); var from = new MailAddress(emailCommand.From.Address, Uri.UnescapeDataString(emailCommand.From.DisplayName)); try { var msgBody = _compressor.Decompress(emailCommand.Body); _emailClient.Send(to, from, emailCommand.Subject, msgBody); } catch (Exception ex) { _logger.Error("An error occurred when trying to send an email", ex); throw; } } public void Send(MailAddress to, MailAddress from, string subject, string body) { var message = new MailMessage { BodyEncoding = new System.Text.UTF8Encoding(true), From = from, Subject = subject, Body = body, IsBodyHtml = true }; message.To.Add(to.Address); if (!string.IsNullOrWhiteSpace(_config.BccEmailRecipents)) { message.Bcc.Add(_config.BccEmailRecipents); } SendEmail(message); if (_config.BackupEmailsEnabled) { SendBackupEmail(message); } } private void SendEmail(MailMessage message) { var smtp = new SmtpClient() { Timeout = 100000 }; try { smtp.Send(message); } finally { if (smtp.DeliveryMethod == SmtpDeliveryMethod.Network) smtp.Dispose(); } } 
+4
source share
1 answer

Please check out this example below using System.Uri methods, hoping this helps you

 string email = "Confirmació n@test.com "; string escaped = System.Uri.EscapeDataString(email); // Confirmaci%C3%B3n%40test.com string unescaped = System.Uri.UnescapeDataString(email); //Confirmació n@test.com 
+3
source

All Articles