2012-07-24 158 views
4

我有以下字符串,我需要用於確認電子郵件地址。這是西班牙語翻譯,當在Web客戶端中查看時導致問題。如下所示。電子郵件地址編碼問題

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

值從資源串採取以下

<data name="BookingConfirmation_FromAddress" xml:space="preserve"> 
    <value>Confirmació[email protected]</value> 
    </data> 

顯示在電子郵件客戶端的值變爲(應該是Confirmació[email protected]

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

你能看到避免這種情況?我知道這是因爲ó,但我不知道如何避免這個問題!

謝謝, 詹姆斯下面

UPDATE

全碼:

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

謝謝但我試過ECNcode /解碼但它不起作用 – 2012-07-24 08:15:08

+0

你可以顯示代碼如何從資源字符串中獲取價值? – 2012-07-24 08:27:28

+0

這實際上是一個有效的電子郵件地址嗎? – 2012-07-24 08:29:54

回答

3

請看下面使用System.Uri類的方法這個例子,希望它可以幫助你

string email = "Confirmació[email protected]"; 

string escaped = 
     System.Uri.EscapeDataString(email); // Confirmaci%C3%B3n%40test.com 
string unescaped = 
     System.Uri.UnescapeDataString(email); //Confirmació[email protected] 
+0

我在運行時收到以下錯誤消息(我試過EscapeDataString和UnescapeDataString):「指定的字符串不是電子郵件地址所需的格式。」 – 2012-07-24 08:18:33

+0

雷德福先生請你可以向我展示違規代碼 – HatSoft 2012-07-24 08:19:04

+0

@JamesRadford是否以這種方式使用System.Uri.method(email); – HatSoft 2012-07-24 08:22:39