簡體   English   中英

具有AlternateView的C#SmtpClient在Hotmail Live MSN OUTLOOK.com服務上接收空白內容

[英]c# SmtpClient with AlternateView receive blank content on Hotmail Live MSN OUTLOOK.com service

我最近遇到了一個新問題。 我發送帶有SmtpClient類的電子郵件,除了一點以外,其他所有內容都正常運行。 當我想檢查Microsoft郵件服務(如hotmail)上的渲染時,電子郵件為空白,但是當我檢查源時,內容在那里。 如果我通過電話上的GMAIL,Exchange或hotmail電子郵件等其他服務檢查郵件,則可以看到郵件的正文部分。

這是我的smtp類別:

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" no-reply@nomdelacompagnie.com </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;
    }
}

當我給班級打電話時:

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

        mail.To.Add("e@e.com");

        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();

我看到了一些解決方案,但對我沒有用: https : //stackoverflow.com/a/7811002 https://stackoverflow.com/a/10624176

如果您需要更多信息,可以與我聯系。

好的,我發現了我的問題。 我的電子郵件HTML部分à在樣式部分(/ ** /)中有注釋,我認為outlook.com不喜歡它。 當我刪除這些評論時,我的電子郵件html部分不再空白。

我有一個問題,就是HTML電子郵件在Hotmail中完全空了,除了用於發送的pdf附件。 我通過將代碼粘貼到html驗證站點中解決了該問題:

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

它指出我沒有在標頭中使用“ /”關閉幾個html meta標簽。 關閉標簽可以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM