简体   繁体   English

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

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

I'm trying to find a solution on how to send an email to multiple Hospital email addresses depending on which orders are linked to the Delivery that the change is being made on - using Postal . 我正在尝试找到一种解决方案,该解决方案取决于如何将邮件发送到多个医院的电子邮件地址,具体取决于哪些订单已链接到正在使用邮政进行更改的交货。

So for a bit of context, I wish to send the same email to all the Hospitals that have orders linked to a delivery. 因此,在某种程度上,我希望将相同的电子邮件发送给所有已将订单链接到交货的医院。 So upon editing a delivery, (to change the status for example), an email must be sent to ALL the hospitals that have Orders in that Delivery. 因此,在编辑交货时(例如,要更改status ),必须将电子邮件发送给该交货中具有订单的所有医院。

The issue is that a Delivery can have many orders and not necessarily just one, and because of that, I'm not sure how to target all the email addresses of all the hospitals that have Orders in that specific Delivery. 问题是交付可能有很多订单,而不一定只有一个订单,因此,我不确定如何定位该特定交付中所有拥有订单的医院的所有电子邮件地址。

Also, a Hospital is only linked to an Order and an Order is linked to a Delivery - and the only way to access the hospital's email is from the Hospital table. 另外,医院仅链接到订单,订单仅链接到交货-访问医院电子邮件的唯一方法是从Hospital表。 Which means I have to go through the Order table and then to the Hospital table. 这意味着我必须先浏览“ Order表,然后再到达“ Hospital表。

How would I go about doing this? 我将如何去做呢? I'm still pretty new to MVC, so I will appreciate all the help I can get. 我对MVC还是很陌生,因此,我将不胜感激。

I'm doing this in the POST of my Edit Method in the Delivery Controller because I only wish to send an email from there after an Edit has been made. 我在交付控制器中的“ 编辑方法”POST中执行此操作,因为我仅希望在进行编辑后从那里发送电子邮件。 Hence why I call it after the savechanges() . 因此,为什么在savechanges()之后调用它。

These lines Order order = db.Orders.Where(o => o.OrderID == order.DeliveryID).FirstOrDefault(); 这些行Order order = db.Orders.Where(o => o.OrderID == order.DeliveryID).FirstOrDefault(); and Hospital hospital = db.Hospitals.(h => h.HospitalID == order.HospitalID); Hospital hospital = db.Hospitals.(h => h.HospitalID == order.HospitalID); are just my attempts. 只是我的尝试。 They don't work, and are not part of the original code. 它们不起作用,也不属于原始代码。

If there is any additional code required, please let me know and I'll add it to the question! 如果需要任何其他代码,请告诉我,我将其添加到问题中!

Models 楷模

Delivery 交货

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

Hospital 医院

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

Order 订购

   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 DeliveryVM视图模型

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 method: 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");
    }

Note 注意

  • I removed a lot of code from the Delivery Edit Method for readability, so if necessary I can add that to the question as well. 为了便于阅读,我从“交付编辑方法”中删除了很多代码,因此如有必要,我也可以将其添加到问题中。

I think you should be able to do something like 我认为您应该可以做类似的事情

//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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM