EASendMail Service is a light and fast email delivery service which works with EASendMail SMTP .Net Component / ANSMTP SMTP Component to enable your application to send mass emails in background service. Along with its ability to picking recipients from database in background and sending email in specified datetime, it eases your task in developing featured email application such as newsletter application. We strongly recommend you to use EASendMail Service with your ASP.NET/Web Application.
To work with EASendMail Service, please download EASendMail Service and install it on your server. If you are using web hosting service and you don't have permission to install service on that server, EASendMail service is not suitable for you.
In most newsletter application, body text usually begins with "Dear [name] ... To unsubscribe this newsletter, please ... [email address]". To approach this body text, email application has to send email to every recipient one by one. EASendMail service provides an advanced technology (variables replacing) to process those emails. To learn more about the variables in EASendMail service, please click here.
Important Notice: if your web application is hosted by ISP and you don't have the permission to install EASendMail Service, then please use BatchSendMail method to send bulk emails in asp.net.
Example
[Visual Basic, C#] The following example demonstrates how to send email with EASendMail SMTP Component. 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, _
sSubject As String )
Dim oMail As SmtpMail = New SmtpMail("TryIt")
Dim oSmtp As SmtpClient = New SmtpClient
Dim errStr As String = ""
Try
oMail.From = New MailAddress( sFrom )
'Please separate multiple addresses by comma(,)
oMail.To = New AddressCollection(sTo)
'To avoid too many email addresses appear in To header, using the following code only
'display the current recipient
oMail.Headers.ReplaceHeader( "To", """{$var_rcptname}"" <{$var_rcptaddr}>" )
oMail.Headers.ReplaceHeader( "X-Rcpt-To", new AddressCollection( txtTo.Text ).ToEncodedString( HeaderEncodingType.EncodingAuto, charset ))
oMail.Subject = sSubject
oMail.TextBody = "Dear {$var_rcptname}, your email address is {$var_rcptaddr}"
'if you want EASendMail service to send the email after 10 minutes, use the following code.
'oMail.Date = System.DateTime.Now.AddMinutes( 10 )
oSmtp.SendMailToQueue( null, oMail)
MessageBox.Show( "The message was sent to EASendMail Service successfully!" )
Catch exp As System.Exception
errStr = String.Format("Exception: Common: {0}", exp.Message)
errStr += "Please make sure you installed EASendMail Service on the server!"
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 sSubject )
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
string err = "";
try
{
oMail.From = sFrom;
//Please separate multiple addresses by comma(,)
oMail.To = sTo;
//To avoid too many email addresses appear in To header, using the following code only
//display the current recipient
oMail.Headers.ReplaceHeader( "To", "\"{$var_rcptname}\" <{$var_rcptaddr}>" );
oMail.Headers.ReplaceHeader( "X-Rcpt-To", new AddressCollection( txtTo.Text ).ToEncodedString( HeaderEncodingType.EncodingAuto, charset ));
oMail.Subject = sSubject;
oMail.TextBody = "Dear {$var_rcptname}, your email address is {$var_rcptaddr}";
// if you want EASendMail service to send the email after 10 minutes, use the following code.
//oMail.Date = System.DateTime.Now.AddMinutes( 10 );
oSmtp.SendMailToQueue( null, oMail );
MessageBox.Show( "The message was sent to EASendMail Service successfully!" );
}
catch( System.Exception exp )
{
err = String.Format( "Exception: Common: {0}", exp.Message );
err += "Please make sure you installed EASendMail Service on the server!";
}
if( err.Length > 0 )
{
MessageBox.Show( err );
}
}
No matter how many recipients (even thousands of email addresses) you specify in above sample, EASendMail SMTP Component spends very short time to submit the email to EASendMail Service (you will be impressed by its performance), and the emails will be delivered in background. {$var_rcptname} and {$var_rcptaddr} will be replaced by the service automatically.
Another typical usage scenario is that most newsletter applications get recipient name and address from database. To simplify the task of developer, EASendMail provides a very powerful way to select recipients from database automatically.
Example
[C#] The following sample demonstrates how to select recipient from database by EASendMail service. To get the full samples of EASendMail, please refer to Samples section.
[C#]
SmtpMail oMail = new SmtpMail("TryIt");
oMail.From = "test@adminsytem.com";
oMail.Subject = "test subject";
// if you want EASendMail service to send the email after 10 minutes, use the following code.
//oMail.Date = System.DateTime.Now.AddMinutes( 10 );
// For more connection string
// MS SQL server
//"Driver={SQL Server};Server=localhost;Database=pubs;Uid=sa;Pwd=asdasd;"
// MS Access
//"Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;"
// ORACLE
//"Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=Username;Pwd=asdasd;"
// MySQL
//"DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=myDatabase;USER=myUsername;PASSWORD=myPassword;OPTION=3;"
// other connection string
// please refer to: http://www.connectionstrings.com/
// To check the database error, please use EASendMail Service Manager->Journal->System Error
//EASendMail will use the following connection to connect to the database, the syntax is same as
//ADO connection object.
oMail.Headers.ReplaceHeader( "X-Data-Connection", "Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\\easendmail\\easendmail_demo.mdb;Uid=;Pwd=;" );
//EASendMail will select the fields by the following sql statement before sending email,
//then pick the recipient address from specified field.
oMail.Headers.ReplaceHeader( "X-Sql-Select", "SELECT id, name, address FROM Recipients" );
//pick "name" field as the recipient name and "address" field as the recipient address.
//you can also use {$var_srecord:fieldname} to pick any field in X-Sql-Select statement
// and put it to subject, bodytext, then EASendMail will replace it automatially.
oMail.Headers.ReplaceHeader( "To", "\"{$var_srecord:name}\" <{$var_srecord:address}>" );
oMail.Headers.ReplaceHeader( "X-Rcpt-To", "{$var_srecord:address}");
//EASendMail service will execute the following sql statement on every email was sent successfully.
oMail.Headers.ReplaceHeader( "X-Sql-OnSentSuccess", "INSERT INTO sentlog ( server, email ) VALUES( '{$var_server}', '{$var_rcptaddr}' )" );
//EASendMail service will execute the following sql statement on every email was unable to sent.
oMail.Headers.ReplaceHeader( "X-Sql-OnSentError", "INSERT INTO errorlog( email, server, errorcode, errordescription ) VALUES( '{$var_rcptaddr}', '{$var_server}', '{$var_errcode}', '{$var_errdesc}' )" );
string s = "Hi {$var_srecord:name}, \r\nthis sample demonstrates how to send email in asp.net with EASendMail service.\r\n\r\n";
s += "To:{$var_srecord:address}\r\n\r\n"; //{$var_srecord:address} will be replaced by EASendMail automatically.
s += "recipient email address and name variable will be replaced by EASendMail service automatically\r\n\r\n";
s += "If no server address was specified, the email will be delivered to the recipient's server by the setting in ";
s += "EASendMail Service.\r\n\r\n";
s += "Your id in database is {$var_srecord:id}.\r\n\r\n";
s += "No matter how many recipients there are, EASendMail ";
s += "service will send the email in background.\r\n\r\n";
//{$var_srecord:id} {$var_srecord:address} {$var_srecord:name} in body text will
//be replaced by EASendMail automatically.
oMail.TextBody = s;
SmtpClient oSmtp = new SmtpClient();
string err = "";
try
{
oSmtp.SendMailToQueue( null, oMail );
}
catch( System.Exception exp )
{
err = String.Format( "Exception: Common: {0}", exp.Message );
err += "Please make sure you installed EASendMail Service on the server!";
}
To learn more about EASendMail Service, please refer to EASendMail Service.
See Also
Using EASendMail SMTP Component
User Authentication and SSL Connection
Digital Signature and E-mail Encryption
Send E-mail Directly (Simulating SMTP server)
EASendMail Namespace References
EASendMail SMTP Component Samples