我需要使用以下字符串来自'地址'的确认电子邮件。这是在网络客户端中查看时导致问题的西班牙语翻译。如下所示。
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();
}
}
答案 0 :(得分: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