ASP.NET 应用程序发送带有超链接的邮件

本文关键字:超链接 NET 应用程序 ASP | 更新日期: 2023-09-27 18:35:00

MailMessage message = new MailMessage();
message.From = new MailAddress("hkar@gmail.com");
message.Subject = "Subject";
message.Body = "Please login";
SmtpClient smtp = new SmtpClient();
message.To.Add("karaman@gmail.com");                  
smtp.Send(message);

我想在已发送邮件的正文中有一个超链接,上面写着"登录"。我该怎么做?

ASP.NET 应用程序发送带有超链接的邮件

message.Body = "Please <a href='"http://www.example.com/login.aspx'">login</a>";

不过,请确保在发送时突出显示内容是 HTML。

message.IsBodyHTML = true;

将消息设置为 message.IsBodyHTML = true

<a href="http://YourWebsite.Com">Login</a>
message.Body = string.Format("Click <a href='{0}'>here</a> to login", loginUrl);

将邮件的格式设置为 HTML,并确保将邮件上的 IsBodyHtml 属性设置为 true:

消息。IsBodyHtml = true;

 System.Text.StringBuildersb = new System.Text.StringBuilder();
 System.Web.Mail.MailMessage mail = new System.Mail.Web.MailMessage(); 
 mail.To = "recipient@address"; 
 mail.From = "sender"; 
 mail.Subject = "Test"; 
 mail.BodyFormat = System.Web.Mail.MailFormat.Html;
 sb.Append("<html><head><title>Test</title><body>"); //HTML content which you want to send
 mail.Body = sb.ToString();
 System.Web.Mail.SmtpMail.SmtpServer = "localhost"; //Your Smtp Server 
 System.Web.Mail.SmtpMail.Send(mail); 

您只需要将正文的格式设置为html,然后就可以在邮件消息中添加html元素