Asynchronously authenticates the specified user to the remote IMAP server with the specified credentials using the specified authentication method.
public Task AuthenticateAsync(
string userName,
string password,
ImapAuthenticationMethod authenticationMethod,
AsyncCompletedEventHandler callback,
object userState = null
)
Public Function AuthenticateAsync( _
ByVal userName As String, _
ByVal password As String, _
ByVal authenticationMethod As ImapAuthenticationMethod, _
ByVal callback As AsyncCompletedEventHandler, _
ByVal userState As Object = null _
) As Task
public:
Task AuthenticateAsync(
String^ userName,
String^ password,
ImapAuthenticationMethod authenticationMethod,
AsyncCompletedEventHandler^ callback,
Object^ userState = null
);
Parameters
- userName
- The username.
- password
- Password for the given username.
- authenticationMethod
- The IMAP authentication method.
- callback
- An optional asynchronous callback to be invoked when this operation completes. This parameter can be null.
- userState
- A user-provided object that identifies this particular asynchronous operation.
Asynchronously connect to an IMAP server and list existing mailboxes (Task-based asynchronous approach).
using System;
using System.ComponentModel;
using System.Text;
using ComponentPro.Net.Mail;
using ComponentPro;
...
// Create a new instance.
Imap client = new Imap();
try
{
// Connect to the IMAP server asynchronously.
await client.ConnectAsync("myserver");
// Or you can specify the IMAP port with
// await client.ConnectAsync("myserver", 143);
// Authenticate the user asynchronously.
await client.AuthenticateAsync("userName", "password");
// Do something here...
StringBuilder sb = new StringBuilder();
FolderCollection list = client.ListFolders();
for (int i = 0; i < list.Count; i++)
{
sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].Name);
}
Console.WriteLine(sb.ToString());
// ...
// Disconnect asynchronously.
await client.DisconnectAsync();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.ToString());
}
Imports System.ComponentModel
Imports System.Text
Imports ComponentPro.Net.Mail
Imports ComponentPro
...
' Create a new instance.
Dim client As New Imap()
Try
' Connect to the IMAP server asynchronously.
Await client.ConnectAsync("myserver")
' Or you can specify the IMAP port with
' Await client.ConnectAsync("myserver", 143);
' Authenticate the user asynchronously.
Await client.AuthenticateAsync("userName", "password")
' Do something here...
Dim sb As New StringBuilder()
Dim list As FolderCollection = client.ListFolders()
For i As Integer = 0 To list.Count - 1
sb.AppendFormat("{0} - {1}" & vbCrLf, i + 1, list(i).Name)
Next i
Console.WriteLine(sb.ToString())
' ...
' Disconnect asynchronously.
Await client.DisconnectAsync()
Catch ex As Exception
Console.WriteLine("Error: " & ex.ToString())
End Try
Asynchronously connect to an IMAP server and list existing mailboxes (Event-based asynchronous approach).
using System;
using System.ComponentModel;
using System.Text;
using ComponentPro.Net.Mail;
using ComponentPro;
...
public void DoAsyncConnect()
{
// Create a new instance.
Imap client = new Imap();
client.ConnectCompleted += client_ConnectCompleted;
client.AuthenticateCompleted += client_AuthenticateCompleted;
client.DisconnectCompleted += client_DisconnectCompleted;
// Connect to the IMAP server asynchronously.
client.ConnectAsync("myserver");
// Or you can specify the IMAP port with
// client.ConnectAsync("myserver", 143);
// Your code here
// ...
}
void client_ConnectCompleted(object sender, ExtendedAsyncCompletedEventArgs<string> e)
{
Imap client = (Imap)sender;
if (e.Error != null)
{
Console.WriteLine("Error: " + e.Error.ToString());
return;
}
// Authenticate the user asynchronously.
client.AuthenticateAsync("userName", "password");
}
void client_AuthenticateCompleted(object sender, AsyncCompletedEventArgs e)
{
Imap client = (Imap)sender;
if (e.Error != null)
{
Console.WriteLine("Error: " + e.Error.ToString());
return;
}
// Do something here...
StringBuilder sb = new StringBuilder();
FolderCollection list = client.ListFolders();
for (int i = 0; i < list.Count; i++)
{
sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].Name);
}
Console.WriteLine(sb.ToString());
// ...
// Disconnect asynchronously.
client.DisconnectAsync();
}
void client_DisconnectCompleted(object sender, ExtendedAsyncCompletedEventArgs<string> e)
{
Imap client = (Imap)sender;
if (e.Error != null)
{
Console.WriteLine("Error: " + e.Error.ToString());
}
}
Imports System.ComponentModel
Imports System.Text
Imports ComponentPro.Net.Mail
Imports ComponentPro
...
Public Sub DoAsyncConnect()
' Create a new instance.
Dim client As New Imap()
AddHandler client.ConnectCompleted, AddressOf client_ConnectCompleted
AddHandler client.AuthenticateCompleted, AddressOf client_AuthenticateCompleted
AddHandler client.DisconnectCompleted, AddressOf client_DisconnectCompleted
' Connect to the IMAP server asynchronously.
client.ConnectAsync("myserver")
' Or you can specify the IMAP port with
' client.ConnectAsync("myserver", 143);
' Your code here
' ...
End Sub
Private Sub client_ConnectCompleted(ByVal sender As Object, ByVal e As ExtendedAsyncCompletedEventArgs(Of String))
Dim client As Imap = CType(sender, Imap)
If e.Error IsNot Nothing Then
Console.WriteLine("Error: " & e.Error.ToString())
Return
End If
' Authenticate the user asynchronously.
client.AuthenticateAsync("userName", "password")
End Sub
Private Sub client_AuthenticateCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
Dim client As Imap = CType(sender, Imap)
If e.Error IsNot Nothing Then
Console.WriteLine("Error: " & e.Error.ToString())
Return
End If
' Do something here...
Dim sb As New StringBuilder()
Dim list As FolderCollection = client.ListFolders()
For i As Integer = 0 To list.Count - 1
sb.AppendFormat("{0} - {1}" & vbCrLf, i + 1, list(i).Name)
Next i
Console.WriteLine(sb.ToString())
' ...
' Disconnect asynchronously.
client.DisconnectAsync()
End Sub
Private Sub client_DisconnectCompleted(ByVal sender As Object, ByVal e As ExtendedAsyncCompletedEventArgs(Of String))
Dim client As Imap = CType(sender, Imap)
If e.Error IsNot Nothing Then
Console.WriteLine("Error: " & e.Error.ToString())
End If
End Sub