Reading mail from outlook not working as expacted

Joshi, Chiragkumar 1 Reputation point
2021-07-20T08:07:44.113+00:00

Hi team,
I am reading the mail using the below code. What is the issue is after this code I am storing some details into my database. But some time above code will read my mail from the IMAP but does not store anything and sometimes it will be stored in my database. I am also capturing errors if any but there is no error occurred. Can you please let me know why some time below code show mail to read in outlook but nothing coming to C# level?

Note: below code is running in task scheduler for every 1 min. And I am only starting the next run only once first will finish.

 MailRepository rep = new MailRepository(setting.HostName, setting.Port, true, setting.UserName, setting.Password);

 var unreadEmails =GetUnreadMails("inbox/iSourcingiContractApprovalLogic").ToList();


public IEnumerable<Message> GetUnreadMails(string mailBox)
        {
            return GetMails(mailBox, "UNSEEN").Cast<Message>();
        }


 private MessageCollection GetMails(string mailBox, string searchPhrase)
        {
            Mailbox mails = Client.SelectMailbox(mailBox);

            MessageCollection messages = mails.SearchParse(searchPhrase);
            return messages;
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,901 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-07-21T05:27:20.887+00:00

    Reading mail from the server and storing it in the database are two steps. Which step is the problem?

    You can use breakpoints to see if you get the expected result after you get the end of reading the mail.

    If there is a problem with the first step, you can try this code:

                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);  
                service.Credentials = new WebCredentials("test@outlook.com", "Password");  
                service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");  
      
                Mailbox mb = new Mailbox("test@outlook.com");  
                FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);  
              
                SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));  
                FindItemsResults<Item> findResults;  
                ItemView view = new ItemView(100);  
                do  
                {  
                    findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view);  
                    foreach (var item in findResults.Items)  
                    {  
                        Console.WriteLine(item.Subject);  
                        Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~");  
                    }  
                }  
                while (findResults.MoreAvailable);  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.