使用带有C#的Exchange服务器删除收件箱邮件项目

时间:2014-11-11 15:27:30

标签: c#-4.0

     public static ReadMail()
     {
        ExchangeService Service = new ExchangeService();
        Service.Credentials = new WebCredentials("", "", "");
        Service.AutodiscoverUrl("xyz@xyz.com");
        Folder inbox = Folder.Bind(Service, WellKnownFolderName.Inbox);
        StreamingSubscription streamingsubscription = Service.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);
        var connection = new StreamingSubscriptionConnection(Service, 30);
        connection.AddSubscription(streamingsubscription);
        connection.OnNotificationEvent += OnNotificationEvent;
        connection.Open();
    }

    private static void OnNotificationEvent(object sender, NotificationEventArgs args)
    {
        Item mail = args.Subscription.Service.FindItems(WellKnownFolderName.Inbox, new ItemView(10)).Items[0];
    }

我使用Exchange服务器(2007)连接到邮件帐户。我能够阅读邮件项目。在我阅读&解析我需要从收件箱中删除邮件项目的邮件项目。请帮我。提前致谢

1 个答案:

答案 0 :(得分:4)

我是使用以下代码完成的:(这将删除前10封邮件)

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        service.Credentials = new WebCredentials("xxx@yyy.com", "******");
        service.AutodiscoverUrl("xxx@yyy.com");
        FindItemsResults<Item> items = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
        if (items.Count() != 0)
        {
            IEnumerable<ItemId> itemIds = from p in items.Items select p.Id;
            service.DeleteItems(itemIds, DeleteMode.MoveToDeletedItems, null, null);
        }