Search code examples
asp.net-mvcemailgoogle-apigmailimap

Read emails from Inbox gmail


I'm traying to read mails from gmail using ImapClient :

using AE.Net.Mail;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Plus.v1;
using Google.Apis.Plus.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Web;

namespace Web.FrontOffice.Utils
{
    public class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/gmail-dotnet-quickstart.json


        public static void ReadMailFromGmail()
        {

            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = "My_ClientId", ClientSecret = "My_ClientSecret" },
                new[] { "https://mail.google.com/","https://www.googleapis.com/auth/userinfo.email"}, "user", CancellationToken.None, new FileDataStore("Analytics.Auth.Store")).Result;


            // Create Gmail API service.
            PlusService service = new PlusService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "My App"});
            // Define parameters of request.          
            Person me = service.People.Get("me").Execute();
            Person.EmailsData myAccountEmail = me.Emails.Where(a => a.Type == "account").FirstOrDefault();
            // List labels.
            ImapClient ic = new ImapClient("imap.gmail.com", myAccountEmail.Value, credential.Token.AccessToken, AE.Net.Mail.AuthMethods.SaslOAuth, 993, true);       
            ic.SelectMailbox("INBOX");

            // MailMessage represents, well, a message in your mailbox           
            var uids = ic.Search(SearchCondition.SentSince(new DateTime(2017, 4, 13)));
            foreach (var uid in uids)
            {
                MailMessage message = ic.GetMessage(uid);

                Debug.WriteLine(message.Body+"   "+message.Subject+"   "+message.Date);
            }          
        }
    }
}

An exception of type 'System.Exception' occurred in AE.Net.Mail.dll but was not handled in user code

Additional information: xm003 BAD Could not parse command


Solution

  • This problem is currently an open bug with AE.Net.Mail.

    Please see the following URL for information:

    https://github.com/andyedinborough/aenetmail/issues/197

    It looks like, from the bug information and comments that it's to do with the DateTime in the search condition.

    Replacing your current SearchCondition with the following might prevent the problem, if I'm reading the comments correctly:

    var condition = new SearchCondition
    {
        Value = string.Format(@"X-GM-RAW ""AFTER:{0:yyyy-MM-dd}""", new DateTime(2017, 4, 13));
    }
    
    // Then pass your condition in to the search function
    var uids = ic.Search(condition);