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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1329922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:13:22  来源:igfitidea点击:

Send HTML email via C# with SmtpClient

c#asp.netemailsmtpclient

提问by

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

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

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

如何在我发送的电子邮件中启用 HTML?

回答by Bdiem

Apply the correct encoding of the Mailbody.

应用正确的 Mailbody 编码。

mail.IsBodyHtml = true;

回答by Ropstah

I believe it was something like:

我相信它是这样的:

mailObject.IsBodyHtml = true;

回答by Josiah Peters

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;

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

回答by nassimlouchani

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 :

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

回答by faester

IsBodyHtml = trueis undoubtedly the most important part.

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

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

但是,如果您想提供包含文本/纯文本部分和文本/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

回答by Naveen

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

如果您使用Mailkit,我们可以使用 TextBody、HtmlBody 和 Both 作为消息正文。只需编写此代码。它会帮助你

            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());                
            }