EASendMail namespace contains classes that allow you to construct and send email messages. Email message is delivered through either arbitrary SMTP email service or dns lookup without specified SMTP server. The classes in this namespace can be used from ASP.NET or any managed application.
Classes
| Class | Description |
| Attachment | Provides properties and methods for constructing an e-mail attachment. |
| AddressCollection | Collection of MailAddress class inherited from System.Collections.ArrayList |
| Certificate | Provides properties and methods for associating digital certificate with an e-mail address. |
| DnsQueryEx | Provides properties and methods for querying MX records of domain from DNS server. |
| HeaderCollection | Collection of HeaderItem class inherited from System.Collections.ArrayList |
| HeaderItem | Provides properties and methods for constructing an e-mail header. |
| MailAddress | Provides properties and methods for constructing an e-mail address including display name, e-mail address and digital certificate. |
| MimePart | Provides properties and methods for constructing a customized MIME part defined in RFC822. |
| MimePartCollection | Collection of MimePart class inherited from System.Collections.ArrayList |
| SmtpClient | Provides properties and methods for sending messages and testing recipients. |
| SmtpClientAsyncResult | Provides an implementation of System.IAsyncResult for use by BeginSendMail method and BeginTestRecipients method to implement the standard asynchronous method pattern. |
| SmtpMail | Provides properties and methods for constructing an e-mail message. |
| SmtpServer | Provides properties and methods for constructing a smtp server instance. |
| SmtpServerException | Represents errors that is returned by smtp server. |
| SmtpTerminatedException | Represents error that sending email is cancelled by user. |
| ProxyServerException | Represents errors that is returned by proxy server.. |
Enumerations
| Enumeration | Description |
| EncryptionAlgorithmType | Provides enumered values for e-mail encryption algorithm. |
| HeaderEncodingType | Provides enumered values for e-mail header, subject, e-mail friendly name encoding. |
| ImportHtmlBodyOptions | Provides enumered values for options of importing html body. |
| ImportTextBodyOptions | Provides enumered values for options of importing plain text body. |
| MailPriority | Specifies the priority level for the e-mail message. |
| SmtpAuthType | Specifies the smtp user authentication mechanism. |
| SmtpConnectType | Specifies the connection type to smtp server. |
| SocksProxyProtocol | Specifies the protocol (socks4/socks5/http) for proxy server. |
| TransferEncodingType | Provides enumered values for e-mail body text encoding. |
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.
[Visual Basic]
Imports EASendMail
Public Sub SendMail( sFrom As String, _
sTo As String, _
sCc As String, _
sServer As String, _
sUserName As String, _
sPassword As String, _
sSubject As String, _
sBodyText As String, _
bSSLConnection As Boolean )
Dim oMail As SmtpMail = New SmtpMail("TryIt")
Dim oSmtp As SmtpClient = New SmtpClient
'To generate a log file for SMTP transaction, please use
'oSmtp.LogFileName = "c:\smtp.log"
Dim errStr As String = ""
Try
'From is a MailAddress object, in c#, it supports implicit converting from string.
'The syntax is like this: "test@adminsystem.com" or "Tester<test@adminsystem.com>"
'The example code without implicit converting
' 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, in C#, it supports implicit converting from string.
' multiple address are separated with (,;)
' The syntax is like this: "test@adminsystem.com, test1@adminsystem.com"
'The example code without implicit converting
' 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"))
oMail.From = New MailAddress( sFrom )
oMail.To = New AddressCollection(sTo)
oMail.Cc = New AddressCollection(sCc)
oMail.Subject = sSubject
oMail.TextBody = sBodyText
'If the sBodyText contains the html tag, please use
'oMail.HtmlBody = sBodyText
'Add attachment
'oMail.AddAttachment( "c:\test.gif" )
Dim oServer As SmtpServer = New SmtpServer(sServer)
If sUserName.Length > 0 And sPassword.Length > 0 Then
oServer.User = sUserName
oServer.Password = sPassword
End If
If (bSSLConnection) Then
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
End If
oSmtp.SendMail(oServer, oMail)
MessageBox.Show(String.Format("The message was sent to {0} successfully!", _
oSmtp.CurrentSmtpServer.Server))
Catch exp As SmtpTerminatedException
errStr = exp.Message
Catch exp As SmtpServerException
errStr = String.Format("Exception: Server Respond: {0}", exp.ErrorMessage)
Catch exp As System.Net.Sockets.SocketException
errStr = String.Format("Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message)
Catch exp As System.ComponentModel.Win32Exception
errStr = String.Format("Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message)
Catch exp As System.Exception
errStr = String.Format("Exception: Common: {0}", exp.Message)
End Try
If errStr.Length > 0 Then
MessageBox.Show(errStr)
End If
End Sub
[C#]
using System;
using System.Collections;
using EASendMail;
public void SendMail( string sFrom,
string sTo,
string sCc,
string sServer,
string sUserName,
string sPassword,
string sSubject,
string sBodyText,
bool bSSLConnection )
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
//To generate a log file for SMTP transaction, please use
//oSmtp.LogFileName = "c:\\smtp.log";
string err = "";
try
{
//From is a MailAddress object, in c#, it supports implicit converting from string.
//The syntax is like this: "test@adminsystem.com" or "Tester<test@adminsystem.com>"
//The example code without implicit converting
// 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, in C#, it supports implicit converting from string.
// multiple address are separated with (,;)
//The syntax is like this: "test@adminsystem.com, test1@adminsystem.com"
//The example code without implicit converting
// 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"));
oMail.From = sFrom;
oMail.To = sTo;
oMail.Cc = sCc;
oMail.Subject = sSubject;
oMail.TextBody = sBodyText;
//If the sBodyText contains the html tag, please use
//oMail.HtmlBody = sBodyText;
//Add attachment
//oMail.AddAttachment( "c:\\test.gif" );
SmtpServer oServer = new SmtpServer( sServer );
if( sUserName.Length != 0 && sPassword.Length != 0 )
{
oServer.User = sUserName;
oServer.Password = sPassword;
}
if( bSSLConnection )
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
oSmtp.SendMail( oServer, oMail );
MessageBox.Show( String.Format( "The message was sent to {0} successfully!",
oSmtp.CurrentSmtpServer.Server ));
}
catch( SmtpTerminatedException exp )
{
err = exp.Message;
}
catch( SmtpServerException exp )
{
err = String.Format( "Exception: Server Respond: {0}", exp.ErrorMessage );
}
catch( System.Net.Sockets.SocketException exp )
{
err = String.Format( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message );
}
catch( System.ComponentModel.Win32Exception exp )
{
err = String.Format( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message );
}
catch( System.Exception exp )
{
err = String.Format( "Exception: Common: {0}", exp.Message );
}
if( err.Length > 0 )
{
MessageBox.Show( err );
}
}
[C++]
using namespace System;
using namespace System::Collections;
using namespace EASendMail;
System::Void SendMail( System::String *sFrom,
System::String *sTo,
System::String *sCc,
System::String *sServer,
System::String *sUserName,
System::String *sPassword,
System::String *sSubject,
System::String *sBodyText,
bool bSSLConnection )
{
SmtpMail *oMail = new SmtpMail(S"TryIt");
SmtpClient *oSmtp = new SmtpClient();
//To generate a log file for SMTP transaction, please use
//oSmtp->LogFileName = S"c:\\smtp.log";
System::String *err = S"";
try
{
//From is a MailAddress object, in c#, it supports implicit converting from string.
//The syntax is like this: "test@adminsystem.com" or "Tester<test@adminsystem.com>"
//The example code without implicit converting
// oMail->From = new MailAddress( S"Tester", S"test@adminsystem.com" );
// oMail->From = new MailAddress( S"Tester<test@adminsystem.com>" );
// oMail->From = new MailAddress( S"test@adminsystem.com" );
//To, Cc and Bcc is a AddressCollection object, in C#, it supports implicit converting from string.
// multiple address are separated with (,;)
//The syntax is like this: "test@adminsystem.com, test1@adminsystem.com"
//The example code without implicit converting
// oMail->To = new AddressCollection( S"test1@adminsystem.com, test2@adminsystem.com" );
// oMail->To = new AddressCollection( S"Tester1<test@adminsystem.com>, Tester2<test2@adminsystem.com>");
// You can add more recipient by Add method
// oMail->>To->Add( new MailAddress( S"tester", S"test@adminsystem.com"));
oMail->From = new EASendMail::MailAddress(sFrom);
oMail->To = new EASendMail::AddressCollection(sTo);
oMail->Cc = new EASendMail::AddressCollection(sCc);
oMail->Subject = sSubject;
oMail->TextBody = sBodyText;
//If the sBodyText contains the html tag, please use
//oMail->HtmlBody = sBodyText;
//Add attachment
//oMail->AddAttachment( S"c:\\test.gif" );
SmtpServer *oServer = new SmtpServer( sServer );
if( sUserName->Length != 0 && sPassword->Length != 0 )
{
oServer->User = sUserName;
oServer->Password = sPassword;
}
if( bSSLConnection )
oServer->ConnectType = SmtpConnectType::ConnectSSLAuto;
oSmtp->SendMail( oServer, oMail );
MessageBox::Show( String::Format( S"The message was sent to {0} successfully!",
oSmtp->CurrentSmtpServer->Server ));
}
catch( EASendMail::SmtpTerminatedException *exp )
{
err = exp->Message;
}
catch( EASendMail::SmtpServerException *exp )
{
err = String::Format( S"Exception: Server Respond: {0}", exp->ErrorMessage );
}
catch( System::Net::Sockets::SocketException *exp )
{
err = String::Format( S"Exception: Networking Error: {0} {1}", exp->ErrorCode.ToString(S"d"), exp->Message );
}
catch( System::ComponentModel::Win32Exception *exp )
{
err = String::Format( S"Exception: System Error: {0} {1}", exp->ErrorCode.ToString(S"d"), exp->Message );
}
catch( System::Exception *exp )
{
err = String::Format( S"Exception: Common: {0}", exp->Message );
}
if( err->Length > 0 )
{
MessageBox::Show( err );
}
}
[JScript.NET]
public function SendMail( sFrom:String,
sTo:String,
sCc:String,
sServer:String,
sUserName:String,
sPassword:String,
sSubject:String,
sBodyText:String,
bSSLConnection:Boolean )
{
var oMail:SmtpMail = new SmtpMail("TryIt");
var oSmtp:SmtpClient = new SmtpClient();
//To generate a log file for SMTP transaction, please use
//oSmtp.LogFileName = "c:\\smtp.log";
var err:String = "";
try
{
//From is a MailAddress object, in c#, it supports implicit converting from string.
//The syntax is like this: "test@adminsystem.com" or "Tester<test@adminsystem.com>"
//The example code without implicit converting
// 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, in C#, it supports implicit converting from string.
// multiple address are separated with (,;)
//The syntax is like this: "test@adminsystem.com, test1@adminsystem.com"
//The example code without implicit converting
// 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"));
oMail.From = new MailAddress(sFrom);
oMail.Subject = sSubject
oMail.To = new AddressCollection(sTo);
oMail.Cc = new AddressCollection(sCc);
oMail.TextBody = sBodyText;
//If the sBodyText contains the html tag, please use
//oMail.HtmlBody = sBodyText;
//Add attachment
//oMail.AddAttachment( "c:\\test.gif" );
var oServer:SmtpServer = new SmtpServer(sServer);
if( sUserName.Length != 0 && sPassword.Length != 0 )
{
oServer.User = sUserName;
oServer.Password = sPassword;
}
if( bSSLConnection )
{
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
}
oSmtp.SendMail( oServer, oMail );
MessageBox.Show( String.Format("The message was sent to {0} successfully!", oServer.Server ));
}
catch( exp:SmtpTerminatedException )
{
err = exp.Message;
}
catch( exp:SmtpServerException )
{
err = String.Format( "Exception: Server Respond: {0}", exp.ErrorMessage );
}
catch( exp:System.Net.Sockets.SocketException )
{
err = String.Format( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message );
}
catch( exp:System.ComponentModel.Win32Exception )
{
err = String.Format( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message );
}
catch( exp:System.Exception )
{
err = String.Format( "Exception: Common: {0}", exp.Message );
}
if( err.Length > 0 )
{
MesageBox.Show( err );
}
}