调用控制器动作时发送电子邮件 [英] Send Email When Controller Action is called

查看:96
本文介绍了调用控制器动作时发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建事件后,我需要向成员发送电子邮件(与事件具有相同的爱好代码),这是我的控制器类



I need to send emails to members (who have same hobby code as events )once event is created this is my controller class



public void SetDefaults()
      {
          ViewBag.Sections = db.Sections.OrderBy(s => s.City).ToList();
          ViewBag.Hobbies = db.Hobbies.OrderBy(h => h.Hobby_Code).ToList();
      }
      public ActionResult Create()
      {
          SetDefaults();
          var events = new Event();
          events.Event_Status = "A";

          if (Request.IsAjaxRequest())
          {
              return PartialView("Create", events);
          }
          return View(events);
      }
      //
      //POST:/Events/Create
  [HttpPost]
      public ActionResult Create(Event events)
  {
      events.Event_Status = "A";

      if (ModelState.IsValid)
      {
          //Save Event
          db.Events.Add(events);
          SendEmail();
          db.SaveChanges();
          string ConfirmMessage = "Event Successfully Created";
          return RedirectToAction("Confirm", "Events", new { ConfirmMessage = ConfirmMessage });
      }
      if (Request.IsAjaxRequest())
      {

        SetDefaults();
          return PartialView("Create",events);
      }
      //Invalid -Redisplay with errors
      SetDefaults();
      return View(events);

      }




这是我的电子邮件课




and this is my email class

public void SendEmail()
        {


            var events = from a in db.Members.ToList()
                         join e in db.Events.ToList() on a.Hobby_Code equals e.Hobby_Code
                         select a;

            var member = db.Members.ToList();

            foreach (var item in member)
            {
                Email.SendEmail(item.Member_Email_Address, "Subject: Event Invite" , "Email Message: You have been invited " , false);
            }

推荐答案

为什么需要调用ToList?为什么您的对象不是可用形式?无论如何,这不是一个问题.您的实际电子邮件代码未显示.您也没有告诉我们为什么此代码无法满足您的要求.请删除它,或对其进行编辑以提出问题.这是一个提出问题的论坛,对吧?
Why do you need to call ToList ? Why is your object not in a usable form ? In any case, this is a non question. Your actual email code is not shown. Nor do you tell us why this code does not do what you want. Please delete this, or edit it to ask a question. This is a forum for questions, right ?


这篇关于调用控制器动作时发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆