SmtpMail Class


Provides properties and methods for create an e-mail message.

System.Object
    EASendMail.SmtpMail

[Visual Basic]
Public Class SmtpMail
[C#]
public class SmtpMail
[C++]
public __gc class SmtpMail
[JScript]
public class SmtpMail

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Public Constructors

SmtpMail Constructor Initializes a new instance of the SmtpMail class.

Public Properties

AutoCalendar Specifies the e-mail generates text/calendar body with ics attachment automatically.
AutoTextBody Specifies the e-mail generates alternative text body for html body automatically.
Bcc Gets or sets a AddressCollection of e-mail addresses that receive a blind carbon copy (BCC) of the e-mail message.
Cc Gets or sets a AddressCollection of e-mail addresses that receive a carbon copy (CC) of the e-mail message.
Charset Gets or sets the charset of the e-mail message.
Date Gets or sets the date time of the e-mail message.
DeliveryNotification Gets or sets the delivery notifications for this e-mail message.
EncodedContent Gets the encoded email content.
EncryptionAlgorithm Gets or sets the algorithm for email encryption.
From Gets or sets the e-mail address of the original sender.
HeaderEncoding Gets or sets the encoding for subject and friendly name in From, To, Cc.
Headers Gets the HeaderCollection for headers of the e-mail message.
HtmlBody Gets or sets the html body of the e-mail message.
Mailer Gets or sets the "X-Mailer" of the e-mail message.
MessageID Gets the Message-ID of the e-mail message.
MimeParts Gets the MimePartCollection of the e-mail message.
Priority Gets or sets the priority of the e-mail message.
ReadReceipt Sets the read receipt request in the email.
Recipients Gets a AddressCollection for all e-mail receivers (To, Cc, Bcc).
ReplyTo Gets or sets the e-mail address of the reply address.
ReturnPath Gets or sets the e-mail address of the delivery report address.
Sender Gets or sets the e-mail address of the sender.
SignatureHashAlgorithm Gets or sets the hash algorithm for email digital signature.
Subject Gets or sets the subject of the e-mail message.
TextBody Gets or sets the text body of the e-mail message.
To Gets or sets a AddressCollection of recipient e-mail addresses.
TransferEncoding Gets or sets encoding for the body text of the e-mail message.

Public Methods

AddAttachment Attaches a file to the e-mail message.
ClearAttachments Clears all the file attachments of the e-mail message.
LoadMessage Loads a RFC822 message ( *.eml) file to current SmtpMail instance.
ImportHtml Imports html source from string to html body of the e-mail message.
ImportHtmlBody Imports html source from file or remote website to html body of the e-mail message.
ImportTextBody Imports text source from file or remote website to text body of the e-mail message.
RemoveAttachment Removes file attachment from the e-mail message by file name.
Reset Resets the From, To, Cc, Bcc, Subject, TextBody, HtmlBody, Message-ID and Date of the e-mail message.
SaveAs Saves the e-mail message to a file (*.eml file).

Example

[Visual Basic, C#, C++, JScript.NET] The following example demonstrates how to send email with EASendMail SMTP Component, but it doesn't demonstrates the events usage. To get the full samples of EASendMail, please refer to Samples section.

[VB - Send Email Example]

Imports EASendMail

Public Sub SendMail(sender As String,
    toRecipients As String,
    ccRecipients As String,
    subject As String,
    bodyText As String,
    server As String,
    user As String,
    password As String,
    useSsl As Boolean)

    Try
        ' From is a MailAddress object, it also supports implicit converting from string directly. 
        ' The syntax is like this: "test@adminsystem.com" or "Tester <test@adminsystem.com>"

        ' oMail.From = New MailAddress("Tester", "test@adminsystem.com") 
        ' oMail.From = New MailAddress("Tester <test@adminsystem.com>") 
        ' oMail.From = New MailAddress("test@adminsystem.com") 

        ' To, Cc and Bcc is a AddressCollection object, it also supports implicit converting from string directly. 
        ' multiple addresses should be separated with (,;) 
        ' The syntax is like this: "test@adminsystem.com, test1@adminsystem.com" 

        ' oMail.To = New AddressCollection("test1@adminsystem.com, test2@adminsystem.com") 
        ' oMail.To = New AddressCollection("Tester1 <test@adminsystem.com>, Tester2 <test2@adminsystem.com>")

        ' You can add more recipient by Add method 
        ' oMail.To.Add(New MailAddress("tester", "test@adminsystem.com"))

        Dim oMail As SmtpMail = New SmtpMail("TryIt")

        oMail.From = New MailAddress(sender)
        oMail.To = New AddressCollection(toRecipients)
        oMail.Cc = New AddressCollection(ccRecipients)

        oMail.Subject = subject
        ' If bodyText contains html tags, please use 
        ' oMail.HtmlBody = bodyText 
        oMail.TextBody = bodyText

        ' Add attachment 
        ' oMail.AddAttachment("c:\test.gif")

        ' Set server address
        Dim oServer As SmtpServer = New SmtpServer(server)
        If user.Length > 0 And password.Length > 0 Then
            ' Set user/password for ESMTP authentication
            oServer.User = user
            oServer.Password = password
        End If

        ' Most mordern SMTP servers require SSL/TLS connection now.
        ' ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
        oServer.ConnectType = If(useSsl, SmtpConnectType.ConnectSSLAuto, SmtpConnectType.ConnectTryTLS)

        Dim oSmtp As SmtpClient = New SmtpClient()

        ' To generate a log file for SMTP transaction, please use 
        ' oSmtp.LogFileName = "c:\smtp.txt"

        oSmtp.SendMail(oServer, oMail)

        Console.WriteLine("The message has been submitted to server successfully!")
    Catch ex As Exception
        Console.WriteLine("Exception: {0}", ex.Message)
    End Try

End Sub


[C# - Send Email Example] using System; using System.Collections; using EASendMail; public static void SendMail(string sender, string toRecipients, string ccRecipients, string subject, string bodyText, string server, string user, string password, bool useSsl) { try { // From is a MailAddress object, it also supports implicit converting from string. // The syntax is like this: "test@adminsystem.com" or "Tester <test@adminsystem.com>" // oMail.From = new MailAddress("Tester", "test@adminsystem.com"); // oMail.From = new MailAddress("Tester <test@adminsystem.com>"); // oMail.From = new MailAddress("test@adminsystem.com"); // To, Cc and Bcc is a AddressCollection object, it also supports implicit converting from string. // multiple addresses should be separated with (,;) // The syntax is like this: "test@adminsystem.com, test1@adminsystem.com" // oMail.To = new AddressCollection("test1@adminsystem.com, test2@adminsystem.com"); // oMail.To = new AddressCollection("Tester1<test@adminsystem.com>, Tester2<test2@adminsystem.com>"); // You can add more recipient by Add method // oMail.To.Add( new MailAddress( "tester", "test@adminsystem.com")); SmtpMail oMail = new SmtpMail("TryIt"); oMail.From = sender; oMail.To = toRecipients; oMail.Cc = ccRecipients; oMail.Subject = subject; // If bodyText contains the html tags, please use // oMail.HtmlBody = bodyText; oMail.TextBody = bodyText; // Add attachment // oMail.AddAttachment("c:\\test.gif"); // Set server address SmtpServer oServer = new SmtpServer(server); if (user.Length != 0 && password.Length != 0) { // Set user/password for ESMTP authentication oServer.User = user; oServer.Password = password; } // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = (useSsl) ? SmtpConnectType.ConnectSSLAuto : SmtpConnectType.ConnectTryTLS; SmtpClient oSmtp = new SmtpClient(); // To generate a log file for SMTP transaction, please use // oSmtp.LogFileName = "c:\\smtp.txt"; oSmtp.SendMail(oServer, oMail); Console.WriteLine("The message has been submitted to server successfully!"); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } }
[C++/CLI - Send Email Example] using namespace System; using namespace System::Collections; using namespace EASendMail; static void SendMail(String ^sender, String ^toRecipients, String ^ccRecipients, String ^subject, String ^bodyText, String ^server, String ^user, String ^password, bool useSsl) { try { // From is a MailAddress object, it also supports implicit converting from string. // The syntax is like this: "test@adminsystem.com" or "Tester <test@adminsystem.com>" // oMail->From = gcnew MailAddress("Tester", "test@adminsystem.com"); // oMail->From = gcnew MailAddress("Tester <test@adminsystem.com>"); // oMail->From = gcnew MailAddress("test@adminsystem.com"); //To, Cc and Bcc is a AddressCollection object, it also supports implicit converting from string. // multiple address should be separated with (,;) //The syntax is like this: "test@adminsystem.com, test1@adminsystem.com" // oMail->To = gcnew AddressCollection("test1@adminsystem.com, test2@adminsystem.com"); // oMail->To = gcnew AddressCollection("Tester1 <test@adminsystem.com>, Tester2 <test2@adminsystem.com>"); // You can add more recipient by Add method // oMail->To->Add(gcnew MailAddress("tester", "test@adminsystem.com")); SmtpMail ^oMail = gcnew SmtpMail("TryIt"); oMail->From = gcnew EASendMail::MailAddress(sender); oMail->To = gcnew EASendMail::AddressCollection(toRecipients); oMail->Cc = gcnew EASendMail::AddressCollection(ccRecipients); oMail->Subject = subject; // If bodyText contains html tags, please use // oMail->HtmlBody = bodyText; oMail->TextBody = bodyText; // Add attachment // oMail->AddAttachment("c:\\test.gif"); // Set server address SmtpServer ^oServer = gcnew SmtpServer(server); if (user->Length != 0 && password->Length != 0) { // Set user/password for ESMTP authentication oServer->User = user; oServer->Password = password; } // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer->ConnectType = (useSsl) ? SmtpConnectType::ConnectSSLAuto : SmtpConnectType::ConnectTryTLS; SmtpClient ^oSmtp = gcnew SmtpClient(); // To generate a log file for SMTP transaction, please use // oSmtp->LogFileName = "c:\\smtp.txt"; oSmtp->SendMail(oServer, oMail); Console::WriteLine("The message has been submitted to server successfully!"); } catch (Exception ^exp) { Console::WriteLine("Exception: {0}", exp->Message); } }
[JScript.NET - Send Email Example] public function SendMail(sender:String, toRecipients:String, ccRecipients:String, subject:String, bodyText:String, server:String, user:String, password:String, useSsl:Boolean) { try { // From is a MailAddress object, it also supports implicit converting from string. // The syntax is like this: "test@adminsystem.com" or "Tester <test@adminsystem.com>" // oMail.From = new MailAddress("Tester", "test@adminsystem.com"); // oMail.From = new MailAddress("Tester<test@adminsystem.com>"); // oMail.From = new MailAddress("test@adminsystem.com"); // To, Cc and Bcc is a AddressCollection object, it also supports implicit converting from string. // multiple addresses should be separated with (,;) // The syntax is like this: "test@adminsystem.com, test1@adminsystem.com" // oMail.To = new AddressCollection("test1@adminsystem.com, test2@adminsystem.com"); // oMail.To = new AddressCollection("Tester1 <test@adminsystem.com>, Tester2 <test2@adminsystem.com>"); // You can add more recipient by Add method // oMail.To.Add(new MailAddress("tester","test@adminsystem.com")); var oMail:SmtpMail = new SmtpMail("TryIt"); oMail.From = new MailAddress(sender); oMail.To = new AddressCollection(toRecipients); oMail.Cc = new AddressCollection(ccRecipients); oMail.Subject = subject; // If bodyText contains html tags, please use // oMail.HtmlBody = bodyText; oMail.TextBody = bodyText; // Add attachment // oMail.AddAttachment("c:\\test.gif"); // Set server address var oServer:SmtpServer = new SmtpServer(server); if(user.Length != 0 && password.Length != 0) { // Set user/password for ESMTP authentication oServer.User = user; oServer.Password = password; } // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = (useSsl)? SmtpConnectType.ConnectSSLAuto : SmtpConnectType.ConnectTryTLS; var oSmtp:SmtpClient = new SmtpClient(); // To generate a log file for SMTP transaction, please use // oSmtp.LogFileName = "c:\\smtp.txt"; oSmtp.SendMail(oServer, oMail); Console.WriteLine("The message has been submitted to server successfully!"); } catch(exp:System.Exception) { Console.WriteLine("Exception: {0}", exp.Message); } }

See Also

Using EASendMail SMTP .NET Component
User Authentication and SSL Connection
From, ReplyTo, Sender and Return-Path
Digital Signature and E-mail Encryption
DomainKeys and DKIM Signature
Send E-mail Directly (Simulating SMTP server)
Work with EASendMail Service (Email Queuing)
Bulk Email Sender Guidelines
Process Bounced Email (Non-Delivery Report) and Email Tracking
EASendMail .NET Namespace References
EASendMail SMTP Component Samples

Online Tutorials

Send Email in VB 6.0 - Tutorial
Send Email in C# - Tutorial
Send Email in VB.NET - Tutorial
Send Email in Visual C++ - Tutorial
Send Email in Managed C++/CLI - Tutorial
Send Email in Delphi - Tutorial
Send Email in MS SQL stored procedure - Tutorial