C#Outlook向多个收件人发送一封电子邮件

C#Outlook向多个收件人发送一封电子邮件,c#,outlook,C#,Outlook,我一直在想如何通过C#向多个收件人发送outlook电子邮件。现在我可以在收件人之间做一个循环,但是在我的发送框中会有很多已发送的电子邮件 Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.MailItem oMsg = (Mi

我一直在想如何通过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)或CIPS.Add(“xxx@xxx.com,yyy@yyy.com,zzz@zzz.com") 无论如何,这是行不通的。有人能帮我吗?

(“xxx@example.com,yyy@example.org,zzz@meta.example.com)分隔符不是如中所示的分号吗 ("xxx@example.com; yyy@example.org; zzz@meta.example.com”)


如果我进入Outlook并发送给多人,则会在“收件人:”字段中显示一个分号。

如果要发送多人,请尝试使用此代码,然后将他们添加到抄送或密件抄送中,或根据需要添加到收件人中

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);
        }
    }
public void SendAutomatedEmail(字符串htmlString、字符串主题、字符串from、字符串fromPwd、字符串收件人)
{
尝试
{
字符串mailServer=“xxxxExchangesver.com”;
string[]allToAddresss=recipient.Split(“;,”.toCharray(),StringSplitOptions.RemoveEmptyEntries);
MailMessage=newmailmessage(from,alltoaddress[0]);
int mailcount1=1;
for(;mailcount1
为什么不给每个收件人打电话“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;

将创建一个包含任意数量收件人的已发送项目,正如我所想。

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

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

    }
private静态void SendMail(字符串体){
尝试{string tomail=System.Configuration.ConfigurationManager.AppSettings[“ToMailString”];
string[]AllToAddresss=tomail.Split(“;,”.tocharray(),StringSplitOptions.RemoveEmptyEntries);
Microsoft.Office.Interop.Outlook.Application oApp=(Microsoft.Office.Interop.Outlook.Application)Marshal.GetActiveObject(“Outlook.Application”);
//Microsoft.Office.Interop.Outlook.Application oApp=新的Microsoft.Office.Interop.Outlook.Application();
//Microsoft.Office.Interop.Outlook.MailItem tempItem=(Microsoft.Office.Interop.Outlook.MailItem)Globals.ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlimType.olMailItem);
Microsoft.Office.Interop.Outlook.MailItem电子邮件=(Microsoft.Office.Interop.Outlook.MailItem)(oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlimType.olMailItem));
email.Subject=“状态报告”;
email.Body=Body;
Microsoft.Office.Interop.Outlook.Recipients oRecips=(Microsoft.Office.Interop.Outlook.Recipients)email.Recipients;
int mailcount1=1;
for(;mailcount1
您只需用分号分隔每个用户。例如,看看下面我的代码

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

@云…你做什么“无论如何都不会工作”。您遇到的错误是什么?如果可能,请让您的电子邮件管理员创建一个您通过电子邮件发送给的组。这样,你就不必在企业添加或删除收件人时重新编译。Outlook无法识别一个或多个姓名。如果我向50人发送电子邮件,我无法识别;我不想在发送框中看到50封电子邮件。它不应该看到,除非您还创建了50个邮件对象,而不是在同一封邮件上只调用add 50次。这就是它在旧版本Outlook上的工作原理,一旦我在一台配备了设备的计算机上,我会尝试用2010进行验证。虽然我也会投票支持一个不涉及任何office组件的解决方案,但最初的问题是如何用Outlook进行验证。此解决方案使用SMTP客户端,该客户端不知道Outlook可能已设置的任何邮件配置文件以及可能对OP很重要的其他内容。公平地说,值得一试。