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:
MailBee.SmtpMail.Smtp.QuickSend("from@me.com", "to@you.com", "Subject", "Message Body");
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:
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");
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»", _
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:
Smtp oMailer = new Smtp();
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:
oMailer.SmtpServers.Add("smtp.domain.com");
oMailer.SmtpServers.Add("smtp.domain.com")
or
oMailer.SmtpServers.Add("127.0.0.1");
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:
oMailer.SmtpServers.Add("smtp.domain.com", "login", "password");
oMailer.SmtpServers.Add("smtp.domain.com", "login", "password")
or
oMailer.SmtpServers.Add("127.0.0.1", "login", "password");
oMailer.SmtpServers.Add("127.0.0.1", "login", "password")
Then, specify the e-mail address of the message sender as shown below:
oMailer.From.AsString = "Dan Brown <dan@domain.com> (Company Info)";
oMailer.From.AsString = "Dan Brown <dan@domain.com> (Company Info)"
or
oMailer.From.AsString = "Dan Brown <dan@domain.com>";
oMailer.From.AsString = "Dan Brown <dan@domain.com>"
or
oMailer.From.AsString = "dan@domain.com";
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:
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>";
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:
oMailer.Subject = "Test message";
oMailer.Subject = "Test message"
Now let's set the body of the message. If the plain text body is preferred, use BodyPlainText property:
oMailer.BodyPlainText = "This is a test e-mail message.";
oMailer.BodyPlainText = "This is a test e-mail message."
To set HTML body, use BodyHtmlText property:
oMailer.BodyHtmlText = @"<html><body><a href=""https://afterlogic.com"">afterlogic.com</a></body></html>";
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:
oMailer.AddAttachment(@"C:\Temp\annual_report.xls");
oMailer.AddAttachment(@"C:\Temp\deposits.doc");
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:
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);
}
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).
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);
}
}
}
}
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