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

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

我想在发送邮件的正文中添加一个超链接,文字显示为“登录”。我该怎么做?


如果正文是HTML,则使用HTML:<a href="...">login</a> - Patryk Ćwiek
确保 body 类型为 html,然后只需发送 html。 - asawyer
5个回答

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

确保在发送时突出显示内容为HTML。

message.IsBodyHTML = true;

2

将消息设置为message.IsBodyHTML = true

<a href="http://YourWebsite.Com">Login</a>

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

很高兴看到一个适用于单独生成的URL的示例,而不是使用硬编码引用。 - Gary Stacey

0
 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元素。

0
将消息格式化为HTML,并确保在MailMessage上设置IsBodyHtml属性为true:
message.IsBodyHtml = true;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接