It's not a new thing to send emails with asp.net. There are many ways to send, such as SmtpMail method, socket method, through Third-party components JMail and so on. But this article is not about the technology used to send mail, But through a company's project (mobile phone theme) Practice said that the mail engine architecture, there are deficiencies and improvements, welcomed the peer criticism.
The way we used to send emails was, trigger in the page to send, for example, registered members, click the Submit button, the member data inserted into the database, and then sent. Although this method is simple and convenient, there is a weakness that can not be resend if the mail fails. The approach in our project is to unify the messages that need to be sent into a mail queue, and then the engine handles the queue. The specific approach is to design the database:
Win_emailqueue (mail queue table)
Queueid int autonumber, primary key ID
Toemail nvarchar (100) recipient
Title nvarchar (100) message header
Content ntext Message contents
Adddate datetime Add Time
Trytimes int Error Retry count
The last time Lastsendtime datetime was sent
status int Status: 0 Not sent 1 has been sent
When you need to send a message, if the registered member succeeds, insert the contents of the message into the table.
The mail engine can be done with one system service. Installed on the Web server on the same server, can also be installed on the basis of the actual load on another server, reducing the burden on the Web server. The Mail engine's task is to have a time interval (such as 5 seconds), query the message queue, and send the message in chronological order, In order to reduce the burden on the engine, you can set 15 of each send, of course, this number should be configured according to the actual situation.
Here are some code to process queues and send messages.
///
Send email queue, from mobile theme http://www.shouji138.com
///
public static void Sendemailqueue ()
{
Take the latest 15 unsuccessful to send.
String sql = "Select Win_emailqueue where status=0 and toemail<> ' ORDER by adddate DESC";
DataTable dt = dbhelpersql.query (SQL). Tables[0];
for (int i = 0; i < dt. Rows.Count; i++)
{
string title = dt. rows[i]["Title"]. ToString ();
string content = dt. rows[i]["Content"]. ToString ();
string to = dt. rows[i]["Toemail"]. ToString ();
String createtime = dt. rows[i]["Adddate"]. ToString ();
String queueid = dt. rows[i]["Queueid"]. ToString ();
BOOL flag = Emailutil.send (title, to, content);
if (flag)
{
Send successfully, set status to 1
sql = "Update win_emailqueue set Status=1 where queueid=" + Queueid + "";
}
Else
{
Send failed, increase the number of failures by 1
sql = "Update win_emailqueue set trytimes=trytimes+1,lastsendtime= '" + DateTime.Now.ToString () + "' where queueid=" + Qu Eueid + "";
}
Dbhelpersql.executesql (SQL);
}
Dt. Dispose ();
More than 10 unsuccessful messages will no longer be sent
sql = "Update win_emailqueue set Status=1 where trytimes>10";
Dbhelpersql.executesql (SQL);
}
///
Perform the send operation from the mobile phone theme http://www.shouji138.com
///
///
public static bool Send (string title, string to, string content)
{
From configuration Items
String fromemail = system.configuration.configurationmanager.appsettings["Smtpusername"];
String smtpserver = system.configuration.configurationmanager.appsettings["SmtpServer"];
String frompwd = system.configuration.configurationmanager.appsettings["Smtppass"];
String fromaddress = system.configuration.configurationmanager.appsettings["Smtpnickname"];
MailMessage mail = new MailMessage ();
Mail. from = new MailAddress (Fromemail, fromaddress, encoding.getencoding ("gb2312"))//sender's mailbox
Mail. To.add (New MailAddress (to));//Recipient
Mail. Subject = title;//Theme
Mail. BODY = content;//Content
Mail. Isbodyhtml = true;
Mail. subjectencoding = encoding.getencoding ("gb2312");
Mail. bodyencoding = encoding.getencoding ("gb2312");
Mail. Isbodyhtml = true;
Mail. Priority = Mailpriority.normal;
SmtpClient sc = new SmtpClient (smtpserver)//email server Mobile Theme Http://www.shouji138.com
NetworkCredential NC = new NetworkCredential (Fromemail, frompwd);//Request account information
Sc. Credentials = NC;
BOOL Successflag;
Try
{
Sc. Send (mail);
Successflag = true;
}
Catch
{
Successflag = false;
}
return successflag;
}
In a virtual host, you can also use the homepage to load a script page, which acts as a mail engine. The code is as follows:
Ajax/ajaxm.aspx
protected void Page_Load (object sender, EventArgs e)
{
Try
{
Email.sendemailqueue (); Send mail
}
catch (Exception ex)
{
Log.saveexception (ex); Save error log
}
Response.Write ("");
Response.End ();
}
And then on the home page or inside pages, insert the code and can.