Rank: Administration
Groups: Administrators
Joined: 11/11/2010(UTC) Posts: 1,154
Thanks: 9 times Was thanked: 55 time(s) in 55 post(s)
|
C# Example// The following example codes demonstrate sending email message using email queue + database // To get full sample projects, please download and install EASendMail on your machine. // To run it correctly, please change SMTP server, user, password, sender, recipient value to yours Code:
using System;
using System.Collections.Generic;
using System.Text;
// Add EASendMail namespace
using EASendMail;
namespace mysendemail
{
class Program
{
static void Main(string[] args)
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Set sender email address, please change it to yours
oMail.From = "test@emailarchitect.net";
// Set email subject
oMail.Subject = "test email from c# project";
// Your SMTP server address
SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");
// User and password for ESMTP authentication, if your server doesn't require
// User authentication, please remove the following codes.
oServer.User = "test@emailarchitect.net";
oServer.Password = "testpassword";
// If your smtp server requires SSL connection, please add this line
// oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
// If you want EASendMail service to send the email after 10 minutes,
// use the following code.
// oMail.Date = System.DateTime.Now.AddMinutes( 10 );
// 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 could not be 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\n";
s += "this sample demonstrates how to send email using email queue.\r\n\r\n";
// {$var_srecord:address} will be replaced by EASendMail automatically.
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;
try
{
Console.WriteLine("start to send email ...");
oSmtp.SendMailToQueue(oServer, oMail);
Console.WriteLine("email was sent to queue successfully!");
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
}
}
}
Click here to read original topic - full version ...If you have any comments or questions about above example codes, please add your comments here. Edited by user Thursday, January 8, 2015 1:30:47 AM(UTC)
| Reason: Not specified
|