在 MailMessage 对象中添加 Html link
Adding Html link in MailMessage Object
我想在邮件消息对象中提供一个 link。尝试谷歌搜索找不到简单更好的东西。
我有以下代码
public ActionResult Register(Customer customer)
{
if (ModelState.IsValid)
{
var count = db.Customers.Where(x => x.Email == customer.Email).Count();
if (count > 0)
{
@ViewBag.Error = "This mail already exists";
return View();
}
db.Customers.Add(customer);
db.SaveChanges();
MailMessage msgobj = new MailMessage();
SmtpClient serverobj = new SmtpClient();
serverobj.Credentials = new NetworkCredential(customer.Email,customer.Password);
serverobj.Port = 587;
serverobj.Host = "Smtp.gmail.com";
serverobj.EnableSsl = true;
msgobj.From = new MailAddress(customer.Email, "Shopper's Stop", System.Text.Encoding.UTF8);
msgobj.To.Add(customer.Email);
msgobj.Subject = "Account Activate Link";
msgobj.Body = GetFormattedMessageHTML(customer);
msgobj.IsBodyHtml = true;
msgobj.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
serverobj.Send(msgobj);
return RedirectToAction("Products", "Home");
}
return View();
}
private String GetFormattedMessageHTML(Customer customer)
{
return "<!DOCTYPE html> " +
"<html xmlns=\"http://www.w3.org/1999/xhtml\">" +
"<head>" +
"<title>Email</title>" +
"</head>" +
"<body style=\"font-family:'Century Gothic'\">" +
"<h1 style=\"text-align:center;\"> " + "</h1>" +
"<h2 style=\"font-size:14px;\">" +
"Name : " + customer.First_Name + " " + customer.Last_Name + "<br />" +
"Company : " + "NewTech Software" + "<br />" +
"Email : " + customer.Email + "<br />" +
//similar to this
//and when clicked how to perform further action
"Activation Link" + "<a href="+"some link"+"sometext to be clicked"+customer.Cust_Id +"></a>"+
"</h2>" +
"</body>" +
"</html>";
}
任何人都可以帮助我。上面的代码工作正常,但我无法生成 link 以及其他应该与 cust_Id 一起传递的内容以在单击时进行验证。
谢谢
试试这个
替换
"<a href="+"some link"+"sometext to be clicked"+customer.Cust_Id +"></a>"
至
"<a href=\""+your_url+"\">clickable text</a>"
这里string your_url="";
我想在邮件消息对象中提供一个 link。尝试谷歌搜索找不到简单更好的东西。 我有以下代码
public ActionResult Register(Customer customer)
{
if (ModelState.IsValid)
{
var count = db.Customers.Where(x => x.Email == customer.Email).Count();
if (count > 0)
{
@ViewBag.Error = "This mail already exists";
return View();
}
db.Customers.Add(customer);
db.SaveChanges();
MailMessage msgobj = new MailMessage();
SmtpClient serverobj = new SmtpClient();
serverobj.Credentials = new NetworkCredential(customer.Email,customer.Password);
serverobj.Port = 587;
serverobj.Host = "Smtp.gmail.com";
serverobj.EnableSsl = true;
msgobj.From = new MailAddress(customer.Email, "Shopper's Stop", System.Text.Encoding.UTF8);
msgobj.To.Add(customer.Email);
msgobj.Subject = "Account Activate Link";
msgobj.Body = GetFormattedMessageHTML(customer);
msgobj.IsBodyHtml = true;
msgobj.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
serverobj.Send(msgobj);
return RedirectToAction("Products", "Home");
}
return View();
}
private String GetFormattedMessageHTML(Customer customer)
{
return "<!DOCTYPE html> " +
"<html xmlns=\"http://www.w3.org/1999/xhtml\">" +
"<head>" +
"<title>Email</title>" +
"</head>" +
"<body style=\"font-family:'Century Gothic'\">" +
"<h1 style=\"text-align:center;\"> " + "</h1>" +
"<h2 style=\"font-size:14px;\">" +
"Name : " + customer.First_Name + " " + customer.Last_Name + "<br />" +
"Company : " + "NewTech Software" + "<br />" +
"Email : " + customer.Email + "<br />" +
//similar to this
//and when clicked how to perform further action
"Activation Link" + "<a href="+"some link"+"sometext to be clicked"+customer.Cust_Id +"></a>"+
"</h2>" +
"</body>" +
"</html>";
}
任何人都可以帮助我。上面的代码工作正常,但我无法生成 link 以及其他应该与 cust_Id 一起传递的内容以在单击时进行验证。 谢谢
试试这个
替换
"<a href="+"some link"+"sometext to be clicked"+customer.Cust_Id +"></a>"
至
"<a href=\""+your_url+"\">clickable text</a>"
这里string your_url="";