Nicholas Mabe

Nicholas Mabe

  • NA
  • 72
  • 4.7k

Creating a list and adding it to the htmlMessage body

Mar 10 2022 3:08 PM

Hi, I'm stuck, any help is most appreciated.

I'm Using Mailkkit and Mimekit On OrderConfirmation. I  am trying to send an email confirming the order is received along with the order number and product list. I've got the mail working ok but I am struggling to list through the products inside the body of the HtmlMessage, currently, I only show one of the products.

All the Products in the order are present at  (Product.Title) inside ShoppingCart of   _unitOfWork.ShoppingCart.GetAll in the following line:

They are also inside OrderDetail

What I think I need to do is create a list of the products from OrderDetails based on the Id (which is the OrderID). Then with a foreach loop, loop through them in the HtmlMessage, well nothing I've tried works......

Here is the full IAction is as follows:

 

public IActionResult OrderConfirmation(int id)      

        {
            OrderDetail orderDetail = _unitOfWork.OrderDetail.GetFirstOrDefault(u => u.Id == id, IncludeProperties: "Product");
            OrderHeader orderHeader = _unitOfWork.OrderHeader.GetFirstOrDefault(u => u.Id == id, IncludeProperties: "ApplicationUser");

              List products = _unitOfWork.Product.GetAll(u => u.Id == orderDetail.ProductId).ToList(); 

              List shoppingCarts = _unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId ==
            orderHeader.ApplicationUserId).ToList();
            _emailSender.SendEmailAsync(orderHeader.ApplicationUser.Email, "New Order:" + orderHeader.Id + "- African Arts",
                "New Order Created: " + orderHeader.Id + ":  " + orderDetail.Product.Title);

//looking to loop throough  orderDetail.Product.Title

            _unitOfWork.ShoppingCart.RemoveRange(shoppingCarts);
            _unitOfWork.Save();
            return View(id);

 

        }

 

In the IAction above the SendEmailAsync is broken down as follows:

//email address

                           emailSender.SendEmailAsync(orderHeader.ApplicationUser.Email,

//string subject

                         "New Order:" + orderHeader.Id + "- African Arts", 

//string HtmlMessage

                         "New Order Created: " + orderHeader.Id + ":  " + orderDetail.Product.Title);

//I'm looking to loop through  orderDetail.Product.Title

 

SendEmailAsync is declared as follows:

 

public class EmailSender : IEmailSender
    {
        public Task SendEmailAsync(string email, string subject, string htmlMessage)
        {

 

i have email address correct, the subject correct and the htmlMessage correct, I just can't seem to work out how to loop inside of the htmlMessage, I get red lines under everything i try. 


Answers (3)