0

我最近面临一个新问题。我使用 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

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

4

2 回答 2

0

好的,我发现了我的问题。在我的电子邮件 HTML 部分中,在样式部分 (/**/) 中有评论,我认为 outlook.com 不喜欢它。当我删除这些评论时,我的电子邮件 html 部分不再是空白。

于 2013-12-09T20:02:58.747 回答
0

我遇到的问题是,除了一起发送的 pdf 附件,Hotmail 中的 html 电子邮件完全为空。我通过将代码粘贴到 html 验证站点来解决了这个问题:

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

它指出我没有用“/”关闭标题中的几个 html 元标记。关闭标签解决了这个问题。

于 2014-06-20T00:15:41.703 回答