POP3 İstemcisi ile Asenkron E-posta İşleme

Pop3Client ile Asenkron İşlemler

Mesajlarla çalışmak, Aspose.Email kullanılarak asenkron olarak da gerçekleştirilebilir Pop3Client. Bu makale, bir posta kutusundan mesajları asenkron olarak nasıl alacağınızı gösterir. Ayrıca, arama kriterleri sağlayarak mesajları listelemeyi … kullanarak gösterir. MailQuery. Görev tabanlı asenkron desenle başlatılan bir posta kutusuyla bir işlemi nasıl kesintiye uğratacağınız ayrı ayrı gösterilecektir (TAP) method.

Mesajları Asenkron Olarak Al

Aşağıdaki kod parçacığı, mesajları asenkron olarak nasıl alacağınızı gösterir.

Pop3Client client = new Pop3Client();
client.Host = "pop.gmail.com";
client.Port = 995;
client.SecurityOptions = SecurityOptions.SSLImplicit;
client.Username = "username";
client.Password = "password";
            
try
{
    Pop3MessageInfoCollection messages = client.ListMessages();
    Console.WriteLine("Total Number of Messages in inbox:" + messages.Count);
    AutoResetEvent evnt = new AutoResetEvent(false);
    MailMessage message = null;
    AsyncCallback callback = delegate(IAsyncResult ar)
    {
        message = client.EndFetchMessage(ar);
        evnt.Set();
    };
    client.BeginFetchMessage(messages[0].SequenceNumber, callback, null);
    evnt.WaitOne();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

MailQuery ile Mesajları Asenkron Listele

Bu MailQuery sınıf, aşağıdaki kod örneğinde gösterildiği gibi mesajların asenkron olarak bir listesini almak için arama kriterleri belirlemede kullanılabilir.

MailQueryBuilder builder = new MailQueryBuilder();
builder.Subject.Contains("Subject");
MailQuery query = builder.GetQuery();
IAsyncResult asyncResult = client.BeginListMessages(query);
Pop3MessageInfoCollection messages = client.EndListMessages(asyncResult);

Bir TAP Yöntemini Kesintiye Uğratma

.NET Framework 4.5’ten itibaren, TAP modeline göre uygulanmış asenkron yöntemleri kullanabilirsiniz. Aşağıdaki kod parçacığı, görev tabanlı asenkron desen yöntemi kullanarak bir posta kutusu hakkında bilgi almanın nasıl yapılacağını gösterir GetMailboxInfoAsync ve ardından bir süre sonra bu işlemi kesintiye uğratın.


using (Pop3Client client = new Pop3Client(host, 995, senderEmail, password, SecurityOptions.Auto))
{
    CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
    AutoResetEvent autoResetEvent = new AutoResetEvent(false);
    Exception exception = null;

    ThreadPool.QueueUserWorkItem(delegate
    {
        try
        {
            // start receiving mailbox information
            var task = client.GetMailboxInfoAsync(cancellationTokenSource.Token);
            Pop3MailboxInfo mailboxInfo = task.GetAwaiter().GetResult();
            Console.WriteLine("Message count: " + mailboxInfo.MessageCount);
        }
        catch (Exception e)
        {
            exception = e;
        }
        finally
        {
            autoResetEvent.Set();
        }
    });

    Thread.Sleep(2000);

    // stop receiving mailbox information
    cancellationTokenSource.Cancel();
    autoResetEvent.WaitOne();

    if (exception is OperationCanceledException)
        Console.WriteLine("Operation has been interrupted: " + exception.Message);
}