IMAP also allows you to upload message from a client to a folder on a IMAP server, and Imap class provides the UploadMessage method for this purpose. By using the UploadMessage method you can upload message from a various source such as from file, stream, or MailMessage object. Unlike other message-related methods, the UploadMessage method uploads the message into a specified folder, so that the current folder is not used for the operation, and you do not need to set the current folder before using the UploadMessage method.
The following steps will help you to do that:
Uploading a message
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
using System;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
- Create a new instance of the Imap class.
- Now you can connect to the IMAP server with Connect methods. The code looks similar to the following:
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "password";
const int port = 993;
const SslSecurityMode securityMode = SslSecurityMode.Implicit;
Imap client = new Imap();
try
{
Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port);
// Connect to the server.
client.Connect(serverName, port, securityMode);
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "password"
Const port As Integer = 993
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
Dim client As New Imap()
Try
Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port)
' Connect to the server.
client.Connect(serverName, port, securityMode)
- Use your user name and password to login with Authenticate methods. The code looks similar to the following:
// Login to the server.
Console.WriteLine("Logging in as {0}...", user);
client.Authenticate(user, password);
' Login to the server.
Console.WriteLine("Logging in as {0}...", user)
client.Authenticate(user, password)
- Now create a new MailMessage object and upload it into INBOX mailbox. The code looks similar to the following:
// Create a new MailMessage object.
MailMessage message = new MailMessage();
message.From.Add("from@thedomain.com");
message.To.Add("name@domain.com");
message.Subject = "Test Subject";
message.BodyText = "Test Content";
// Upload message.
Console.WriteLine("Uploading message...");
// Upload the message to the 'Inbox' mailbox.
client.UploadMessage("Inbox", message);
' Create a new MailMessage object.
Dim message As New MailMessage()
message.From.Add("from@thedomain.com")
message.To.Add("name@domain.com")
message.Subject = "Test Subject"
message.BodyText = "Test Content"
' Upload message.
Console.WriteLine("Uploading message...")
' Upload the message to the 'Inbox' mailbox.
client.UploadMessage("Inbox", message)
- After completing your work, call the Disconnect method to close the IMAP session.
Final example code
using System;
using ComponentPro.Net;
using ComponentPro.Net.Mail;
...
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "password";
const int port = 993;
const SslSecurityMode securityMode = SslSecurityMode.Implicit;
Imap client = new Imap();
try
{
Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port);
// Connect to the server.
client.Connect(serverName, port, securityMode);
// Login to the server.
Console.WriteLine("Logging in as {0}...", user);
client.Authenticate(user, password);
// Create a new MailMessage object.
MailMessage message = new MailMessage();
message.From.Add("from@thedomain.com");
message.To.Add("name@domain.com");
message.Subject = "Test Subject";
message.BodyText = "Test Content";
// Upload message.
Console.WriteLine("Uploading message...");
// Upload the message to the 'Inbox' mailbox.
client.UploadMessage("Inbox", message);
// Disconnect.
Console.WriteLine("Disconnecting...");
client.Disconnect();
}
catch (ImapException imapExc)
{
Console.WriteLine(string.Format("An IMAP error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status));
}
catch (Exception exc)
{
Console.WriteLine(string.Format("An error occurred: {0}", exc.Message));
}
Imports ComponentPro.Net
Imports ComponentPro.Net.Mail
...
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "password"
Const port As Integer = 993
Const securityMode As SslSecurityMode = SslSecurityMode.Implicit
Dim client As New Imap()
Try
Console.WriteLine("Connecting IMAP server: {0}:{1}...", serverName, port)
' Connect to the server.
client.Connect(serverName, port, securityMode)
' Login to the server.
Console.WriteLine("Logging in as {0}...", user)
client.Authenticate(user, password)
' Create a new MailMessage object.
Dim message As New MailMessage()
message.From.Add("from@thedomain.com")
message.To.Add("name@domain.com")
message.Subject = "Test Subject"
message.BodyText = "Test Content"
' Upload message.
Console.WriteLine("Uploading message...")
' Upload the message to the 'Inbox' mailbox.
client.UploadMessage("Inbox", message)
' Disconnect.
Console.WriteLine("Disconnecting...")
client.Disconnect()
Catch imapExc As ImapException
Console.WriteLine(String.Format("An IMAP error occurred: {0}, ErrorStatus: {1}", imapExc.Message, imapExc.Status))
Catch exc As Exception
Console.WriteLine(String.Format("An error occurred: {0}", exc.Message))
End Try