1

I am working on an asp.net core 5.0 MVC application, I have used Mailkit Nuget package to send an email to the client after creating a record with all its answers. I have put the code related to the Mailkit inside the Create(Post) method but after creating and sending email I am getting System.NullReferenceException in my Mailkit code part for one of my fields called Product Type when I write it like sale.ProductType.ProductTypeDesc which is basically a drop-down and has a look-up table and so a foreign key to the main table(Sale). but it is working with sale.ProductTypeId which is not something that I want. Can anyone give me solution?

Here are my models:

using System;

using System.Collections.Generic;

#nullable disable

namespace Sale_app.Models

{

public partial class Sale

{
    public int SaleId { get; set; }

    public string ProductName { get; set; }

    public string ProductTypeId { get; set; }
    

    public virtual ProductType ProductType { get; set; }
 
}

}

using System;

using System.Collections.Generic;

#nullable disable

namespace Sale_app.Models

{ public partial class ProductType

{
    public ProductType()

    {

        Sales = new HashSet<Sale>();

    }

    public int ProductTypeId { get; set; }

    public string ProductTypeDesc { get; set; }

    public virtual ICollection<Sale> Sales { get; set; }

}

}

and these are Create methods in my Controller:

//Get Sale

public IActionResult Create()

    {

        ViewBag.ProductTypeList = _context.ProductType.ToList();

        return View();
    }

[HttpPost]

    [ValidateAntiForgeryToken]

    public IActionResult Create([Bind("SaleId,ProductTypeId,ProductName")] Sale sale)

    {
        
        if (ModelState.IsValid)
        {
            try
            {
                _context.Add(sale);
                _context.SaveChanges();

                //instantiate a new MimeMessage
                var message = new MimeMessage();
                //Setting the To e-mail address
                message.To.Add(new MailboxAddress("E-mail Recipient Name", "an email address comes here"))
                //Setting the From e-mail address
                message.From.Add(new MailboxAddress("Sale Form " + "Response", "an email address comes here"));
                //E-mail subject 
                message.Subject = "Sale Form";
                //E-mail message body
                message.Body = new TextPart(TextFormat.Html)
                {
                    Text = "<b>SaleId:</b> " + sale.SaleId + "<br/>" + "<b>ProductName:</b> " + sale.ProductName + "<br/>" +
                    "<b>ProductType</b>: " + sale.ProductType.ProductTypeDecs

                };

                //Configure the e-mail
                using (var emailClient = new SmtpClient())
                {
                    emailClient.Connect("smtp.gmail.com", 587, false);
                    emailClient.Authenticate("an email address comes here", "a password comes here");
                    emailClient.Send(message);
                    emailClient.Disconnect(true);
                }
                return RedirectToAction(nameof(Index));

            }

            catch (Exception ex)
            {
                ModelState.Clear();
                ViewBag.Message = $" Oops! We have a problem here {ex.Message}";
            }
        }


        return View(sale);
       
    }

1 Answer 1

0

Change your code :


_context.Add(sale);
_context.SaveChanges();

var newSale= _context.Sales
.Include(i=> i.ProductType)
.Where(i=> i.SaleId=sale.SaleId)
.FirstOrDefault();

......
......
 message.Body = new TextPart(TextFormat.Html)
  {
   Text = "<b>SaleId:</b> " + newSale.SaleId + "<br/>" + "<b>ProductName:</b> " + newSale.ProductName + "<br/>" +
"<b>ProductType</b>: " + newSale.ProductType.ProductTypeDecs
 };

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for this solution. It worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.