Quantcast
Channel: ImapX 2
Viewing all articles
Browse latest Browse all 1952

New Post: IDLE support?

$
0
0
Hi Pavel

I am struggling to use ImapX in a windows service.

The service must use IDLE to wait for incoming messages and save their attachments to disk.

It works periodically but mostly not, maby you can see what I'm doing wrong?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using ImapX;
using ImapX.Exceptions;
using System.Net.Sockets;

namespace IMAP.Service
{
public partial class Service : ServiceBase
{
    private static string pathRoot = @"C:\attachments\";
    private static string host = "xxxxxxxxxx";
    private static string lockin = "xxxx@xxxxx.com";
    private static string password = "xxxxx";

    public Service()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        try
        {
            ImapClient client = new ImapClient(host, useSsl: true, validateServerCertificate: false);
            if (client.Connect())
            {
                if (client.Login(lockin, password))
                {
                    LogEvent("Imap Client Loged in", EventLogEntryType.Information);

                    // Attach handler to message arrived event
                    client.OnNewMessagesArrived += ImapClient_OnNewMessagesArrived;

                    // Start idling for inbox messages
                    client.Folders.Inbox.StartIdling();
                    LogEvent("IMAP Service idling started", EventLogEntryType.Information);
                }
                else LogEvent("User or password invalid", EventLogEntryType.Error);
            }
            else LogEvent("Connection could not be established", EventLogEntryType.Error);
        }
        catch (Exception e)
        {
            LogEvent("General exception.\n" + e.Message, EventLogEntryType.Error);
        }
    }

    static void ImapClient_OnNewMessagesArrived(object sender, IdleEventArgs e)
    {
        LogEvent("Message received", EventLogEntryType.Information);

        foreach (Message massage in e.Messages)
        {
            foreach (Attachment file in massage.Attachments)
            {
                file.Download();
                file.Save(pathRoot);
            }
        }

        LogEvent("Message processed", EventLogEntryType.Information);
    }

    private static void LogEvent(string message, EventLogEntryType type)
    {
        EventLog EventLog = new EventLog();
        EventLog.Source = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
        EventLog.WriteEntry(message, type);
    }

    protected override void OnStop()
    {
        LogEvent("IMAP Service stoped", EventLogEntryType.Information);
    }
}
}

Viewing all articles
Browse latest Browse all 1952

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>