在使用 c# 发送的电子邮件中插入链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15754319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert a link in to a email send using c#
提问by user2234111
I develop a program to send emails automatically using c#, and I want to insert a link to a web site to that email. How can I do it?
我开发了一个使用 c# 自动发送电子邮件的程序,我想插入一个指向该电子邮件的网站链接。我该怎么做?
public bool genarateEmail(String from, String to, String cc, String displayName,
String password, String subjet, String body)
{
bool EmailIsSent = false;
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
try
{
m.From = new MailAddress(from, displayName);
m.To.Add(new MailAddress(to, displayName));
m.CC.Add(new MailAddress("[email protected]", "Display name CC"));
m.Subject = subjet;
m.IsBodyHtml = true;
m.Body = body;
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new
System.Net.NetworkCredential(from, password);
sc.EnableSsl = true;
sc.Send(m);
EmailIsSent = true;
}
catch (Exception ex)
{
EmailIsSent = false;
}
return EmailIsSent;
}
I want to send a link through this email. How should I add it to email?
我想通过这封电子邮件发送一个链接。我应该如何将其添加到电子邮件中?
采纳答案by Gayashan
String body = "Your message : <a href='http://www.example.com'></a>"
m.Body = body;
回答by David Hoerster
You should be able to just add the mark-up for the link in your body
variable:
您应该能够在body
变量中添加链接的标记:
body = "blah blah <a href='http://www.example.com'>blah</a>";
body = "blah blah <a href='http://www.example.com'>blah</a>";
You shouldn't have to do anything special since you're specifying your body contains HTML (m.IsBodyHtml = true
).
您不必做任何特别的事情,因为您指定的正文包含 HTML ( m.IsBodyHtml = true
)。
回答by James Moring
Within the body. This will require that the body be constructed as HTML so the that a or can be used to render your link. You can use something like StringTemplate to generate the html including your link.
体内。这将要求将正文构造为 HTML,以便 a 或 可用于呈现您的链接。您可以使用 StringTemplate 之类的东西来生成包含链接的 html。
回答by user7767814
For some dynamic links, the email service providers will not show your link into email body if the link not prepend http (security issues) like localhost:xxxx/myPage
对于某些动态链接,如果链接没有像 localhost:xxxx/myPage 这样的 http(安全问题),则电子邮件服务提供商不会将您的链接显示在电子邮件正文中
m.body = "<a href='http://" + Request.Url.Authority + "/myPage'>click here</a>"