3
Answers

Creating a list and adding it to the htmlMessage body

Photo of Nicholas Mabe

Nicholas Mabe

3y
774
1

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)

1
Photo of Nicholas Mabe
NA 72 5.3k 3y
 
 //gets the first Title, Price and Quantity, loops through and appends the other items in the list.
 
foreach(var item in ShoppingCartVM.ListCart)
{
TempData["ProductList"] += "<span>" + item.Product.Title + "Price" + item.Price.ToString("c") + "Quantity:" + item.Count + "</span><br>";
};
 
 //Puts it all in a variable called ProductList
var ProductList = TempData["ProductList"];
// writes the email with all the products... 
List<ShoppingCart> shoppingCarts = _unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId ==
orderHeader.ApplicationUserId).ToList();
_emailSender.SendEmailAsync(orderHeader.ApplicationUser.Email, "New Order:" + orderHeader.Id + "- African Arts",
"New Order Created:&nbsp" + orderHeader.Id + ": &nbsp" + ProductList);
 
I need to improve the layout with additional HTML, but it works, Is this the best way to do this? 
 
 
1
Photo of Nicholas Mabe
NA 72 5.3k 3y
Hi Arvind, thank you for your suggestion unfortunately it doesn't really address my question. However, the lack of information I have been able to find on the subject seems to indicate that you can't code inside the htmlMessage element of Mimekit. which I also haven't been able to find an answer to.
 
Doing inside htmlMessage seems cleanest, I don't imagine doing it outside and adding the text to a variable will be to much of a issue, 
 
0
Photo of Arvind Yadav
NA 3.2k 527k 3y
msg = new MailMessage("xxxx@gmail.com",
"yyyy@gmail.com", "Message from PSSP System",
"This email sent by the PSSP system<br />" +
"<b>this is bold text!</b>");
msg.IsBodyHtml = true;
 
 
 
 
Refer this 
https://stackoverflow.com/questions/56786431/attach-the-contents-of-a-list-or-a-for-loop-in-the-body-of-an-email