繁体   English   中英

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

[英]Email to group using EWS C#

我正在使用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创建通讯组列表的另一种方法?

谢谢

问题解决了。
我只需要添加组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..
            }
        }

暂无
暂无

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

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