Category Archives: SMTP

All posts related to the SMTP library

Sending mail with attachments

MIME attachments are represented by Attachment objects, and are managed by use of the MailMessage.Attachments property. The source of attachments can be a file, a MemoryStream, or any other type of .NET Framework Stream. As a result, the ways an attachment can be created is limitless. This topic demonstrates two ways of creating an attachment.

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.Subject = "Test Subject";
   mmMessage.BodyText = "Test Content";
   // Attach file to the message.
   mmMessage.Attachments.Add("myfile.dat");
   // Attach content data from a stream to the message.
   Stream stream = new FileStream("myfile.dat", FileMode.Open);
   // The stream will be automatically closed after adding to the attachment list.
   mmMessage.Attachments.Add(new Attachment(stream, "teststream"));
   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 the message with attachment...");
   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.Subject = "Test Subject"
    mmMessage.BodyText = "Test Content"
    ' Attach file to the message.
    mmMessage.Attachments.Add("myfile.dat")
    ' Attach content data from a stream to the message.
    Dim stream As Stream = New FileStream("myfile.dat", FileMode.Open)
    ' The stream will be automatically closed after adding to the attachment list.
    mmMessage.Attachments.Add(New Attachment(stream, "teststream"))
    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 the message with attachment...")
    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

Creating a new mail message

Introduction to the Non-MIME and MIME messages

Email messages can generally be separated into two broad categories:

  • Non-MIME messages.
  • MIME messages.

Non-MIME messages

A non-MIME message refers to any message that is created in the original format defined in RFC822. These messages are generally made up of simple text and Uuencoded or Yencoded attachments.

MIME messages

MIME (Multipurpose Internet Mail Extensions) was defined to expand upon the original RFC822 message structure’s ability to transport files and the inability to create a complex-multipart message. These types of messages are usually recognizable by the “MIME-VERSION : 1.0″ header line and the MIME-boundary separating the parts of the message.

Since almost all email readers today are MIME compatible, it is recommended to create all messages as MIME messages. Create a non-MIME messages only if it is likely that the mail reader used to receive the message will be unable to read MIME.

The examples below show how to create a simple mail message using the UltimateMail component:

C#:

// Create a new instance of the MailMessage class.
MailMessage msg = new MailMessage();
msg.From.Add("john@somedomain.com");
msg.To.Add("someone@somedomain.com");
msg.Subject = "Hello";
msg.BodyText = "How are you?";

VB.NET:

' Create a new instance of the MailMessage class.
Dim msg As New MailMessage()
msg.From.Add("john@somedomain.com")
msg.To.Add("someone@somedomain.com")
msg.Subject = "Hello"
msg.BodyText = "How are you?"

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

How to use GMAIL Account to send emails

Explicit connection and Implicit connection are two secure methods to connect to a secure SMTP/SMTP/POP3 server. This topic will go in detail the Implicit connection.

Originally, a separate port was assigned to the SSL version of all these three protocols. These were 465 for SMTP/SSL, 993 for SMTP/SSL and 995 for POP3/SSL. Upon connection to this port, an SSL negotiation starts immediately and the control connection is secured. All data connections are also secured implicitly in the same way. This is similar to the approach used by HTTPS.

The code snippet below shows how to connect to the Gmail SMTP server securely using Implicit security mode and send an email using Ultimate Mail component:

C#

// Create a new instance of the SmtpClient class. 
SmtpClient client = new SmtpClient(); 
// Connect to the server. 
client.Connect("smtp.gmail.com", 465, SecurityMode.Implicit); 
// Login to the server. 
client.Authenticate("user", "password"); 
// Create a new mail message. 
MailMessage msg = new MailMessage(); 
msg.Subject = "Test Subject"; 
msg.BodyText = "Content"; 
msg.From = "from@mydomain.com"; 
msg.To = "to@somedomain.com"; 
// And send it. 
client.Send(msg); 
// Close the connection. 
client.Disconnect();

VB.NET

' Create a new instance of the SmtpClient class. 
Dim client As New SmtpClient() 
' Connect to the server. 
client.Connect("smtp.gmail.com", 465, SecurityMode.Implicit) 
' Login to the server. 
client.Authenticate("user", "password") 
' Create a new mail message. 
Dim msg As New MailMessage() 
msg.Subject = "Test Subject" 
msg.BodyText = "Content" 
msg.From = "from@mydomain.com" 
msg.To = "to@somedomain.com" 
' And send it. 
client.Send(msg) 
' Close the connection. 
client.Disconnect()