4

我有以下字符串需要用于确认电子邮件“发件人”地址。这是在 Web 客户端中查看时导致问题的西班牙语翻译。如下图所示。

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

取自资源字符串的值,如下所示

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

在电子邮件客户端中,值变为(应为 Confirmación@test.com)

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

你能看到如何避免这种情况吗?我知道这是因为 ó 但我不知道如何避免这个问题!

谢谢,詹姆斯

更新

完整代码如下:

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

1 回答 1

3

请看下面这个使用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
于 2012-07-24T08:09:30.513 回答