使用EWS C#向群组发送电子邮件

时间:2016-08-29 08:35:15

标签: c# exchangewebservices

我正在使用ExchangeWebServices C# 我试图发送电子邮件到分发列表,所以 我创建了一个小组如下:

private void CreateGroup(ExchangeService service)
{
    // Create a new contact group object.
    ContactGroup myContactGroup = new ContactGroup(service);

    // Give the group a name.
    myContactGroup.DisplayName = "TestContactGroup";

    // Add some members to the group.
    myContactGroup.Members.Add(new GroupMember("Euser@mydomain.com"));
    myContactGroup.Members.Add(new GroupMember("Euser1@mydomain.com"));
    myContactGroup.Members.Add(new GroupMember("Euser2@mydomain.com"));

    // Save the group.
    myContactGroup.Save();

}

现在我试图向这个群组发送电子邮件,我该怎么做? 我尝试了什么:

EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("TestContactGroup");//Throw an exception "At least one recipient isn't valid."
//email.ToRecipients.Add("TestContactGroup@mydomain.com");//"Return" the mail that "The email address you entered couldn't be found."

email.Subject = "MySubject";
email.Body = new MessageBody("MyBody");

// Send the mail
email.Send();

如果我试图发送给TestContactGroup我有例外:

  

"至少有一位收件人无效。"

如果我尝试发送到TestContactGroup@mydomain.com,我收到一封邮件,说明邮件未找到。

那么,我如何向我列出的群组列表发送电子邮件?或者用EWS创建分发列表的另一种方法是什么?

由于

1 个答案:

答案 0 :(得分:0)

问题解决了。
我只需要添加组ID如下:

myContactGroup.Id = grpId;

我的群组ID如下:

// Instantiate the item view with the number of items to retrieve from the Contacts folder.
        ItemView view = new ItemView(9999);

         // Request the items in the Contacts folder that have the properties that you selected.
        FindItemsResults<Item> contactItems = service.FindItems(WellKnownFolderName.Contacts, view);
        string groupId = string.Empty;
        List<ItemId> groupItemIds = new List<ItemId>();

        // Loop through all contacts 
        foreach (Item item in contactItems)
        {
            //Check to see if ContactGroup
            if (item is ContactGroup)
            {
                //Get the contact group
                ContactGroup contactGroup = item as ContactGroup;
                groupItemIds.Add(item.Id);//Using to send an email by item id, can classify by DisplayName etc..
            }
        }