简体   繁体   English

电子邮件地址编码问题

[英]Email address encoding issues

I have the following string I need to use for a confirmation email 'from' address. 我需要使用以下字符串来发送'来自'地址的确认电子邮件。 This is the spanish translation which is causing a problem when viewing within a web client. 这是在网络客户端中查看时导致问题的西班牙语翻译。 Shown below. 如下所示。

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

Value taken from a resource string, shown below 从资源字符串中获取的值,如下所示

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

Within the email client the value becomes (should be Confirmación@test.com) 在电子邮件客户端中,值变为(应为Confirmación@test.com)

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

Can you see how to avoid this? 你能看到如何避免这种情况吗? I know it's because of the ó but I'm not sure how to avoid this problem! 我知道这是因为ó但我不确定如何避免这个问题!

Thanks, James 谢谢,詹姆斯

UPDATE 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();
        }
    }

Please look at this example below using System.Uri class methods, hope it helps you 请使用System.Uri类方法查看下面的示例,希望对您有所帮助

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM