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")

The developer 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, the developer 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, the developer should 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, the developer should 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 recipients of a message, the developer should use the corresponding proper-ties 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, the developer should use the Subject property of the SMTP object as shown below:

C#

oMailer.Subject = "Test message";

VB.NET

oMailer.Subject = "Test message"

Besides, a mail message may have no subject.

The developer should also specify the body of the message. If the plain text body is preferred, the developer should use the BodyPlainText property:

C#

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

VB.NET

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

Otherwise, if the developer is going to use HTML-formatted body, the BodyHtmlText property should be set:

C#

oMailer.BodyHtmlText = @"<DIV>Test HTML message.</DIV><BR><BR>
		<FONT face=""Arial Black"" color=#0000ff size=5>
		<P align=left><STRONG><U>
		This is a test HTML mes-sage.
		</U></STRONG></P></FONT>
		<hr size=1>
		<a href=""http://www.afterlogic.com"">www.afterlogic.com</a>";

VB.NET

oMailer.BodyHtmlText = "<DIV>Test HTML message.</DIV><BR><BR>" & vbCrLf & _
		"<FONT face=""Arial Black"" color=#0000ff size=5>" & vbCrLf & _
		"<P align=left><STRONG><U>" & vbCrLf & _
		"This is a test HTML mes-sage." & vbCrLf & _
		"</U></STRONG></P></FONT>" & vbCrLf & _
		"<hr size=1>" & vbCrLf & _
		"<a href=""http://www.afterlogic.com"">www.afterlogic.com</a>"

However, the developer can use both of these types of message bodies together. In this case, the settings of the mail client affect, which body is displayed by this mail client.

To attach any file to an e-mail message, the developer should use the AddAttachment method, which just requires the developer to specify the full path to this file. To add the multiple attachments, the developer should repeat calling AddAttachment method as shown below:

C#

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

VB.NET

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

To send a message, the developer should call the Send method of SMTP object. Since this method throws exceptions on any occurred error, the developer 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, please see the corresponding topic of the Troubleshooting section.

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 two specified recipients.

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

C#

using System;
using MailBee;
using MailBee.SmtpMail;

namespace EmailApp
{
    class Class1
    {
        [STAThread]
        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@compay.com> (Company Info)";

            oMailer.Subject = "Test e-mail";

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

            oMailer.BodyHtmlText = @"<DIV>Test HTML message.</DIV><BR><BR>
            <FONT face=""Arial Black"" color=#0000ff size=5>
            <P align=left><STRONG><U>
            This is a test HTML mes-sage.
            </U></STRONG></P></FONT>
            <hr size=1>
            <a href=""http://www.afterlogic.com"">www.afterlogic.com</a>";

            oMailer.AddAttachment(@"C:\annual_reoprt.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.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 = "<DIV>Test HTML message.</DIV><BR><BR>" & vbCrLf & _
            "<FONT face=""Arial Black"" color=#0000ff size=5>" & vbCrLf & _
            "<P align=left><STRONG><U>" & vbCrLf & _
            "This is a test HTML mes-sage." & vbCrLf & _
            "</U></STRONG></P></FONT>" & vbCrLf & _
            "<hr size=1>" & vbCrLf & _
            "<a href=""http://www.afterlogic.com"">www.afterlogic.com</a>"
 
            oMailer.AddAttachment("C:\annual_reoprt.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