最近、新たな問題に直面しています。SmtpClient クラスを使用してメールを送信すると、1 つの点を除いてすべて正常に動作します。hotmail のような Microsoft メール サービスでレンダリングを確認したい場合、電子メールは空白ですが、ソースを確認するとコンテンツがそこにあります。電話で 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
さらに情報が必要な場合は、私に連絡してください。