2013-04-27 3 views
4

전자 메일을 잘못 인코딩하는 클라이언트가 있습니다. System.Net.Mail 클래스를 사용하고 본문 인코딩을 UTF-8로 설정하고 있습니다. 나는 약간의 독해를했기 때문에 전자 메일의 본문을 UTF-8 바이트 배열로 인코딩하는 문자열로 설정해야하므로 변환해야하므로 UTF- 16. 옳은?전자 메일의 UTF-8 인코딩

내가 보낼 때 : 위원장 s'agit 디부 유엔 메시지 드 테스트가 난 보좌관 데 caractères 프랑스어 르 네이 팅 시스템 버지니아 gérer 레 메시지 envoyés 주석을 결정 붓는다. Merci et bon journée.

그들은 참조 : * 김정일 s'agit 디부 유엔 메시지 드 테스트 언급 terminer 다 ©을 부어 르 système 버지니아 조지아 © 총통 레 메시지 envoyà ©의 à 난 보좌관 데 caractères franç ais.

메르 등 봉 journà © 전자. *

내가 이메일에 다른 인코딩을 시도,하지만 난 정말 클라이언트 잘못이 올바르게 내 이메일 클라이언트를 표시하기 때문에 이메일을 디코딩 것을 의심하고있다. 내가 놓친 게 있니? 할 수있는 다른 것이 있습니까?

코드

SmtpMailService.SendMail(string.Empty, toAddress, "[email protected]", "", subject, sb.ToString(), false); 

public static bool SendMail(string fromAddress, string toAddress, string ccAddress, string bccAddress, string subject, string body, bool isHtmlBody) 
    { 

     if (String.IsNullOrEmpty(toAddress)) return false; 
     var toMailAddress = new MailAddress(toAddress); 

     var fromMailAddress = new MailAddress(String.IsNullOrEmpty(fromAddress) ? DefaultFromAddress : fromAddress); 

     var mailMessage = new MailMessage(fromMailAddress, toMailAddress); 

     if (!String.IsNullOrEmpty(ccAddress)) 
     { 
      mailMessage.CC.Add(new MailAddress(ccAddress)); 
     } 

     if (!String.IsNullOrEmpty(bccAddress)) 
     { 
      mailMessage.Bcc.Add(new MailAddress(bccAddress)); 
     } 

     if (!string.IsNullOrEmpty(fromAddress)) mailMessage.Headers.Add("Reply-To", fromAddress); 

     mailMessage.Subject = subject; 
     mailMessage.IsBodyHtml = isHtmlBody; 
     mailMessage.Body = body; 
     mailMessage.BodyEncoding = System.Text.Encoding.UTF8; 

     var enableSslCfg = ConfigurationManager.AppSettings["Email.Ssl"]; 
     var enableSsl = string.IsNullOrEmpty(enableSslCfg) || bool.Parse(enableSslCfg); 
     var client = new SmtpClient {EnableSsl = enableSsl}; 

     client.Send(mailMessage); 


     return true; 
    } 
+0

'누락 된 것이 있습니까?'코드가 표시 될 수 있습니까? – I4V

+0

표시 할 코드가 실제로 없다. 추상적 인 질문이 더 많지만 어쨌든 추가 할 것이다. –

+0

이 메서드에 전달 될 때'body'는 UTF8로 인코딩 된 문자열인가? –

답변

1

아래에있는 유일한 방법은 당신이 문자지도를 사용으로 적합한 문자로 문자를 대체하고 HTML은 다른 쓰레기 문자 코드를 인코딩한다

수정할 수 있습니다 다음과 같은 코드 :

WebClient myClient = new WebClient(); 

     byte[] requestHTML; 
     string currentPageUrl = HttpContext.Current.Request.Url.AbsoluteUri; 
     UTF8Encoding utf8 = new UTF8Encoding(); 
     requestHTML = myClient.DownloadData(currentPageUrl); 
     var str = utf8.GetString(requestHTML);" 
     str = str.Replace("’", "'"); 
1

귀하의 예는 귀하가 보내는 텍스트가 UTF-8 인코더 ng는 Windows-1252 또는 ISO-8859-1 인코딩으로 해석됩니다 (여러 인코딩 오류 결과, 특히 테이블 here의 마지막 행 참조).

코드가 괜찮은 것 같습니다. 문제는 클라이언트 쪽이 메일 본문의 인코딩을 무시한다는 것입니다 (예를 들어, HTML 페이지에 HTML을 포함시키고 래퍼 HTML이 기본 ISO-8859-1을 사용하기 때문에). 부호화).

다른 공급 업체 (예 : Gmail)에게 메일을 보내고 결과를 확인하십시오. 잘 구성된 공급자는 본문 인코딩을 처리해야합니다. 가정 위에 해당하는 경우

해결 방법 1

오류는 클라이언트 측에 고정해야합니다.그들은 몸 인코딩을 처리해야 하나 또는 항상 UTF-8 인 경우, 그들은 단순히 HTML 헤더에 다음 태그를 추가해야합니다 : 당신의 클라이언트 측에 영향을 미칠 수없는 경우

<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

해결 방법 2

모든 비용은 다른 인코딩을 사용하려고 할 수 있습니다 :

mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1"); 

을 유의하시기 바랍니다 당신의 프랑스어 예를 들어 이런 식으로 올바른 것입니다; 그러나 서유럽 어 이외의 언어는 올바르게 표시되지 않습니다.

+0

Gmail에서 문제가 발생하지 않지만 Outlook에서는 문제가 발생합니다. – edosoft

+0

Outlook에서 메일을 열고 작업/기타 작업/인코딩을 클릭하십시오. 무엇이 선정 되었습니까? 서유럽 어인 경우 UTF-8로 전환하십시오. 항상 잘못된 설정이 자동으로 감지되면 작업/기타 작업/소스보기에서 소스를 확인하십시오. 여기서 HTML 또는 일반 텍스트 본문을 볼 수 있습니다. charset 또는 lang은 어디에서 정의 되었습니까? – taffer

+0

주 : MailMessage 클래스는 메일 보드에서이 META 태그를 제거합니다. 그래서 우리는 다음을 사용해야합니다 : mail.Headers.Add ("Content-Type", "content = text/html; charset = \"UTF-8 \ ""); – SalientBrain

1

대체로 내 문제를 해결 한 것은 대체보기에서 콘텐츠 유형을 설정하는 것이 었습니다. msg.BodyEncoding을 설정하는 것만으로도 Gmail이 이메일을 올바르게 표시했지만 Outlook에서 작동하지 않았습니다.

var msg = new MailMessage() 
    msg.BodyEncoding = Encoding.GetEncoding(1252); 
    msg.IsBodyHtml = true; 
    //htmlBody is a string containing the entire body text 
    var htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, new ContentType("text/html")); 
    //This does the trick 
    htmlView.ContentType.CharSet = Encoding.UTF8.WebName; 
    msg.AlternateViews.Add(htmlView); 
+0

새로운 ContentType을 작성하는 동안 charset을 직접 설정할 수도 있습니다. new ContentType ("text/html; charset = UTF8"); – sucil