以下は、私が毎日使用している SMTP ヘルパーの抜粋です....
public bool SendMail(string strTo, string strFrom, string strCc, string strBcc, string strBody, string strSubject)
{
bool isComplete = true;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
//Default port will be 25
smtpClient.Port = 25;
message.From = new MailAddress(smtpEmailSource);
message.To.Add(strTo);
message.Subject = strSubject;
if (strCc != "") { message.Bcc.Add(new MailAddress(strCc)); }
if (strBcc != "") { message.Bcc.Add(new MailAddress(strBcc)); }
message.IsBodyHtml = true;
string html = strBody; //I usually use .HTML files with tags (e.g. {firstName}) I replace with content. This allows me to edit the emails in VS by opening a .HTML file and it's easy to do string replacements.
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, new ContentType("text/html"));
message.AlternateViews.Add(htmlView);
// Send SMTP mail
smtpClient.Send(message);
}
catch
{
isComplete = false;
}
return isComplete;
}
[アップデート]
私が最初に中断した重要なポイント...
IsBodyHtml は、メッセージが HTML 形式であることを示しています。HTML の 1 つのビューのみを送信する場合は、これで十分です。
AlternateView は HTML を保存するために使用されます。これは HTML メッセージの送信には必要ありませんが、受信者が HTML をレンダリングできない場合に備えて、HTML とプレーン テキストを含むメッセージを送信する場合に必要です。
上記のplainViewを取り出したので、これは明らかではありません、申し訳ありません...
ここで重要なのは、HTML 形式のメッセージを送信する場合は、IsBodyHtml = true (デフォルトは false) を使用して、コンテンツを HTML としてレンダリングする必要があることです。