MailBee. NET Objects Tutorials

Sending a simple text and HTML e-mail

The simplest way to quickly send an e-mail message in a single line of code is to use the QuickSend method of SMTP object. In this case the developer does not even need to create a new instance of SMTP object. Only the basic properties (such as From, To, Subject, Body, etc) must be specified:

C#

MailBee.SmtpMail.Smtp.QuickSend("from@me.com", "to@you.com", "Subject", "Message Body");

VB.NET

MailBee.SmtpMail.Smtp.QuickSend("from@me.com", "to@you.com", "Subject", "Message Body")

You can also send an e-mail message with an attachment by using QuickSend method as follows:

C#

MailBee.SmtpMail.Smtp.QuickSend("From Me <me@domain.com> (Company Info)",
                                "To you <you@company.com>",
                                "Subject", "Plain text body",
                                "<html>HTML-formatted body</html>",
                                null, @"C:\My Documents\report.doc");

VB.NET

MailBee.SmtpMail.Smtp.QuickSend("From Me <me@domain.com> (Company Info)", _
                                "To you <you@company.com>", _
                                "Subject", "Plain text body", _
                                "&laquo;html&raquo;HTML-formatted body&laquo;/html&raquo;", _
                                Nothing, "C:\My Documents\report.doc")

Otherwise, you can directly use an instance of SMTP object which provides a wide range of methods and properties for tuning the process of a message sending. A new instance of the SMTP object in C# application can be created as follows:

C#

Smtp oMailer = new Smtp();

VB.NET

Dim oMailer As New Smtp()

If the SMTP server does not require any authentication, the specified host name or the equal IP address is enough to connect to this SMTP server as follows:

C#

oMailer.SmtpServers.Add("smtp.domain.com");

VB.NET

oMailer.SmtpServers.Add("smtp.domain.com")

or

C#

oMailer.SmtpServers.Add("127.0.0.1");

VB.NET

oMailer.SmtpServers.Add("127.0.0.1")

On the other hand, if the SMTP server requires authentication, specify the name of the account on this server and the corresponding password:

C#

oMailer.SmtpServers.Add("smtp.domain.com", "login", "password");

VB.NET

oMailer.SmtpServers.Add("smtp.domain.com", "login", "password")

or

C#

oMailer.SmtpServers.Add("127.0.0.1", "login", "password");

VB.NET

oMailer.SmtpServers.Add("127.0.0.1", "login", "password")

Then, specify the e-mail address of the message sender as shown below:

C#

oMailer.From.AsString = "Dan Brown <dan@domain.com> (Company Info)";

VB.NET

oMailer.From.AsString = "Dan Brown <dan@domain.com> (Company Info)"

or

C#

oMailer.From.AsString = "Dan Brown <dan@domain.com>";

VB.NET

oMailer.From.AsString = "Dan Brown <dan@domain.com>"

or

C#

oMailer.From.AsString = "dan@domain.com";

VB.NET

oMailer.From.AsString = "dan@domain.com"

To add To, CC, BCC, or Reply-To to the message, use the corresponding properties of SMTP object as shown below:

C#

oMailer.To.AsString = "Bill Smith <b.smith@domain.com> (Remarks), Kathy@mail.com ";
oMailer.Cc.AsString = "Joe Black <j.black@domain.com>, Joseph <joseph@domain.com>";
oMailer.Bcc.AsString = "t.jay@domain.com, s.connor@domain.com";
oMailer.ReplyTo.AsString = "john@domain.com, Barbara Jones <b.jones@mail.com>";

VB.NET

oMailer.To.AsString = "Bill Smith <b.smith@domain.com> (Remarks), Kathy@mail.com "
oMailer.Cc.AsString = "Joe Black <j.black@domain.com>, Joseph <joseph@domain.com>"
oMailer.Bcc.AsString = "t.jay@domain.com, s.connor@domain.com"
oMailer.ReplyTo.AsString = "john@domain.com, Barbara Jones <b.jones@mail.com>"

To specify the subject of a mail message, use Subject property of the SMTP object as shown below:

C#

oMailer.Subject = "Test message";

VB.NET

oMailer.Subject = "Test message"

Now let's set the body of the message. If the plain text body is preferred, use BodyPlainText property:

C#

oMailer.BodyPlainText = "This is a test e-mail message.";

VB.NET

oMailer.BodyPlainText = "This is a test e-mail message."

To set HTML body, use BodyHtmlText property:

C#

oMailer.BodyHtmlText = @"<html><body><a href=""https://afterlogic.com"">afterlogic.com</a></body></html>";

VB.NET

oMailer.BodyHtmlText = "<html><body><a href=""https://afterlogic.com"">afterlogic.com</a></body></html>"

You can also ask MailBee to automatically create plain-text version of HTML message (the sample in the end of this article does that).

To attach a file to an e-mail message, use AddAttachment method. To add the multiple attachments, make multiple calls to AddAttachment method as shown below:

C#

oMailer.AddAttachment(@"C:\Temp\annual_report.xls");
oMailer.AddAttachment(@"C:\Temp\deposits.doc");

VB.NET

oMailer.AddAttachment("C:\Temp\annual_report.xls")
oMailer.AddAttachment("C:\Temp\deposits.doc")

To send a message, call Send method of SMTP object. Since this method throws exceptions on any occurred error, you can handle these exceptions as follows:

C#

try
{
    oMailer.Send();
    Console.WriteLine("The message has been successfully sent.");
}
catch (MailBeeSmtpRefusedRecipientException e)
{
    Console.WriteLine("The following recipient was refused by SMTP server: " + e.RefusedRecipientEmail);
}

VB.NET

Try
    oMailer.Send()
    Console.WriteLine("The message has been successfully sent.")
Catch e As MailBeeSmtpRefusedRecipientException
    Console.WriteLine("The following recipient was refused by SMTP server: " + e.RefusedRecipientEmail)
End Try

Should you experience any problems with the following sample, check Troubleshooting page.

Sample Code:

Summary: The following example creates a new message having both the plain text and HTML-formatted bodies and attaches .xls document to this message. Then the message is sent to multiple recipients.

Before using MailBee.NET Objects, make sure the library is unlocked (see "Sales, Licensing, and Support" and "Using MailBee.NET Objects in Your Projects" sections).

C#

using System;
using MailBee;
using MailBee.Mime;
using MailBee.SmtpMail;

namespace EmailApp
{
    class Class1
    {
        static void Main(string[] args)
        {
            Smtp oMailer = new Smtp();

            oMailer.From.AsString = "John Doe <j.doe@domain.com> (Company Info)";

            oMailer.To.AsString = "Bill Smith <b.smith@domain.com>, Kathy Ritchie <kr@company.com> (Company Info)";

            oMailer.Subject = "Test e-mail";

            oMailer.BodyPlainText = "This is a test e-mail message.";

            oMailer.BodyHtmlText = @"<html><body><a href=""https://afterlogic.com"">afterlogic.com</a></body></html>";
            
            oMailer.Message.Builder.HtmlToPlainMode = HtmlToPlainAutoConvert.IfHtml;

            oMailer.AddAttachment(@"C:\Temp\annual_report.xls");

            oMailer.SmtpServers.Add("127.0.0.1", "login", "password");

            oMailer.SmtpServers[0].AllowRefusedRecipients = false;

            try
            {
                oMailer.Send();
                Console.WriteLine("The message has been successfully sent.");
            }
            catch (MailBeeSmtpRefusedRecipientException e)
            {
                Console.WriteLine("The following recipient was refused by SMTP server: " +
                e.RefusedRecipientEmail);
            }
        }
    }
}

VB.NET

Imports System
Imports MailBee
Imports MailBee.Mime
Imports MailBee.SmtpMail
 
Namespace EmailApp
    Class Class1
        Shared Sub Main(ByVal args() As String)
            Dim oMailer As New Smtp() 
 
            oMailer.From.AsString = "John Doe <j.doe@domain.com> (Company Info)"
 
            oMailer.To.AsString = "Bill Smith <b.smith@domain.com>, Kathy Ritchie <kr@compay.com> (Company Info)"
 
            oMailer.Subject = "Test e-mail"
 
            oMailer.BodyPlainText = "This is a test e-mail message."
 
            oMailer.BodyHtmlText = <html><body><a href=""https://afterlogic.com"">afterlogic.com</a></body></html>"
            
            oMailer.Message.Builder.HtmlToPlainMode = HtmlToPlainAutoConvert.IfHtml
 
            oMailer.AddAttachment("C:\Temp\annual_report.xls")
 
            oMailer.SmtpServers.Add("127.0.0.1", "login", "password")
 
            oMailer.SmtpServers(0).AllowRefusedRecipients = False
 
            Try
                oMailer.Send()
                Console.WriteLine("The message has been successfully sent.")
            Catch e As MailBeeSmtpRefusedRecipientException
                Console.WriteLine("The following recipient was refused by SMTP server: " &
                e.RefusedRecipientEmail)
            End Try
        End Sub
    End Class
End Namespace