繁体   English   中英

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

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

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

因此,在某种程度上,我希望将相同的电子邮件发送给所有已将订单链接到交货的医院。 因此,在编辑交货时(例如,要更改status ),必须将电子邮件发送给该交货中具有订单的所有医院。

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

另外,医院仅链接到订单,订单仅链接到交货-访问医院电子邮件的唯一方法是从Hospital表。 这意味着我必须先浏览“ Order表,然后再到达“ Hospital表。

我将如何去做呢? 我对MVC还是很陌生,因此,我将不胜感激。

我在交付控制器中的“ 编辑方法”POST中执行此操作,因为我仅希望在进行编辑后从那里发送电子邮件。 因此,为什么在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视图模型

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

暂无
暂无

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

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