如何在 ASP.NET MVC 中使用 Postal 向数据库中的多个电子邮件地址发送电子邮件

How to send an email to multiple email addresses in the database using Postal in ASP.NET MVC

我正在尝试找到一种解决方案,以解决如何向 多个 医院电子邮件地址发送电子邮件,具体取决于哪些订单链接到正在进行更改的交付- 使用 Postal

因此,为了了解一些情况,我希望向所有订单与分娩相关联的医院发送相同的电子邮件。因此,在编辑交付时(例如更改 status),必须向 ALL 在该交付中有订单的医院发送电子邮件。

问题是一个 Delivery 可以有很多订单,不一定只有一个,因此,我不确定如何定位在该特定 Delivery 中有订单的所有医院的所有电子邮件地址。

此外,医院仅链接到订单,订单链接到交付 - 访问医院电子邮件的唯一方法是从 Hospital table。这意味着我必须通过 Order table 然后到 Hospital table。

我该怎么做?我对 MVC 还是很陌生,所以我会很感激我能得到的所有帮助。

我在 Delivery Controller 中 POSTEdit Method 中执行此操作 因为我只想在编辑完成后从那里发送电子邮件。因此,为什么我在 savechanges() 之后调用它。

这些行Order order = db.Orders.Where(o => o.OrderID == order.DeliveryID).FirstOrDefault();Hospital hospital = db.Hospitals.(h => h.HospitalID == order.HospitalID); 只是我的尝试。它们不起作用,并且不是原始代码的一部分。

如果需要任何额外的代码,请告诉我,我会把它添加到问题中!

型号

送货

public class Delivery
{

    public int DeliveryID { get; set; }

    public int DriverID { get; set; }

    public virtual Driver Driver { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}

医院

public class Hospital
{
    public int HospitalID { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }

    public virtual ICollection<Order> Orders { get; set; }
}

订单

   public class Order
    {
        public int OrderID { get; set; }

        public int HospitalID { get; set; }

        public int? DeliveryID { get; set; }}

        public virtual Hospital Hospital { get; set; }

        public virtual Delivery Delivery { get; set; }
    }

DeliveryVM viewModel

public class DeliveryVM
{
    public int? ID { get; set; } 

    public int DriverID { get; set; }

    public SelectList DriverList { get; set; }

    public List<OrderVM> Orders { get; set; }
}

Delivery Controller POST 方法:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(DeliveryVM model)
    {

        // Get the data model based on the ID of the view model
        Delivery delivery = db.Deliverys.Find(model.ID);
        // Map the view model properties to the data model
        delivery.DriverID = model.DriverID;
         ....

        db.SaveChanges();

            //Email
            Order order = db.Orders.Where(o => o.OrderID == order.DeliveryID).FirstOrDefault(); // I tried this but it didn't work. It was just an attempt
            Hospital hospital = db.Hospitals.(h => h.HospitalID == order.HospitalID); // I was going to use this to retrieve the hospitalIDs linked to the Order? Don't know if thats correct
            dynamic email = new Email("Example");
            email.ID = delivery.DeliveryID;
            email.To = hospital.Email; // this is where you set the email you are sending to. 
            email.Send();
            //End
        return RedirectToAction("Index");
    }

注意

我认为你应该可以做类似

的事情
//Email
var emailQuery = from o in db.Orders
    from h in o.Hospital
    where o.DeliveryID = model.ID 
//i'm not sure where this order comes from but if you 
//don't really have that then you may need to use your DeliverVM instead
    select new { Email = h.Email };

var emails = emailQuery.ToList();

dynamic email = new Email("Example");
email.ID = order.DeliveryID;
email.To = emails.Join(";"); // you can have more that one email because you have multiple orders
email.Send();