Compose e-mail created from another object or file

This guide explains how you can send an existing e-mail (for instance, the e-mail previously stored on disk, in database, XML file, created with System.Net.Mail.MailMessage object, etc).


Send e-mail created from System.Net.Mail.MailMessage

If your existing code composes e-mails with the help of System.Net.Mail.MailMessage class, you can re-use this code with MailBee.NET and easily export this object into MailBee.Mime.MailMessage object used by MailBee.NET class as the standard representation of an e-mail.

When dealing with System.Net.Mail and MailBee.NET classes in the same application, you'll need to use fully qualified type names for MailMessage class because both System.Net.Mail and MailBee.Mime namespaces have classes with such name:

System.Net.Mail.MailMessage sysMsg = new System.Net.Mail.MailMessage();
sysMsg.Subject = "Test message";
MailBee.Mime.MailMessage msg = new MailBee.Mime.MailMessage();
msg.ConvertFromSystemNetMail(sysMsg);
Dim sysMsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
sysMsg.Subject = "Test message"
Dim msg As MailBee.Mime.MailMessage = New MailBee.Mime.MailMessage()
msg.ConvertFromSystemNetMail(sysMsg)

Or, you can create aliases for System.Net.Mail.MailMessage and MailBee.Mime.MailMessage classes and use these alias names instead.

The below is a complete Console application sample which imports all the required namespaces, defines aliases and sends the e-mail built from System.Net.Mail.MailMessage class (we assume the license key is already set outside the code, see Import namespaces and set license key topic for details).

In particular, we build the new e-mail as a container e-mail and add the original e-mail (converted from System.Net.Mail.MailMessage) as an attachment to the container e-mail ("forward as attachment" technique):

using System;
using System.Net.Mail;
using MailBee;
using MailBee.Mime;
using MailBee.SmtpMail;
using SysMailMessage = System.Net.Mail.MailMessage;
using MailBeeMessage = MailBee.Mime.MailMessage;

class Program
{
    static void Main(string[] args)
    {
        // Prepare System.Net.Mail.MailMessage object.
        SysMailMessage sysMsg = new SysMailMessage();
        sysMsg.Subject = "Test message";
        sysMsg.From = new MailAddress("john.doe@domain.com");
        sysMsg.To.Add("jane.doe@company.com");
        sysMsg.Body = "Test message body";

        // Create MailBee.Mime.MailMessage from System.Net.Mail.MailMessage.
        MailBeeMessage msg = new MailBeeMessage();
        msg.ConvertFromSystemNetMail(sysMsg);

        Smtp mailer = new Smtp();

        // Use SMTP relay server with authentication.
        mailer.SmtpServers.Add("mail.domain.com", "john.doe", "secret");

        // Create an envelope message, add the original message as attachment
        // of the envelope message, and assign the envelope message as the
        // message to be sent by Smtp object.
        mailer.Message = msg.ForwardAsAttachment();

        // Set other headers of the message to be sent.
        mailer.Message.From.Email = "john@here.com";
        mailer.Message.To.Add("mike@there.com");
        mailer.Message.Subject = "See forwarded message in attachment";

        mailer.Send();
    }
}
Imports System
Imports System.Net.Mail
Imports MailBee
Imports MailBee.Mime
Imports MailBee.SmtpMail
Imports SysMailMessage = System.Net.Mail.MailMessage
Imports MailBeeMessage = MailBee.Mime.MailMessage 

Module Module1
    Sub Main()
        ' Prepare System.Net.Mail.MailMessage object.
        Dim sysMsg As SysMailMessage = New SysMailMessage()
        sysMsg.Subject = "Test message"
        sysMsg.From = New MailAddress("john.doe@domain.com")
        sysMsg.To.Add("jane.doe@company.com")
        sysMsg.Body = "Test message body"

        ' Create MailBee.Mime.MailMessage from System.Net.Mail.MailMessage.
        Dim msg As MailBeeMessage = New MailBeeMessage()
        msg.ConvertFromSystemNetMail(sysMsg)

        Dim mailer As Smtp = New Smtp() 

        ' Use SMTP relay server with authentication.
        mailer.SmtpServers.Add("mail.domain.com", "john.doe", "secret")

        ' Create an envelope message, add the original message as attachment
        ' of the envelope message, and assign the envelope message as the
        ' message to be sent by Smtp object.
        mailer.Message = msg.ForwardAsAttachment()

        ' Set other headers of the message to be sent.
        mailer.Message.From.Email = "john@here.com"
        mailer.Message.To.Add("mike@there.com")
        mailer.Message.Subject = "See forwarded message in attachment"

        mailer.Send()
    End Sub
End Module

In many cases, you won't need to worry about collisions of System.Net.Mail.MailMessage and MailBee.Mime.MailMessage class names at all. Just don't import MailBee.Mime namespace if your code never (or at least rarely) directly uses the class names within MailBee.Mime namespace.

The Console application sample below is different from the previous sample in that it simply re-sends the message which was converted from System.Net.Mail.MailMessage and never uses any class names within MailBee.Mime namespace (the sample does use Smtp.Message property which is of MailBee.Mime.MailMessage type but it never uses this class name explicitly):

using System;
using System.Net.Mail;
using MailBee;
using MailBee.SmtpMail;

class Program
{
    static void Main(string[] args)
    {
        // Prepare System.Net.Mail.MailMessage object. We don't need to use
        // aliases or full class names as MailBee.Mime namespace wasn't imported.
        MailMessage sysMsg = new MailMessage();
        sysMsg.Subject = "Test message";
        sysMsg.From = new MailAddress("john.doe@domain.com");
        sysMsg.To.Add("jane.doe@company.com");
        sysMsg.Body = "Test message body";

        Smtp mailer = new Smtp();

        // Use SMTP relay server with authentication.
        mailer.SmtpServers.Add("mail.domain.com", "john.doe", "secret");

        // We implicitly use MailBee.Mime.MailMessage here but the compiler anyway
        // knows the type of mailer.Message and the code compiles just fine even
        // though we never imported MailBee.Mime namespace.
        mailer.Message.ConvertFromSystemNetMail(sysMsg);

        // Demonstrate that we can modify the converted message.
        // Charset property is a good example as you can't easily set the charset
        // with System.Net.Mail.MailMessage and that can be the reason why you
        // need to do this at MailBee.Mime stage (not at System.Net.Mail stage).
        mailer.Message.Charset = "utf-8";

        mailer.Send();
    }
}
Imports System
Imports System.Net.Mail
Imports MailBee
Imports MailBee.Mime
Imports MailBee.SmtpMail
Imports SysMailMessage = System.Net.Mail.MailMessage
Imports MailBeeMessage = MailBee.Mime.MailMessage 

Module Module1
    Sub Main()
        ' Prepare System.Net.Mail.MailMessage object.
        Dim sysMsg As SysMailMessage = New SysMailMessage()
        sysMsg.Subject = "Test message"
        sysMsg.From = New MailAddress("john.doe@domain.com")
        sysMsg.To.Add("jane.doe@company.com")
        sysMsg.Body = "Test message body"

        ' Create MailBee.Mime.MailMessage from System.Net.Mail.MailMessage.
        Dim msg As MailBeeMessage = New MailBeeMessage()
        msg.ConvertFromSystemNetMail(sysMsg)

        Dim mailer As Smtp = New Smtp() 

        ' Use SMTP relay server with authentication.
        mailer.SmtpServers.Add("mail.domain.com", "john.doe", "secret")

        ' Create an envelope message, add the original message as attachment
        ' of the envelope message, and assign the envelope message as the
        ' message to be sent by Smtp object.
        mailer.Message = msg.ForwardAsAttachment()

        ' Set other headers of the message to be sent.
        mailer.Message.From.Email = "john@here.com"
        mailer.Message.To.Add("mike@there.com")
        mailer.Message.Subject = "See forwarded message in attachment"

        mailer.Send()
    End Sub
End Module

MailBee.NET also allows you to convert MailBee.Mime.MailMessage back to System.Net.Mail.MailMessage. Use MailBee.Mime.MailMessage.ConvertToSystemNetMail method if you need this.


Send e-mail created from memory

If you have an existing e-mail in the form of a byte array or byte stream, you can easily load it into MailMessage object using the overloads of MailMessage.LoadMessage method which accept byte array or Stream, then alter the properties of the loaded message if needed, and finally send it.

The sample below sends the e-mail loaded from a byte array (which you could have received by reading from any data source like database):

Smtp mailer = new Smtp();

// Use SMTP relay server with authentication.
mailer.SmtpServers.Add("mail.domain.com", "john.doe", "secret");

// Load the message from byte array.
mailer.Message.LoadMessage(msgData);

// Demonstrate that we can modify the loaded message.
// Update the date when the message was composed to the current moment.
mailer.Message.Date = DateTime.Now;

mailer.Send();
Dim mailer As Smtp = New Smtp()

' Use SMTP relay server with authentication.
mailer.SmtpServers.Add("mail.domain.com", "john.doe", "secret")

' Load the message from byte array.
mailer.Message.LoadMessage(msgData)

' Demonstrate that we can modify the loaded message.
' Update the date when the message was composed to the current moment.
mailer.Message.Date = DateTime.Now

mailer.Send()

In this and other samples in this guide, it's assumed MailBee and MailBee.SmtpMail namespaces have been imported and the license key has been set (see Import namespaces and set license key topic for details). msgData must point to the byte array (byte[] in C# and Byte() in VB) which you need to set in your code.


Send e-mail created from file

If you have an e-mail in MIME format (for instance, you previously saved it into a file with MailMessage.SaveMessage(string) method), you can then load it back with MailMessage.LoadMessage(string) method, and send it out.

The data format of the file is exactly the same as the byte array's format used in Send e-mail created from memory topic. Thus, MailMessage.SaveMessage(string) method in fact reads the file in a byte array and then loads the message from that byte array using MailMessage.LoadMessage(byte[]) overload.

Note that you can load any MIME-compliant e-mail file even if it wasn't created by MailBee.NET. MIME-compliant e-mail files usually have .EML extension. The sample below loads the e-mail from .EML file and sends it:

Smtp mailer = new Smtp();

// Use SMTP relay server with authentication.
mailer.SmtpServers.Add("smtp.domain.com", "joe@domain.com", "secret");

// Load the message from .EML file (it must be in MIME RFC2822 format).
mailer.Message.LoadMessage(@"C:\Temp\message.eml");

// Demonstrate that we can modify the loaded message.
// Update the date when the message was composed to the current moment.
mailer.Message.Date = DateTime.Now;

mailer.Send();
Dim mailer As Smtp = New Smtp()

' Use SMTP relay server with authentication.
mailer.SmtpServers.Add("smtp.domain.com", "joe@domain.com", "secret")

' Load the message from .EML file (it must be in MIME RFC2822 format).
mailer.Message.LoadMessage("C:\Temp\message.eml")

' Demonstrate that we can modify the loaded message.
' Update the date when the message was composed to the current moment.
mailer.Message.Date = DateTime.Now

mailer.Send()

Besides of .EML, some programs also use .MSG extension for MIME files. However, .MSG extension is usually associated with MS Office Outlook e-mail messages in binary format.

In case if you indeed would like to read Outlook .MSG files with MailBee.NET, use MailBee.NET Outlook component. It's included in MailBee.NET assembly but it's licensed separately from SMTP component (MailBee.NET Objects bundle includes all the licenses). See Send e-mail created from Outlook .MSG message topic for details.


Send e-mail created from serialized XML

If you have previously saved an e-mail with MailMessage.Serialize(string) method, you can then restore it with MailMessage.Deserialize(string) and send it:

Smtp mailer = new Smtp();

// Use SMTP relay server with authentication.
mailer.SmtpServers.Add("mail.domain.com", "john.doe", "secret");

// Load the message from XML file (which was earlier produced
// with MailMessage.Serialize method call).
mailer.Message.Deserialize(@"C:\Temp\message.xml");

// Demonstrate that we can modify the loaded message.
// Update the date when the message was composed to the current moment.
mailer.Message.Date = DateTime.Now;

mailer.Send();
Dim mailer As Smtp = New Smtp()

' Use SMTP relay server with authentication.
mailer.SmtpServers.Add("mail.domain.com", "john.doe", "secret")

' Load the message from XML file (which was earlier produced
' with MailMessage.Serialize method call).
mailer.Message.Deserialize("C:\Temp\message.xml")

' Demonstrate that we can modify the loaded message.
' Update the date when the message was composed to the current moment.
mailer.Message.Date = DateTime.Now

mailer.Send()

And you can also deserialize from memory (rather than from a file) using MailMessage.Deserialize(XmlTextReader) overload.


Send e-mail created from Outlook .MSG message

If you have .MSG file in Outlook format, you can convert it into MailBee.Mime.MailMessage object and then do with it whatever you want. For instance, you can then modify and send it.

To load .MSG file into MailBee.Mime.MailMessage object, MailBee.NET Outlook license is required (MailBee.NET SMTP license does not include it while the full MailBee.NET Objects license does).

Before you can use Outlook component, import MailBee.Outlook namespace and set MailBee.Global.LicenseKey (unless you already did this to unlock other components).

The self-contained Console sample below imports all the required namespaces, sets the required license key directly in the code (although you could have placed it elsewhere, such as in app.config), loads the e-mail from .MSG file and sends it out:

using System;
using MailBee;
using MailBee.SmtpMail;
using MailBee.Outlook;

class Program
{
    static void Main(string[] args)
    {
        // Set license keys not relying on app.config or registry.
        MailBee.Global.LicenseKey = "MailBee.NET SMTP or Objects key";

        Smtp mailer = new Smtp();

        // Use SMTP relay server with authentication.
        mailer.SmtpServers.Add("smtp.domain.com", "joe@domain.com", "secret");

        // Load the e-mail to be sent from Outlook .MSG file.
        MsgConvert msgReader = new MsgConvert();
        mailer.Message = msgReader.MsgToMailMessage(@"C:\Temp\message.msg");

        // Demonstrate that we can modify the loaded message.
        // Update the date when the message was composed to the current moment.
        mailer.Message.Date = DateTime.Now;

        mailer.Send();
    }
}
Imports System
Imports MailBee
Imports MailBee.SmtpMail
Imports MailBee.Outlook

Module Module1
    Sub Main()
        ' Set license keys not relying on app.config or registry.
        MailBee.Global.LicenseKey = "MailBee.NET SMTP or Objects key"

        Dim mailer As Smtp = New Smtp() 

        ' Use SMTP relay server with authentication.
        mailer.SmtpServers.Add("smtp.domain.com", "joe@domain.com", "secret")

        ' Load the e-mail to be sent from Outlook .MSG file.
        Dim msgReader As MsgConvert = New MsgConvert() 
        mailer.Message = msgReader.MsgToMailMessage("C:\Temp\message.msg")

        ' Demonstrate that we can modify the loaded message.
        ' Update the date when the message was composed to the current moment.
        mailer.Message.Date = DateTime.Now

        mailer.Send()
    End Sub
End Module

If your .MSG data is stored in memory rather than file, you can read it from Stream with MsgConvert.MsgToMailMessage(Stream) method.

You can also extract e-mails from Outlook .PST files. In this case, use PstReader class. Both MsgConvert and PstReader classes are considered the parts of the same component and are covered by the same license.


Send feedback to AfterLogic

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