使用SmtpClient通过C#发送HTML电子邮件

时间:2009-08-25 17:59:58

标签: c# asp.net email smtpclient

如何发送HTML电子邮件?我使用this answer中的代码发送带有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?

6 个答案:

答案 0 :(得分:90)

这就是我的工作:

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

请注意,我将邮件消息html设置为true:mail.IsBodyHtml = true;

答案 1 :(得分:18)

我相信它是这样的:

mailObject.IsBodyHtml = true;

答案 2 :(得分:16)

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

答案 3 :(得分:8)

应用Mailbody的正确编码。

mail.IsBodyHtml = true;

答案 4 :(得分:1)

我有一个想法,你可以在你的项目中添加一个复选框,用于发送电子邮件作为html作为用户选项,并添加此代码以启用它:

         MailMessage mail = new MailMessage(from, to, subject, message);

         if(checkBox1.CheckState == CheckState.Checked )
           {
               mail.IsBodyHtml = true;
           }

         SmtpClient client = new SmtpClient("localhost");

         client.Send(mail);

答案 5 :(得分:0)

如果您使用的是 Mailkit ,我们可以将TextBody,HtmlBody和Both用作邮件正文。只需编写此代码。它将为您提供帮助

            MimeMessage mailMessage = new MimeMessage();
            mailMessage.From.Add(new MailboxAddress(senderName, sender@address.com));
            mailMessage.Sender = new MailboxAddress(senderName, sender@address.com);
            mailMessage.To.Add(new MailboxAddress(emailid, emailid));
            mailMessage.Subject = subject;
            mailMessage.ReplyTo.Add(new MailboxAddress(replyToAddress));
            mailMessage.Subject = subject;
            var builder = new BodyBuilder();
            builder.HtmlBody = "Hello There";            
            try
            {
                using (var smtpClient = new SmtpClient())
                {
                    smtpClient.Connect("HostName", "Port", MailKit.Security.SecureSocketOptions.None);
                    smtpClient.Authenticate("user@name.com", "password");

                    smtpClient.Send(mailMessage);
                    Console.WriteLine("Success");
                }
            }
            catch (SmtpCommandException ex)
            {
                Console.WriteLine(ex.ToString());              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());                
            }