Use C# code to Delete all read outlook emails, meetings and appointments in EWS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lassi
    New Member
    • Jul 2014
    • 4

    Use C# code to Delete all read outlook emails, meetings and appointments in EWS

    hi,
    When I was trying to delete everything other than a normal email.
    I want to delete appointments, Meetings etc.
    Code:
    Appointment appointment = new Appointment(service);
    
    appointment.Subject = subject;
    
    appointment.Body = body;
    
    appointment.Delete(DeleteMode.MoveToDeletedItems);
    And i was also trying to select all read emails and delete them.
    Code:
     int readEmailCount = 0;
    
                        SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true));
    
                        ItemView view = new ItemView(999);
    
                        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
                        readEmailCount = findResults.Items.Count;
                        Console.WriteLine("Number of emails read : " + readEmailCount);
    
                        for (int i = 0; i < readEmailCount; ++i )
                        {
                            while(emailMessage.Subject != null)
                            {
                            if (i > 0)
                            {
                                Console.WriteLine("================================================");
                                Console.WriteLine("\n");
                                Console.WriteLine("{0}.Read mail from outlook inbox", ++i);
                                Console.WriteLine(string.Format("Subject:-     {0}", emailMessage.Subject));
                                // emailMessage.Delete(DeleteMode.MoveToDeletedItems);
                                Console.WriteLine("================================================");
                                Console.WriteLine("\n");
                            }
                        }
                        }
    Could you please help me to solve this

    System.InvalidO perationExcepti on: This operation can't be performed because this service object doesn't have an Id.
    at Microsoft.Excha nge.WebServices .Data.ServiceOb ject.ThrowIfThi sIsNew()
    at Microsoft.Excha nge.WebServices .Data.Item.Inte rnalDelete(Dele teMode deleteMode, Nullable`1 sendCancellatio nsMode, Nullable`1 affectedTaskOcc urrences, Boolean suppressReadRec eipts)
    at Microsoft.Excha nge.WebServices .Data.Item.Dele te(DeleteMode deleteMode, Boolean suppressReadRec eipts)
    at Microsoft.Excha nge.WebServices .Data.Item.Dele te(DeleteMode deleteMode)
    at AP.Utils.MailUt il.SendMail_HTM L(String from_address, String to_address, String cc_address, String subject, String body, String smtp_server, MailFileAttachm ent fileAttachment)



    Thanks in advance
Working...