简体   繁体   English

使用 Mailkit 或 mimekit 向多个收件人发送一封电子邮件

[英]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.请不要将其标记为重复问题,因为该解决方案适用于邮件消息,而不适用于 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");
            }
        }

You can use AddRange method like this.您可以像这样使用 AddRange 方法。

        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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM