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

发布于 2024-08-03 02:17:08 字数 483 浏览 6 评论 0原文

如何发送 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?

How do I send an HTML email? I use the code in this answer to send emails with SmtpClient, but they're always plain text, so the link in the example message below is not formatted as such.

<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>

How do I enable HTML in the e-mail messages I send?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

落花随流水 2024-08-10 02:17:08

这就是我所做的:

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

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

This is what I do:

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

Note that I set the mail message html to true: mail.IsBodyHtml = true;

ζ澈沫 2024-08-10 02:17:08

我相信它是这样的:

mailObject.IsBodyHtml = true;

I believe it was something like:

mailObject.IsBodyHtml = true;
你在我安 2024-08-10 02:17:08

IsBodyHtml = true无疑是最重要的部分。

但是,如果您想提供一封电子邮件,其中同时包含文本/纯文本部分和文本/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

IsBodyHtml = true is undoubtedly the most important part.

But if you want to provide an email with both a text/plain part and a text/html part composed as alternates, it is also possible using the AlternateView class:

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
纵山崖 2024-08-10 02:17:08

应用邮件正文的正确编码。

mail.IsBodyHtml = true;

Apply the correct encoding of the Mailbody.

mail.IsBodyHtml = true;
红颜悴 2024-08-10 02:17:08

我有一个想法,您可以在项目中添加一个复选框,以作为用户的选项以 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);

i have an idea , you can add a check box to your project for sending email as html as an option for user , and add this code to enable it :

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

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

         SmtpClient client = new SmtpClient("localhost");

         client.Send(mail);
盗琴音 2024-08-10 02:17:08

如果您使用Mailkit,我们可以使用TextBody、HtmlBody 和两者作为邮件正文。只需编写这段代码即可。它会帮助你

            MimeMessage mailMessage = new MimeMessage();
            mailMessage.From.Add(new MailboxAddress(senderName, [email protected]));
            mailMessage.Sender = new MailboxAddress(senderName, [email protected]);
            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";
            mailMessage.Body = builder.ToMessageBody();            
            try
            {
                using (var smtpClient = new SmtpClient())
                {
                    smtpClient.Connect("HostName", "Port", MailKit.Security.SecureSocketOptions.None);
                    smtpClient.Authenticate("[email protected]", "password");

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

If you are using Mailkit,We can use TextBody,HtmlBody and Both for message body. Just write this code. It will help you

            MimeMessage mailMessage = new MimeMessage();
            mailMessage.From.Add(new MailboxAddress(senderName, [email protected]));
            mailMessage.Sender = new MailboxAddress(senderName, [email protected]);
            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";
            mailMessage.Body = builder.ToMessageBody();            
            try
            {
                using (var smtpClient = new SmtpClient())
                {
                    smtpClient.Connect("HostName", "Port", MailKit.Security.SecureSocketOptions.None);
                    smtpClient.Authenticate("[email protected]", "password");

                    smtpClient.Send(mailMessage);
                    Console.WriteLine("Success");
                }
            }
            catch (SmtpCommandException ex)
            {
                Console.WriteLine(ex.ToString());              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());                
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文