2

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") これは何とか機能しません。誰でもこれについて私を助けることができますか?

4

5 に答える 5

4

各ユーザーをセミコロンで区切るだけです。たとえば、以下の私のコードを見てください。

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();
于 2016-05-18T17:33:17.317 に答える
3

("xxx@example.com,yyy@example.org,zzz@meta.example.com") は、("xxx@example.com; yyy@example.org; zzz@ meta.example.com") ??

Outlook で複数の人に送信すると、to: フィールドにセミコロンが表示されます。

于 2013-03-13T18:11:53.793 に答える
1

受信者ごとに「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;

私が思ったように、任意の数の受信者を持つ 1 つの送信済みアイテムを作成します。

于 2013-03-13T18:40:23.563 に答える
1

このコードは、複数の受信者を送信するために正常に機能しています

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

    }
于 2014-06-09T07:06:29.690 に答える
0

複数の人に送信したい場合は、このコードを試してください。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);
        }
    }
于 2013-03-13T18:16:36.740 に答える