从表到邮件插入链接(ASP.NET MVC)

时间:2017-03-13 16:26:18

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我想从我的ASP.NET MVC网站发送电子邮件

我在我的控制器上写这个方法

public ActionResult SendEmail(SmartSolutions.Models.InvitationMail _objModelMail)
{
    if (ModelState.IsValid)
    {
        var emailList = db.InvitationMails.Select(model => model.To);
        var emails = String.Join(",", emailList);
        MailMessage mail = new MailMessage();
        mail.To.Add(emails);
        mail.From = new MailAddress(_objModelMail.From = "xxxxxxxx@gmail.com");
        mail.Subject = _objModelMail.Subject = "Видео интервью";

        string Body = _objModelMail.Body = "Предлагаем пройти интервью";
        mail.Body = Body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential
        ("xxxxxxxx@gmail.com", "*********************");// Enter seders User name and password
        smtp.EnableSsl = true;
        smtp.Send(mail);
        return View("SendEmail", _objModelMail as IEnumerable<InvitationMail>);
    }
     else
     {
         return View();
     }
}

但是当我尝试写这样的string Body = _objModelMail.Body = "Предлагаем пройти интервью" + __objModelMail.Link;

链接不粘贴到Body。

在表格中填写链接。

我的问题在哪里?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

这是代码

  public ActionResult SendEmail(SmartSolutions.Models.InvitationMail _objModelMail)
    {


         if (ModelState.IsValid)
        {
            var emailList = db.InvitationMails.Select(model => model.To);
            var emailList2 = db.InvitationMails.Select(model => model.Link);
            var link2 = emailList2.FirstOrDefault();
            var emails = String.Join(",", emailList);
            MailMessage mail = new MailMessage();
            mail.To.Add(emails);
            mail.From = new MailAddress(_objModelMail.From = "xxxxxxxx@gmail.com");
            mail.Subject = _objModelMail.Subject = "Видео интервью";

            string Body = _objModelMail.Body= "Предлагаем пройти интервью"+ link2;

            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("xxxxxxxx@gmail.com", "********");// Enter senders User name and password
            smtp.EnableSsl = true;
            smtp.Send(mail);
            return View("SendEmail", _objModelMail as IEnumerable<InvitationMail>);
        }
         else
         {
             return View();
         }
    }