如何在电子邮件正文消息中添加链接?

7
我希望发送一封包含超链接的电子邮件,我试过发送不带链接的电子邮件可以成功,但是添加链接后出现错误。
以下是我的代码:
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);

2
那个链接标记必须是正文字符串的一部分,不能只是“挂在那里”。 - Hans Kesting
1
如果你仔细观察StackOverflow引擎在你发布代码时为你做的格式化,就会发现一个问题确实跃然纸上。 - JMK
@JMK 我认为视觉上将 "+<a href="" 解释为字符串,因此真正的字符串的其余部分会出错,我不知道如何同时处理链接和字符串。 - F 505
6个回答

15
您需要像下面这样启用邮件消息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/zh-cn/library/system.text.stringbuilder(v=vs.110).aspx


谢谢Mikael,我认为可视化解释此“+<a href=" 为字符串,因此其余的真实字符串会出现错误。我不知道如何同时处理链接和字符串。 - F 505
1
@F505 - 那个链接没什么特别的。此时标记只是字符串内容。只有在邮件客户端中,该HTML正文被解释为“HTML”,然后才成为一个链接。 - Hans Kesting

3
     String body = "ur message : <a href='http://www.yoursite.com'></a>"
     o.Body = body;

o.IsBodyHtml = true

3

将消息标记为HTML格式 o.IsBodyHtml = true


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

1

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


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

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