+1 vote

I'm using .NET IMAP library, but I'm very paranoid about anyone on my dev team accidentally modifying our code and accidentally calling delete on a bunch of messages at once.

Is there some flag which I can set to prevent all modifications to email messages or IMAP folders?

I really want to only allow my email application to have read only access. Is there something in your code that allows me to do this? I just want an extra safety check for myself.

by

1 Answer

0 votes
 
Best answer

Use Imap.Examine instead of Imap.Select when you select/open IMAP folder.

This way folder is opened in read-only mode and IMAP server should prevent any modifications be made to the message.

Using Examine prevents removing of the \Recent and \Unseen flags, thus some IMAP servers require to use *Imap.Peek** methods instead of *Imap.Get**:

using(Imap imap = new Imap())
{
   imap.Connect("imap.server.com");  // or ConnectSSL for SSL
   imap.UseBestLogin("user", "password");

   imap.ExamineInbox();  // or Examine("FolderName")
   List<long> uids = imap.Search(Flag.Unseen);
   foreach (long uid in uids)
   {
       IMail email = new MailBuilder()
           .CreateFromEml(imap.PeekMessageByUID(uid));

       string email = email.Subject;
   }
   imap.Close();
}
by (297k points)
...