SMTP with filesystem, POP3, IMAP (forward from POP3, send to SMTP and IMAP)


Introduction

MailBee.NET SMTP component allows you not only compose e-mails but also load them from other sources like files in MIME format, memory, e-mail messages received from a POP3 or IMAP server, and so on.

For instance, you can receive an e-mail from a POP3 or IMAP server, adjust it and send it again. Or, you can send an e-mail and also upload it into a folder on an IMAP server. You can save the e-mail into a file, read it back another time and send it, and so on.

Using MailBee.NET Outlook component, you can also load e-mails from Outlook .MSG and .PST files. This component is the part of MailBee.NET assembly, see PstReader and MsgConvert classes if interested.


Load e-mail from file and send it

Let's assume you have a file containing an e-mail message in MIME format (i.e. any valid .EML file). That's how you can read it from disk and send it out:

Smtp mailer = new Smtp();

// Use SMTP relay server with authentication.
mailer.SmtpServers.Add("mail.here.com", "joe", "secret");

mailer.Message.LoadMessage("C:\\Docs\\message.eml");
mailer.Send();
Dim mailer As New Smtp()

' Use SMTP relay server with authentication.
mailer.SmtpServers.Add("mail.here.com""joe""secret")

mailer.Message.LoadMessage("C:\Docs\message.eml")
mailer.Send()

We assume MailBee, MailBee.Mime, and MailBee.SmtpMail namespaces are imported and the license key is set. See Import namespaces and set license key topic for details.

The above sends a single e-mail. What if you got 1000 e-mails to be sent from files? You can use MailBee.NET Queue for that (it's a separate application with its own installer, not a part of MailBee.NET assembly).

If for some reason you can't use MailBee.NET Queue directly, you can still examine its source code to find how to efficiently use MailBee.NET to read e-mails from files and send them (MailBee.NET Queue comes with the full source code).

In particular, MailBee.NET Queue internally uses Smtp.AddJob (string, string, bool, string, EmailAddressCollection) method which allows the application to load from files and send out large volumes of e-mails without loading them all into memory at once.

You can also alter the e-mail which you loaded from a file, no limitations here. Add, remove or change any header or any part.

On other hand, you may want to completely disable MailBee.NET to alter the e-mail you read from a file. For instance, when a message gets sent, MailBee.NET by default changes its "Date" to the timestamp of the current moment, sets the unique Message-ID, can generate plain-text alternate body for HMTL-only e-mail, etc. To disable this, use the properties of MessageBuilderConfig class, like MessageBuilderConfig.SetDateOnSend property.

To access MessageBuilderConfig object of the message attached to the Smtp object (denoted by mailer instance), use MailMessage.Builder property. For instance, to disable automatic setting of "Date" header:

mailer.Message.Builder.SetDateOnSend = false;
mailer.Message.Builder.SetDateOnSend = false

The above applies to any methods of loading messages from external source (from file, stream, memory array, etc).


Send e-mail and save it to Sent Items on IMAP server

If you need to upload the e-mail you've just sent into a folder on your IMAP server, that's how:

Smtp mailer = new Smtp();

// First part of the log is all about SMTP session.
mailer.Log.Enabled = true;
mailer.Log.Filename = "C:\\Temp\\log.txt";
mailer.Log.Clear();

mailer.SmtpServers.Add("smtp.here.com", "joe@here.com", "secret");

mailer.Message.From.AsString = "Joe D. <joe@here.com>";
mailer.Message.To.AsString = "Carl S. <carl@there.com>";
mailer.Message.Subject = "Greetings from Joe";
mailer.Message.BodyPlainText = "Hi there!";

mailer.Send();

Console.WriteLine("Sent successfully");

Imap imp = new Imap();

// Second part of the log represents IMAP session.
imp.Log.Enabled = true;
imp.Log.Filename = "C:\\Temp\\log.txt";

imp.Connect("imap.here.com");
imp.Login("joe@here.com", "secret");

try
{
    imp.UploadMessage(mailer.Message, "Sent");
    Console.WriteLine("Uploaded successfully");
}
catch (MailBeeImapNegativeResponseException e)
{
    Console.WriteLine("Perhaps, can't select folder");
    if (!string.IsNullOrEmpty(e.HumanReadable))
    {
        Console.WriteLine("Server reported: " + e.HumanReadable);
    }
    Console.WriteLine();
    Console.WriteLine("The folders available are:");
    FolderCollection folders = imp.DownloadFolders();
    foreach (Folder fold in folders)
    {
        Console.WriteLine(fold.Name);
    }
}

imp.Disconnect();
Dim mailer As New Smtp()

' First part of the log is all about SMTP session.
mailer.Log.Enabled = True
mailer.Log.Filename = "C:\Temp\log.txt"
mailer.Log.Clear()

mailer.SmtpServers.Add("smtp.here.com", "joe@here.com", "secret")

mailer.Message.From.AsString = "Joe D. <joe@here.com>"
mailer.Message.To.AsString = "Carl S. <carl@there.com>"
mailer.Message.Subject = "Greetings from Joe"
mailer.Message.BodyPlainText = "Hi there!"

mailer.Send()

Console.WriteLine("Sent successfully")

Dim imp As New Imap()

' Second part of the log represents IMAP session.
imp.Log.Enabled = True
imp.Log.Filename = "C:\Temp\log.txt"

imp.Connect("imap.here.com")
imp.Login("joe@here.com", "secret")

Try
    imp.UploadMessage(mailer.Message, "Sent")
    Console.WriteLine("Uploaded successfully")
Catch e As MailBeeImapNegativeResponseException
    Console.WriteLine("Perhaps, can't select folder")
    If Not String.IsNullOrEmpty(e.HumanReadable) Then
        Console.WriteLine("Server reported: " & e.HumanReadable)
    End If
    Console.WriteLine()
    Console.WriteLine("The folders available are:")
    Dim folders As FolderCollection = imp.DownloadFolders()
    Dim fold As Folder
    For Each fold In folders
        Console.WriteLine(fold.Name)
    Next fold
End Try

imp.Disconnect()

We assume your license key covers not only Smtp class but Imap as well. You also need to import MailBee.ImapMail namespace.


Send e-mail and save it to file stream

The sample below sends an e-mail and saves it into a stream.

We're using the file stream here but this is for demonstration purposes only. You can save directly in files with MailBee.NET (see the commented piece of the code). With streams, however, you can output the message data anywhere.

To compile this sample, you'll need to import System.IO namespace as well:

Smtp mailer = new Smtp();

mailer.SmtpServers.Add("smtp.here.com", "joe@here.com", "secret");

mailer.Message.From.AsString = "Joe D. <joe@here.com>";
mailer.Message.To.AsString = "Carl S. <carl@there.com>";
mailer.Message.Subject = "Greetings from Joe";
mailer.Message.BodyPlainText = "Hi there!";

mailer.Send();

// Use stream to save e-mail to disk.
FileStream fs = new FileStream("C:\\Temp\\message.eml", FileMode.Create);
mailer.Message.SaveMessage(fs);
fs.Close();

// Simpler method to save e-mail to disk.
// mailer.Message.SaveMessage("C:\\Temp\\message.eml");

Console.WriteLine("Sent and saved");
Dim mailer As New Smtp()

mailer.SmtpServers.Add("smtp.here.com", "joe@here.com", "secret")

mailer.Message.From.AsString = "Joe D. <joe@here.com>"
mailer.Message.To.AsString = "Carl S. <carl@there.com>"
mailer.Message.Subject = "Greetings from Joe"
mailer.Message.BodyPlainText = "Hi there!"

mailer.Send()

' Use stream to save e-mail to disk.
Dim fs As New FileStream("C:\Temp\message.eml", FileMode.Create)
mailer.Message.SaveMessage(fs)
fs.Close()

' Simpler method to save e-mail to disk.
' mailer.Message.SaveMessage("C:\\Temp\\message.eml")

Console.WriteLine("Sent and saved")

Receive e-mail with POP3, forward it as attachment of another e-mail and send with SMTP

Individual MailBee.NET components can efficiently interoperate with each other. For instance, you can get an existing e-mail (for example, receive it from a POP3 server), alter it in some way or even build a new e-mail from it, and send the resulting e-mail out (or, upload it to an IMAP server, save it into a file or whatever).

This sample receives an e-mail from a POP3 server, creates a new e-mail, attaches the previously received e-mail to the new e-mail in "forward as attachment" way (it does this twice to demonstrate different methods of this), and submits the resulting e-mail to the SMTP relay server:

// Get the most recent message in Inbox.
MailMessage msg = Pop3.QuickDownloadMessage(
    "pop.domain.com", "joe", "secret", -1);

Smtp mailer = new Smtp();
mailer.SmtpServers.Add("smtp.domain.com", "joe", "secret");

// Build forwarded message, method #1 (simpler).
// Creates new message and attaches old message.
mailer.Message = msg.ForwardAsAttachment();

// Build forwarded message, method #2 (allows adding multiple forwarded
// messages). Adds the message being forwarded to an existing message.
mailer.Message.Attachments.Add(msg, string.Empty, null, null, null,
    NewAttachmentOptions.Inline, MailTransferEncoding.None);

mailer.Message.From.AsString = "Joe D. <joe@domain.com>";
mailer.Message.To.AsString = "Carl S. <carl@there.com>";
mailer.Message.Subject = "FW: message";
mailer.Message.BodyPlainText = "See the forwarded message";

mailer.Send();
' Get the most recent message in Inbox.
Dim msg As MailMessage = Pop3.QuickDownloadMessage( _
    "pop.domain.com", "joe", "secret", -1)

Dim mailer As New Smtp()
mailer.SmtpServers.Add("smtp.domain.com", "joe", "secret")

' Build forwarded message, method #1 (simpler).
' Creates new message and attaches old message.
mailer.Message = msg.ForwardAsAttachment()

' Build forwarded message, method #2 (allows adding multiple forwarded
' messages). Adds the message being forwarded to an existing message.
mailer.Message.Attachments.Add(msg, String.Empty, Nothing, Nothing, Nothing, _
    NewAttachmentOptions.Inline, MailTransferEncoding.None)

mailer.Message.From.AsString = "Joe D. <joe@domain.com>"
mailer.Message.To.AsString = "Carl S. <carl@there.com>"
mailer.Message.Subject = "FW: message"
mailer.Message.BodyPlainText = "See the forwarded message"

mailer.Send()

We assume the license key covers not only Smtp class but Pop3 as well. You also need to import MailBee.Pop3Mail namespace.


Receive e-mail with IMAP, reply to it and send via SMTP

Replying and forwarding inline (not as attachment) in the general case is complicated if you want to keep the original message text in the reply. This is because there is not single standard for this and each e-mail client uses different methods for this. Replying to HTML and plain-text messages may also be different.

This sample makes a reply in a pretty simple way. The original body is put in behind the new text, and a separator is inserted in between. If the source message is HTML-only, plain-text is generated from it first as the sample creates the reply in plain-text:

// Get the most recent message in Inbox.
MailMessage msg = Imap.QuickDownloadMessage(
    "mail.provider.com", "joe@here.com", "secret", "Inbox", -1);

Smtp mailer = new Smtp();
mailer.SmtpServers.Add("mail.provider.com", "joe@here.com", "secret");

// Make sure we've got plain-text body of the original,
// even if the original message didn't have it.
msg.Parser.HtmlToPlainMode = HtmlToPlainAutoConvert.IfNoPlain;

// Set the reply body.
mailer.Message.BodyPlainText =
    "This is reply, the original follows...\r\n\r\n" +
    "--------------------------------------\r\n\r\n" +
    msg.BodyPlainText;

// Set "To" from "Reply-To" or "From" of the original.
if (msg.ReplyTo.Count > 0)
{
    mailer.Message.To.Add(msg.ReplyTo);
}
else
{
    mailer.Message.To.Add(msg.From);
}

// Add In-Reply-To to make it easier for the receiving side
// to match this reply and the original message.
mailer.Message.Headers["In-Reply-To"] = msg.MessageID;

// Set other headers. For demonstration purposes, in "From" field,
// specify display name and e-mail address separately.
mailer.Message.From = new EmailAddress("Joe D.", "joe@here.com");
mailer.Message.Subject = "Re: " + msg.Subject;

mailer.Send();
' Get the most recent message in Inbox.
Dim msg As MailMessage = Imap.QuickDownloadMessage( _
    "mail.provider.com", "joe@here.com", "secret", "Inbox", -1)

Dim mailer As New Smtp()
mailer.SmtpServers.Add("mail.provider.com", "joe@here.com", "secret")

' Make sure we've got plain-text body of the original,
' even if the original message didn't have it.
msg.Parser.HtmlToPlainMode = HtmlToPlainAutoConvert.IfNoPlain

' Set the reply body.
mailer.Message.BodyPlainText = _
    "This is reply, the original follows..." & vbCrLf & vbCrLf & _
    "--------------------------------------" & vbCrLf & vbCrLf & _
    msg.BodyPlainText

' Set "To" from "Reply-To" or "From" of the original.
If msg.ReplyTo.Count > 0 Then
    mailer.Message.To.Add(msg.ReplyTo)
Else
    mailer.Message.To.Add(msg.From)
End If

' Add In-Reply-To to make it easier for the receiving side
' to match this reply and the original message.
mailer.Message.Headers("In-Reply-To") = msg.MessageID

' Set other headers. For demonstration purposes, in "From" field,
' specify display name and e-mail address separately.
mailer.Message.From = New EmailAddress("Joe D.", "joe@here.com")
mailer.Message.Subject = "Re: " & msg.Subject

mailer.Send()

We assume your license key covers not only Smtp class but Imap as well. You also need to import MailBee.ImapMail namespace.


Send feedback to AfterLogic

Copyright © 2006-2024 AfterLogic Corporation. All rights reserved.