question

chiragjoshi-0663 avatar image
0 Votes"
chiragjoshi-0663 asked TimonYang-MSFT commented

Reading mail from outlook not working as expacted

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;
         }







dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@chiragjoshi-0663
Does this code help your problem? If there are other questions, please feel free to let me know.

0 Votes 0 ·