【问题标题】:C# Outlook send one email to multiple recipientsC# Outlook 向多个收件人发送一封电子邮件
【发布时间】:2018-10-04 00:59:51
【问题描述】:

我一直在试图弄清楚如何通过 C# 将 Outlook 电子邮件发送给多个收件人。现在我可以在收件人之间做一个循环,但是我的发件箱里会有很多已发送的电子邮件。

            Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

            oMsg.HTMLBody ="test";
            oMsg.Subject = "test" ;
            Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;        
            Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add("xxx@xxx.com");
            oRecip.Resolve();
            oMsg.Send();
            oRecip = null;
            oRecips = null;
            oMsg = null;
            oApp = null;

如果我向其中添加多个地址,例如: (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add("xxx@xxx.com,yyy@yyy.com,zzz@zzz.com") 这不会以某种方式工作。谁能帮我解决这个问题?

【问题讨论】:

  • @Yun...你怎么办'不会以某种方式工作'。您遇到什么错误?
  • 如果可能,让您的电子邮件管理员创建一个您通过电子邮件发送到的组。这样您就不必在企业添加或删除收件人时重新编译。
  • Outlook 无法识别一个或多个名称。

标签: c# outlook


【解决方案1】:

您只需用分号分隔每个用户。例如看看我下面的代码。

Outlook.MailItem mail = null;
 Outlook.Application objApp = new Outlook.Application();               
 mail=(Outlook.MailItem)objApp.CreateItem(Outlook.OlItemType.olMailItem);

  mail.Subject ="HI";
  mail.To = "personone@yahoo.com; Persontwo@yahoo.com";
  mail.Attachments.Add("C:\SOME_FOLDER\SomeFile");
  mail.Body="xxxxxx";
  mail.Send();

【讨论】:

    【解决方案2】:

    ("xxx@example.com,yyy@example.org,zzz@meta.example.com") 不是分号,如 ("xxx@example.com; yyy@example.org; zzz@meta.example.com") ??

    如果我进入我的 Outlook 并发送给多个人,它会在 to: 字段中显示一个分号。

    【讨论】:

    • 还不错,值得一试。
    【解决方案3】:

    为什么不为每个收件人调用“oRecips.Add”?毕竟它是添加到收件人...?

    编辑:刚刚验证:

     Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
     Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
    
     oMsg.HTMLBody = "test";
     oMsg.Subject = "test";
     Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
    
     foreach (var recipient in new string[] { "a@b.c", "c@d.e" })
     {
          Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(recipient);
          oRecip.Resolve();
     }
     oRecips = null;
    
     oMsg.Send();
     oMsg = null;
     oApp = null;
    

    将创建一个包含任意数量收件人的已发送项目,就像我想的那样。

    【讨论】:

    • 如果我向 50 个人发送电子邮件,我不想在已发送的框中看到 50 封电子邮件。
    • 不应该 - 除非您还创建 50 个消息对象,而不是仅仅在同一消息上调用 add 50 次。这就是它在旧版 Outlook 中的工作方式,一旦我在一台配备齐全的计算机上,我将尝试使用 2010 进行验证。
    【解决方案4】:

    此代码适用于发送多个收件人

                private static void SendMail(string body){
                try   { string tomail = System.Configuration.ConfigurationManager.AppSettings["ToMailString"];
                string[] allToAddresses = tomail.Split(";,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    
                Microsoft.Office.Interop.Outlook.Application oApp = (Microsoft.Office.Interop.Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
                //Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
                //Microsoft.Office.Interop.Outlook.MailItem tempItem = (Microsoft.Office.Interop.Outlook.MailItem)Globals.ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
    
                Microsoft.Office.Interop.Outlook.MailItem email = (Microsoft.Office.Interop.Outlook.MailItem)(oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem));
                email.Subject = "Status Report";
                email.Body = body;
                Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)email.Recipients;
                int mailcount1 = 1;
                for (; mailcount1 < allToAddresses.Length; mailcount1++)
                {
                    if (allToAddresses[mailcount1].Trim() != "")
                    {
                        //email.Recipients.Add(allToAddresses[mailcount1]);
                        Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(allToAddresses[mailcount1]);
                        oRecip.Resolve();
                    }
                }
    
                oRecips = null;
                ((Microsoft.Office.Interop.Outlook.MailItem)email).Send(); 
                Console.WriteLine("Mail Sent");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
    
        }
    

    【讨论】:

      【解决方案5】:

      如果您想发送多个人,请尝试使用此代码,将他们添加到 cc 或 bcc 或按您的意愿添加

      public void SendAutomatedEmail(string htmlString, string subject, string from, string fromPwd, string recipient)
          {
              try
              {
                  string mailServer = "xxxxExchangesver.com";
                  string[] allToAddresses = recipient.Split(";,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
      
                  MailMessage message = new MailMessage(from, allToAddresses[0]);
                  int mailcount1 = 1;
                  for (; mailcount1 < allToAddresses.Length; mailcount1++)
                  {
                      if (allToAddresses[mailcount1].Trim() != "")
                          message.To.Add(allToAddresses[mailcount1]);
                  }
                  message.IsBodyHtml = true;
                  message.Body = htmlString;
                  message.Subject = subject;
                  message.CC.Add(from);
      
                  SmtpClient client = new SmtpClient(mailServer);
      
                  var AuthenticationDetails = new NetworkCredential(from, fromPwd);
                  client.Credentials = AuthenticationDetails;
                  client.EnableSsl = true;
                  client.Send(message);
                  client.Dispose();
      
              }
              catch (Exception e)
              {
                  status(e.Message, Color.Red);
              }
          }
      

      【讨论】:

      • 虽然我也会投票支持不涉及任何办公组件的解决方案,但最初的问题是如何使用 Outlook 来实现。此解决方案使用 SMTP 客户端,该客户端不知道 Outlook 可能已设置的任何邮件配置文件以及对 OP 可能很重要的其他内容...
      猜你喜欢
      • 2016-03-07
      • 1970-01-01
      • 2019-04-26
      • 2012-05-18
      • 2017-12-05
      • 2015-08-29
      • 1970-01-01
      相关资源
      最近更新 更多