1

I'm trying to fetch emails as soon as they arrive in my inbox using MailSystem.NET library. Everything works fine IMAP client gets connected but my NewMessageReceived event is never fired.

Please Help

Below is the code:

public static Imap4Client _imap = new Imap4Client();
    public string SenderEmailAddress = System.Configuration.ConfigurationManager.AppSettings["EmailAddress"];
    public string SenderEmailPassword = System.Configuration.ConfigurationManager.AppSettings["EmailPassword"];
    public static Mailbox inbox = new Mailbox();
    protected void Application_Start()
    {

        var worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(StartIdleProcess);

        if (worker.IsBusy)
            worker.CancelAsync();

        worker.RunWorkerAsync();
    }

    private void StartIdleProcess(object sender, DoWorkEventArgs e)
    {
        try
        {


            if (_imap != null && _imap.IsConnected)
            {
                _imap.StopIdle();
                _imap.Disconnect();
            }

            _imap = new Imap4Client();
            _imap.NewMessageReceived += new NewMessageReceivedEventHandler(NewMessageReceived);
            _imap.ConnectSsl("imap.gmail.com", 993);
            _imap.Login(SenderEmailAddress, SenderEmailPassword);

            inbox = _imap.SelectMailbox("inbox");
            int[] ids = inbox.Search("UNSEEN");


            inbox.Subscribe();

            _imap.StartIdle();
        }
        catch (Exception ex)
        {

        }
    }

    public static void NewMessageReceived(object source, NewMessageReceivedEventArgs e)
    {
        int offset = e.MessageCount - 2;
        Message message = inbox.Fetch.MessageObject(offset);
        Debug.WriteLine("message subject: " + message.Subject);
        // Do something with the source...

        _imap.StopIdle();
    }
DevSab
  • 121
  • 6
  • I tried updating my code as mentioned here http://mailsystem.codeplex.com/discussions/244156 but it still doesn't work. – DevSab May 24 '17 at 16:56
  • I am also facing the same problem. Are you able to get any solution? – Mannu Dec 02 '19 at 07:34

1 Answers1

1

I can't tell you the exact reason but it seems that interacting with the imapclient from the NewMessageReceived event just doesn't work.

In NewMessageReceived call _imap.StopIdle() then continue in your main execution flow and restart idle. Then use a boolean to drop out of the loop entirely.

private bool _stop = false;

private void StartIdle(object sender, DoWorkEventArgs e)
{
   //Setup client
   _imap = new Imap4Client();
   _imap.NewMessageReceived += new NewMessageReceivedEventHandler(NewMessageReceived);
   StartRepeatExecution();
}

public void StartRepeatExecution()
{
   _imap.StartIdle();
   if(_stop) return;

   //Handle your new messages here! dummy code
    var mailBox = _imap.SelectMailBox("inbox");
    var messages = mailBox.SearchParse("").Last();

   StartRepeatExecution();
}

public static void NewMessageReceived(object source, NewMessageReceivedEventArgs e)
{
   //StopIdle will return to where _imap.StartIdle() was called.
   _imap.StopIdle();
}

public void StopRepeatExecution()
{ 
   _stop = true;
}
Chem
  • 11
  • 1