2013-10-24 18 views
0

최근에 새로운 문제가 발생합니다. SmtpClient 클래스를 사용하여 전자 메일을 보내면 한 점을 제외하고 모두 잘 작동합니다. Hotmail과 같은 Microsoft 메일 서비스의 렌더링을 확인하려면 이메일이 비어 있지만 소스를 확인하면 내용이 표시됩니다. 전화로 GMAIL, Exchange 또는 hotmail 전자 메일과 같은 다른 서비스로 메일을 확인하면 메시지의 본문 부분을 볼 수 있습니다. 이AlternateView를 사용하는 SmtpClient는 Hotmail Live MSN OUTLOOK.com 서비스의 빈 콘텐츠를 수신합니다.

public class SmtpEmail { 
    private List<string> _to = new List<string>(); 
    private List<string> _cc = new List<string>(); 
    private List<string> _cci = new List<string>(); 
    private List<string> _replyTo = new List<string>(); 
    private List<Attachment> _attachments = new List<Attachment>(); 
    private List<AlternateView> _alternateViews = new List<AlternateView>(); 

    public string Host { get; set; } 
    public string User { get; set; } 
    public string Password { get; set; } 
    public int Port { get; set; } 
    public string From { get; set; } 
    public string FromName { get; set; } 
    public bool IsHTMLBody { get; set; } 
    public string Titre { get; set; } 
    public string Body { get; set; } 

    public List<string> To { 
     get { return _to; } 
    } 

    public List<string> CC { 
     get { return _cc; } 
    } 

    public List<string> CCi { 
     get { return _cci; } 
    } 

    public List<string> ReplyTo { 
     get { return _replyTo; } 
    } 

    public List<Attachment> Attachments { 
     get { return _attachments; } 
    } 

    /// <summary> 
    /// Pour ajouter un message en mode text et un message en mode html, il faut mettre la propriété IsHtmlBody à false, 
    /// mettre le message texte dans le la propriété Body et mettre le message HTML dans un AlternateView comme ceci: 
    /// EX: mail.AlternateViews.Add(System.Net.Mail.AlternateView.CreateAlternateViewFromString(html, new System.Net.Mime.ContentType("text/html"))); 
    /// </summary> 
    public List<AlternateView> AlternateViews { 
     get { return _alternateViews; } 
    } 

    public SmtpEmail(string Host, string User, string Password, int Port, string From) { 
     this.Host = Host; 
     this.User = User; 
     this.Password = Password; 
     this.Port = Port; 
     this.From = From; 
     this.FromName = string.Empty; 
    } 

    /// <summary> 
    /// Constructeur pour l'initialisation de l'envoi du courriel 
    /// </summary> 
    /// <param name="Host">L'adresse du serveur SMPT</param> 
    /// <param name="User">Le nom d'utilisateur du serveur SMPT</param> 
    /// <param name="Password">Le mot de passe du serveur SMPT</param> 
    /// <param name="Port">Le port du serveur SMPT</param> 
    /// <param name="From">L'adresse courriel de provenance du courriel</param> 
    /// <param name="FromName">Nom de remplacement pour le courriel. EX: "Nom de la compagnie" [email protected] </param> 
    public SmtpEmail(string Host, string User, string Password, int Port, string From, string FromName) { 
     this.Host = Host; 
     this.User = User; 
     this.Password = Password; 
     this.Port = Port; 
     this.From = From; 
     this.FromName = FromName; 
    } 

    public bool SendMessage() { 
     MailMessage message = new MailMessage(); 
     if(string.IsNullOrEmpty(this.FromName.Trim())) { 
      message.From = new MailAddress(this.From); 
     } else { 
      message.From = new MailAddress(this.From, this.FromName); 
     } 

     foreach(string email in _to) 
      message.To.Add(new MailAddress(email)); 

     foreach(string email in _cc) 
      message.CC.Add(new MailAddress(email)); 

     foreach(string email in _cci) 
      message.Bcc.Add(new MailAddress(email)); 

     foreach(string email in _replyTo) 
      message.ReplyToList.Add(new MailAddress(email)); 

     foreach(Attachment attachment in Attachments) 
      message.Attachments.Add(attachment); 

     foreach(AlternateView alternateView in AlternateViews) 
      message.AlternateViews.Add(alternateView); 

     if(AlternateViews.Count > 0) { 
      message.Headers.Add("content-type", "multipart/mixed"); 
     } 

     message.IsBodyHtml = this.IsHTMLBody; 
     message.Subject = this.Titre; 
     message.Body = this.Body; 

     SmtpClient client = new SmtpClient(this.Host, this.Port); 
     client.Credentials = new NetworkCredential(this.User, this.Password); 

     try { 
      client.Send(message); 
     } catch { 
      return false; 
     } 

     return true; 
    } 
} 

나는 내 수업 전화 : https://stackoverflow.com/a/7811002 https://stackoverflow.com/a/10624176

경우 :

SmtpEmail mail = new SmtpEmail(Smtp.Host, Smtp.Username, Smtp.Password, Convert.ToInt32(Smtp.Port), Smtp.From, "exemple.com"); 

     mail.To.Add("[email protected]"); 

     mail.Titre = titre; 
     mail.Body = plainText; 

     mail.IsHTMLBody = false; 

     mail.AlternateViews.Add(System.Net.Mail.AlternateView.CreateAlternateViewFromString(HtmlText, Encoding.UTF8, System.Net.Mime.MediaTypeNames.Text.Html)); 

     mail.SendMessage(); 

내가 나를 위해 몇 가지 솔루션 아무것도하지만, 일을 본 다음

내 SMTP 클래스입니다 당신은 저에게 연락 할 수있는 더 많은 정보가 필요합니다.

답변

0

확인 제 경우에 문제가 발견되었습니다. 내 이메일 HTML 부분에서 스타일 섹션 (/ ** /)에 의견이 있으며 outlook.com이 마음에 들지 않는다고 생각합니다. 내가 그 주석을 제거 할 때, 내 이메일 HTML 부분은 더 이상 공백이 아니었다.

0

나는 함께 보낸 PDF 첨부 파일을 제외하고는 HTML 메일이 Hotmail에서 완전히 비어있는 문제가있었습니다. 나는 붙여 넣기 - 보내고 html로 검증 사이트에 내 코드를하여 문제를 해결 :

http://www.freeformatter.com/html-validator.html

그것은 내가 "/"와 헤더에 HTML 메타 태그의 몇 가지를 폐쇄하지 않은 것을 지적했다. 태그를 닫으면 문제가 해결되었습니다.