51

HTML メールを送信するにはどうすればよいですか? この回答のコードを使用して で電子メールを送信しSmtpClientますが、それらは常にプレーン テキストであるため、以下のメッセージ例のリンクはそのようにフォーマットされていません。

<p>Welcome to SiteName. To activate your account, visit this URL: <a href="http://SiteName.com/a?key=1234">http://SiteName.com/a?key=1234</a>.</p>

送信する電子メール メッセージで HTML を有効にするにはどうすればよいですか?

4

6 に答える 6

100

これが私がすることです:

MailMessage mail = new MailMessage(from, to, subject, message);
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient("localhost");
client.Send(mail);

メール メッセージの html を true に設定していることに注意してください。mail.IsBodyHtml = true;

于 2009-08-25T18:02:38.607 に答える
19

IsBodyHtml = true間違いなく最も重要な部分です。

しかし、代替として構成された text/plain 部分と text/html 部分の両方を含むメールを提供したい場合は、次のAlternateViewクラスを使用することもできます。

MailMessage msg = new MailMessage();
AlternateView plainView = AlternateView
     .CreateAlternateViewFromString("Some plaintext", Encoding.UTF8, "text/plain");
// We have something to show in real old mail clients.
msg.AlternateViews.Add(plainView); 
string htmlText = "The <b>fancy</b> part.";
AlternateView htmlView = 
     AlternateView.CreateAlternateViewFromString(htmlText, Encoding.UTF8, "text/html");
msg.AlternateViews.Add(htmlView); // And a html attachment to make sure.
msg.Body = htmlText;  // But the basis is the html body
msg.IsBodyHtml = true; // But the basis is the html body
于 2014-12-10T23:54:32.057 に答える