Sending Mail to Multiple Recipients

This topic illustrates how to compose an email message and send it to multiple recipients with a single line of code. ComponentPro UltimateMail makes sending to multiple recipients easy to manage, by representing these types as collections of the MailMessage object.

If you are using the component to send emails through Gmail SMTP, you need to set the server name to smtp.gmail.com, port to 465, and security mode to Implicit. The following image shows you the configuration in the SMTP Send Mail sample project:

Ultimate SMTP Send Mail

Using Ultimate Mail Library to send mails:

First, you need to connect to the server with a specified port and security mode, authenticate the user if required and call the Send method to send the message to the recipients. Before using the SmtpClient class, you need to add using directive for the namespace ComponentSoft.Net. The following example takes advantage of the easy-to-use features of the library.

C#

const string serverName = “myserver”;
const string user = name@domain.com;
const string password = “mytestpassword”;
const int port = 465;
const SecurityMode securityMode = SecurityMode.Implicit;
SmtpClient client =
 new SmtpClient();
try
{
MailMessage mmMessage = new MailMessage();
mmMessage.From.Add(
from@thedomain.com);
mmMessage.To.Add(
name@domain.com);
mmMessage.To.Add(
someone@domain.com);
mmMessage.Cc.Add(
someone2@domain.com);
mmMessage.Bcc.Add(
someone3@domain.com);
mmMessage.Subject =
 “Test Subject”;
mmMessage.BodyText =
 “Test Content”;
Console.WriteLine(
“Connecting SMTP server: {0}:{1}…”, serverName, port);
// Connect to the server. (i.e. componentsoft.net)
client.Connect(serverName, port, securityMode);
// Login to the server.
Console.WriteLine(“Logging in as {0}…”, user);
client.Authenticate(user, password);
Console.WriteLine(
“Sending mail message…”);
client.Send(mmMessage);
Console.WriteLine(
“Message sent…”);
// Disconnect.
Console.WriteLine(“Disconnecting…”);
client.Disconnect();
}
catch (SmtpException smtpExc)
{
MessageBox.Show(
string.Format(“An SMTP error occurred: {0}, ErrorStatus: {1}”, smtpExc.Message, smtpExc.Status));
}
catch (Exception exc)
{
MessageBox.Show(
string.Format(“An error occurred: {0}”, exc.Message));
}

VB.NET

Const serverName As String = “myserver”
Const user As String = “name@domain.com”
Const password As String = “mytestpassword”
Const port As Integer = 465
Const securityMode As SecurityMode = securityMode.Implicit
Dim client As New SmtpClient()
Try
Dim mmMessage As New MailMessage()
mmMessage.From.Add(“from@thedomain.com”)
mmMessage.To.Add(“name@domain.com”)
mmMessage.To.Add(“someone@domain.com”)
mmMessage.Cc.Add(“someone2@domain.com”)
mmMessage.Bcc.Add(“someone3@domain.com”)
mmMessage.Subject = “Test Subject”
mmMessage.BodyText = “Test Content”
Console.WriteLine(“Connecting SMTP server: {0}:{1}…”, serverName, port)
‘ Connect to the server.
client.Connect(serverName, port, securityMode)
‘ Login to the server.
Console.WriteLine(“Logging in as {0}…”, user)
client.Authenticate(user, password)
Console.WriteLine(“Sending mail message…”)
client.Send(mmMessage)
Console.WriteLine(“Message sent…”)
‘ Disconnect.
Console.WriteLine(“Disconnecting…”)
client.Disconnect()
Catch smtpExc As SmtpException
MessageBox.Show(String.Format(“An SMTP error occurred: {0}, ErrorStatus: {1}”, smtpExc.Message, smtpExc.Status))
Catch exc As Exception
MessageBox.Show(String.Format(“An error occurred: {0}”, exc.Message))
End Try