I recently face a new problem. I send email with the SmtpClient Class everything work fine except for one point. When I want to check the render on Microsoft mail service like hotmail the email is blank but when I check the source the content is there. If I check the mail with other service like GMAIL, Exchange or hotmail email on phone I can see the body part of the message.
Here is my smtp class:
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;
}
}
There when I call my class:
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();
I saw some solution but nothing work for me: https://stackoverflow.com/a/7811002 https://stackoverflow.com/a/10624176
If you need more information you can contact me.
OK I found the problem in my case. In my email HTML part à have comment in the style section (/**/) and I think outlook.com dont like it. When I remove those comment, my email html part was no more blank.