简体   繁体   中英

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.

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. Which means I have to go through the Order table and then to the Hospital table.

How would I go about doing this? I'm still pretty new to MVC, so I will appreciate all the help I can get.

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. Hence why I call it after the savechanges() .

These lines Order order = db.Orders.Where(o => o.OrderID == order.DeliveryID).FirstOrDefault(); and 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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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