gpt4 book ai didi

c# - 如何在邮件正文中包含链接?

转载 作者:太空狗 更新时间:2023-10-29 22:10:45 24 4
gpt4 key购买 nike

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

这是代码:

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

最佳答案

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

关于c# - 如何在邮件正文中包含链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20051731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com