Visual Studio 如何在邮件正文中包含链接?

dgtucam1  于 2023-02-19  发布在  其他
关注(0)|答案(6)|浏览(112)

我想发送一封带有超链接的电子邮件,我尝试发送没有链接的电子邮件,它的工作,但当我添加链接时,它给出了一个错误
这是我的代码:

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

wribegjk1#

您需要为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

更新看起来是你的字符串构建给你带来了麻烦。有时候,当把字符串放在一起(或者当它被调用时把它们连接起来)时,正确地得到所有的引号是很棘手的。当创建像电子邮件这样的大字符串时,有一些选项可以让它正确。
First,常规字符串-缺点是难以读取

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

字符串构建器文档:http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx

pftdvrlh

pftdvrlh2#

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

mtb9vblg

mtb9vblg3#

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

o.IsBodyHtml = true
drkbr07n

drkbr07n4#

您忘记转义“:href=\”.... "〉登录

cvxl0en2

cvxl0en25#

语法错误:

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

nwo49xxi6#

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

相关问题