在MailMessage对象中添加Html链接

时间:2015-01-09 06:03:06

标签: asp.net-mvc-4 email

我想在Mail Message对象中提供一个链接。尝试谷歌搜索无法找到简单和更好的东西。 我有以下代码

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

任何人都可以帮助我。上面的代码工作正常,但是我无法生成链接,还有什么其他的东西应该通过cust_Id来传递,以便在点击时进行验证。 感谢

1 个答案:

答案 0 :(得分:1)

试试这个

替换

"<a href="+"some link"+"sometext to be clicked"+customer.Cust_Id +"></a>"

   "<a href=\""+your_url+"\">clickable text</a>"

string your_url="";