How to pass email list to MailboxAddress from database Entity Framework in ASP.NET Core MVC

Viewed 30

I want to send bulk Emails by using MailKit.Net.Smtp, using MimeKit.

I get email lists and email body data like (feeamount and feetype), these two fields send to the same row email address.

Code to get list and data:

public IActionResult FeeNotification(int? id)
{
    var ids = _context.FeeEntry
                      .Where(i => i.FeeInfoID.Equals(id))
                      .Select(x => x.StudentID).ToList();

    var q = (from t in _context.FeeEntry
                               .Include(f => f.AddFeeInfo)
                               .Include(f => f.students)
             join std in _context.Students
                                 .Select(p => new StudentRegModel() { StudentID = p.StudentID }) on t.StudentID equals std.StudentID
             join fi in _context.FeeInfo.Select(p => new AddFeeInfo() { FeeInfoID = p.FeeInfoID }) on t.FeeInfoID equals fi.FeeInfoID
             where ids.Contains(std.StudentID)
             select t).ToList();

    return View(q);
}

I want to pass list of emailaddress to the

message.To.Add(new MailboxAddress(list of emailes)

output view is browser this is just view if you have other way to complete this task please suggest me

Send email code is this code send only one email, I want to send full list email that show above in in my code.

I know that foreach loop will work here but I do not know the proper way to use it.

using (var client = new SmtpClient())
{
    var myvalues = (from values in _context.FeeInfo
                    where values.FeeInfoID == feeEntry.FeeInfoID
                    select values.FeeAmount).FirstOrDefault();

    client.Connect("smtp.gmail.com", 465);
    client.Authenticate("emailaddress", "password");

    var bodyBuilder = new BodyBuilder
    {
        HtmlBody = $"<table style = 'border:2px solid blue' border='1'> <thead>  </thead>  <tr>" +
                   $"<th> </th><th>Name</th> <th>Submit Date </th>  <th>Fee Amount</th>  </tr> " +
                   $"<tbody> <tr> <th>1</th> <td>{std.Student.StdName}</td> <td> {feeEntry.FeeDeppositDate}</td> <td>{ViewData["FeeInfoID"]} </td><tr> </tbody> </table>  ",
        TextBody = "{formData.ID}\r\n{ formData.subject}\r\n{formData.body}\r\n{formData.Email}"
    };

    var message = new MimeMessage
    {
        Body = bodyBuilder.ToMessageBody()
    };
    message.From.Add(new MailboxAddress("No Reply OCMWP", "bs180201421@vu.edu.pk"));
    message.To.Add(new MailboxAddress(std.Student.StdName, std.Student.StdEmailAddress));
    message.Subject = "Fee Submitted Information (OCMWP)";

    client.Send(message);
    client.Disconnect(true);
}
2 Answers

Internally, To is a list, so if you have a list of students from your database you can do something like this to add them all to the same email:

foreach (Student student in students)
{
    message.To.Add(new MailboxAddress(student.Name, student.EmailAddress));
}

It works as below.

Sending bulk mail to database users with ASP.NET Core MVC and Entity Framework:

var q = (from t in _context.FeeEntry.Include(f => f.AddFeeInfo).Include(f => f.students)
         join std in _context.Students.Select(p=> new StudentRegModel() { StudentID = p.StudentID }) on t.StudentID equals std.StudentID
         join fi in _context.FeeInfo.Select(p => new AddFeeInfo() { FeeInfoID = p.FeeInfoID }) on t.FeeInfoID equals fi.FeeInfoID
         where ids.Contains(std.StudentID)
         select t).ToList();

using (var client = new SmtpClient())
{
    foreach (FeeEntry student in q)
    {
        client.Connect("smtp.gmail.com", 465);
        client.Authenticate("your email address", "password");

        var bodyBuilder = new BodyBuilder
        {
            HtmlBody = $"<table style = 'border:2px solid blue' border='1'> <thead>  </thead>  <tr>" +
                       $"<th> </th><th>Name</th> <th>Submit Date </th>  <th>Fee Amount</th>  </tr> " +
                       $"<tbody> <tr> <th>1</th> <td>{student.students.StdName}</td> <td> {student.AddFeeInfo.FeeAmount}</td> <td>{ViewData["FeeInfoID"]} </td><tr> </tbody> </table>  ",
            TextBody = "{formData.ID}\r\n{ formData.subject}\r\n{formData.body}\r\n{formData.Email}"
        };

        var message = new MimeMessage
        {
            Body = bodyBuilder.ToMessageBody()
        };

        message.From.Add(new MailboxAddress("No Reply OCMWP", "mail address"));
        message.To.Add(new MailboxAddress(student.students.StdName, student.students.StdEmailAddress));
        message.Subject = "Fee Submited Information ( OCMWP)";

        client.Send(message);
        client.Disconnect(true);
    }
}
Related