使用EWS c#向组发送电子邮件
本文关键字:电子邮件 EWS 使用 | 更新日期: 2023-09-27 18:04:24
我正在使用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..
}
}