POP3 클라이언트를 사용한 비동기 이메일 처리

Pop3Client와 비동기 작업

메시지 작업은 Aspose.Email을 사용하여 비동기적으로 수행할 수도 있습니다 Pop3Client. 이 문서는 사서함에서 메시지를 비동기적으로 검색하는 방법을 보여줍니다. 또한 검색 기준을 제공하여 메시지를 나열하는 방법을 보여줍니다. MailQuery. 작업을 중단하는 방법은 작업 기반 비동기 패턴( TAP) 메서드.

메시지 비동기 검색

다음 코드 스니펫은 메시지를 비동기적으로 검색하는 방법을 보여줍니다.

MailQuery를 사용한 비동기 메시지 목록

다음은 MailQuery 클래스는 다음 코드 샘플에 표시된 대로 비동기적으로 메시지 목록을 검색하기 위한 검색 기준을 지정하는 데 사용할 수 있습니다.

TAP 메서드 중단

.NET Framework 4.5부터 TAP 모델에 따라 구현된 비동기 메서드를 사용할 수 있습니다. 아래 코드 스니펫은 해당 작업 기반 비동기 패턴 메서드를 사용하여 사서함 정보를 받는 방법을 보여줍니다. GetMailboxInfoAsync 그리고 잠시 후 이 프로세스를 중단합니다.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET

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