Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a single email to multiple recipients using Mailkit or mimekit

Please do not mark it as a duplicate question because the solution exists for mail message, not for mailkit.

I am trying to send an email to multiple addresses. I tried using the code below but I have not tried using a loop.

        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("CUBES", from));
        message.To.Add(new MailboxAddress("Not Reply", fcemail));
        message.Cc.Add(new MailboxAddress("Maria",CC));
        message.Subject = "Approval Required for Business Case";

       using (var client = new SmtpClient())
        {
            try
            {
                client.Connect(host, port);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.Authenticate(user, password);
                client.Send(message);
                client.Disconnect(true);
            }
            catch (Exception ex)
            {
                ViewBag.error = ex.Message.ToString();
                ViewData["Message"] = "msgx";
                return View("NotFoundErrors");
            }
        }
like image 493
Reply not Avatar asked Nov 24 '18 08:11

Reply not


1 Answers

You can use AddRange method like this.

InternetAddressList list = new InternetAddressList();
list.Add(new MailboxAddress(emailaddress));
list.Add(new MailboxAddress(emailaddress));
list.Add(new MailboxAddress(emailaddress));
var message = new MimeMessage();
message.From.Add(new MailboxAddress("CUBES", from));
message.To.AddRange(list);
like image 187
Faizan Avatar answered Sep 22 '22 11:09

Faizan