【问题标题】:How to include link in a mail body message?如何在邮件正文中包含链接?
【发布时间】:2013-11-18 15:39:28
【问题描述】:

我想发送一封带有超链接的电子邮件,我尝试发送没有链接的电子邮件并且它有效,但是当我添加链接时它给出了一个错误

这是代码:

MailMessage o = new MailMessage("f@hotmail.com", "f@hotmail.com", "KAUH Account Activation", "Hello, " + name + "\n Your KAUH Account about to activate click the link below to complete the actination process \n "+<a href=\"http://localhost:49496/Activated.aspx">login</a>);
NetworkCredential netCred = new NetworkCredential("f@hotmail.com", "****");
SmtpClient smtpobj = new SmtpClient("smtp.live.com", 587);
smtpobj.EnableSsl = true;
smtpobj.Credentials = netCred;
smtpobj.Send(o);

【问题讨论】:

  • 链接标记必须是正文字符串的一部分,它不能只是“闲逛”
  • 如果您在发布代码时仔细查看 StackOverflow 引擎为您执行的格式设置,您会发现一个问题。
  • @JMK 我认为视觉将这个 "+

标签: c# .net visual-studio smtp visual-studio-2013


【解决方案1】:

您需要为 MailMessage 的正文启用 HTML,如下所示:

o.IsBodyHtml = true;

也许您应该选择另一个构造函数,以使代码更具可读性。可能是这样的:

var mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender@domain.com", "Customer Service");
mailMessage.To.Add(new MailAddress("someone@domain.com"));
mailMessage.Subject = "A descriptive subject";
mailMessage.IsBodyHtml = true;
mailMessage.Body = "Body containing <strong>HTML</strong>";

完整文档:http://msdn.microsoft.com/en-us/library/System.Net.Mail.MailMessage(v=vs.110).aspx

更新 似乎是您的字符串构建给您带来了麻烦。有时,将字符串放在一起(或将它们连接起来)时,要使所有引号都正确是很棘手的。在创建像电子邮件这样的大字符串时,有一些选项可以让它正确。

首先,常规字符串 - 缺点是难以阅读

string body = "Hello, " + name + "\n Your KAUH Account about to activate click the link below to complete the actination process \n <a href=\"http://localhost:49496/Activated.aspx">login</a>";

第二,逐字字符串 - 允许代码中的换行符提高可读性。请注意开头的 @ 字符,并且引号转义序列从 \" 更改为 ""

string body = @"Hello, " + name + "\n Your KAUH Account about to
    activate click the link below to complete the actination process \n 
    <a href=""http://localhost:49496/Activated.aspx"">login</a>"

第三,字符串生成器。这实际上是许多方面的首选方式。

var body = new StringBuilder();
body.AppendFormat("Hello, {0}\n", name);
body.AppendLine(@"Your KAUH Account about to activate click 
    the link below to complete the actination process");
body.AppendLine("<a href=\"http://localhost:49496/Activated.aspx\">login</a>");
mailMessage.Body = body.ToString();

StringBuilder 文档:http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx

【讨论】:

  • 谢谢 Mikael,我认为视觉上会将这个 "+
  • @F505 - 这个链接没什么特别的。此时标记只是字符串内容。只有在邮件客户端,html body 被解释为“html”,然后它变成了一个链接。
【解决方案2】:

将消息标记为 html o.IsBodyHtml = true

【讨论】:

    【解决方案3】:
         String body = "ur message : <a href='http://www.yoursite.com'></a>"
         o.Body = body;
    
    o.IsBodyHtml = true
    

    【讨论】:

      【解决方案4】:

      您忘记转义 " : href=\" .... \">登录

      【讨论】:

        【解决方案5】:

        语法错误:

        MailMessage o [...snip...] \n "+<a href=\"http://localh [...snip...]
                                      ^--terminates the string
                                        ^^^^^^^^^^^^^^--interpreted as code
        

        【讨论】:

          【解决方案6】:
          string url = lblHidOnlineURL.Value + hidEncryptedEmpCode.Value;
          body = hid_EmailBody.Value.Replace("@Compting", "HHH").Replace("@toll", hid_TollFreeNo.Value).Replace("@llnk", "<a style='font-family: Tahoma; font-size: 10pt; color: #800000; font-weight: bold' href='http://" + url + "'>click here To Download</a>");
          

          【讨论】:

            猜你喜欢
            • 2014-12-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-16
            • 2020-10-08
            • 2023-02-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多