【问题标题】:ASP.NET app to send an mail with a hyperlinkASP.NET 应用程序发送带有超链接的邮件
【发布时间】:2013-05-29 13:11:48
【问题描述】:
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>
  • 确保正文类型是html,然后发送html。

标签: c# asp.net .net


【解决方案1】:
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>
    

    【讨论】:

      【解决方案3】:
      message.Body = string.Format("Click <a href='{0}'>here</a> to login", loginUrl);
      

      【讨论】:

      • 很高兴看到一个适合单独生成的 URL 的示例,而不是使用硬编码参考。
      【解决方案4】:

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

      message.IsBodyHtml = true;

      【讨论】:

        【解决方案5】:
         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元素

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-31
          • 2011-12-01
          • 2022-12-20
          • 1970-01-01
          • 1970-01-01
          • 2012-10-21
          • 2012-08-10
          相关资源
          最近更新 更多