We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Check and Delete Emails which are not related to a User

We are facing an issue with our Exchange server database and it cause all emails to be distributed to all mailboxes. So my question is, Can I use Aspose.Email to connect with Exchange and check all emails and delete all emails that are not related to a user (where he is not on TO, CC or BCC).


This Topic is created by MuzammilKhan using the Email to Topic plugin.

@osmansays

In order to connect to exchange server and getting specific user related emails, please find the code sample given below:

// Connect to Exchange Server using ImapClient class
ImapClient client = new ImapClient("Host", "UserName", "Password");
client.SupportedEncryption = EncryptionProtocols.Tls12;
            
//Filter emails
ImapQueryBuilder builder = new ImapQueryBuilder();
builder.To.Contains("User Email");
builder.Cc.Contains("User Email");
builder.Bcc.Contains("User Email");
MailQuery query = builder.GetQuery();
IAsyncResult asyncResult = client.BeginListMessages(query);

// Get the list of messages
ImapMessageInfoCollection messages = client.EndListMessages(asyncResult);
foreach (ImapMessageInfo msgInfo in messages)
{
    Console.WriteLine(msgInfo.Subject);
    
    // Mark the message as read
    client.ChangeMessageFlags(1, ImapMessageFlags.IsRead);

    //Delete Message
    client.DeleteMessage(msgInfo.MessageId);
}
// Disconnect from the server
client.Dispose();

Thanks Muzammil for your valuable support.

Can I use a service/admin account to delete other mailboxes emails?or it should be used same mailbox credentials?

@osmansays

The API shall get all the mail messages accessible to that user’s account and there are two ways to connect exchange server. Either using EWS client or using IMAP client. Please follow the link below for more details:
https://docs.aspose.com/display/emailnet/Connecting+to+Exchange+Server