📄 emailutility.cs
字号:
namespace ASPNET.StarterKit.Communities {
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mail;
//*********************************************************************
//
// EmailUtility Class
//
// Contains static utility methods for sending emails.
//
//*********************************************************************
public class EmailUtility {
//*********************************************************************
//
// GetNewsletterStatus Method
//
// Returns the number of newsletters sent and the number of
// newsletters that still need to be sent.
//
//*********************************************************************
public static SqlDataReader GetNewsletterStatus(int newsletterID) {
SqlConnection conPortal = new SqlConnection( CommunityGlobals.ConnectionString );
SqlCommand cmdGet = new SqlCommand( "Community_AdminGetNewsletterStatus", conPortal );
cmdGet.CommandType = CommandType.StoredProcedure;
cmdGet.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmdGet.Parameters.Add("@newsletterID", newsletterID);
conPortal.Open();
SqlDataReader dr = cmdGet.ExecuteReader();
return dr;
}
//*********************************************************************
//
// SendNewsletter Method
//
// Sends newsletter to all users who requested a newsletter.
//
//*********************************************************************
public static void SendNewsletter
(
int newsletterID,
string subject,
string body,
MailFormat bodyFormat
) {
int status;
string subjectForUser;
string bodyForUser;
// Get the Smtp Server
string smtpServer = CommunityGlobals.SmtpServer;
// Format Subject and Body Text
subject = FormatContentText(subject, bodyFormat);
body = FormatContentText(body, bodyFormat);
// Create connection for updating email status
SqlConnection conStatus = new SqlConnection( CommunityGlobals.ConnectionString );
conStatus.Open();
// Get the users to send email
SqlConnection conNews = new SqlConnection( CommunityGlobals.ConnectionString );
SqlCommand cmdGet = new SqlCommand( "Community_AdminSendNewsletter", conNews );
cmdGet.CommandType = CommandType.StoredProcedure;
cmdGet.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmdGet.Parameters.Add("@newsletterID", newsletterID);
// Loop through the emails
conNews.Open();
SqlDataReader dr = cmdGet.ExecuteReader();
while (dr.Read()) {
// Update status
status = UpdateNewsletterStatus(conStatus, newsletterID, (int)dr["user_id"]);
if (status == 1)
throw new Exception( "newsletter already sent!");
// Copy the newsletter template
subjectForUser = subject;
bodyForUser = body;
// Format Subject for individual user
subjectForUser = FormatUserText
(
subject,
bodyFormat,
(string)dr["user_username"],
(string)dr["user_password"],
(string)dr["user_firstname"],
(string)dr["user_lastname"]
);
// Format Body for individual user
bodyForUser = FormatUserText
(
body,
bodyFormat,
(string)dr["user_username"],
(string)dr["user_password"],
(string)dr["user_firstname"],
(string)dr["user_lastname"]
);
// Send the newsletter
try {
SendEmail
(
String.Format("news@{0}", CommunityGlobals.PrimaryDomain),
(string)dr["user_email"],
subjectForUser,
bodyForUser,
bodyFormat,
smtpServer
);
} catch (Exception ex) {
ActivityUtility.RecordError("Error sending newsletter", ex);
}
// clear the user copies
subjectForUser = String.Empty;
bodyForUser = String.Empty;
}
conNews.Close();
// Close the update status connection
conStatus.Close();
}
//*********************************************************************
//
// UpdateNewsletterStatus Method
//
// Records the fact that a newsletter has been sent to a user
// so that the newsletter is not emailed twice.
//
//*********************************************************************
private static int UpdateNewsletterStatus
(
SqlConnection connection,
int newsletterID,
int userID
) {
SqlCommand cmdUpdateStatus = new SqlCommand( "Community_AdminUpdateNewsletterStatus", connection );
cmdUpdateStatus.CommandType = CommandType.StoredProcedure;
cmdUpdateStatus.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
cmdUpdateStatus.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmdUpdateStatus.Parameters.Add("@newsletterID", newsletterID);
cmdUpdateStatus.Parameters.Add("@userID", userID);
cmdUpdateStatus.ExecuteNonQuery();
return (int)cmdUpdateStatus.Parameters["@RETURN_VALUE"].Value;
}
//*********************************************************************
//
// AddEmail Method
//
// Adds a new email to the Emails table so it can be sent by the timer.
//
//*********************************************************************
// public static void AddEmail
// (
// string from,
// string to,
// string subject,
// string body,
// MailFormat bodyFormat,
// string smtpServer
// ) {
//
// SqlCommand cmdAdd = new SqlCommand( "Community_EmailsAddEmail", connection );
// cmdUpdateStatus.CommandType = CommandType.StoredProcedure;
// cmdUpdateStatus.Parameters.Add("@RETURN_VALUE", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
// cmdUpdateStatus.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
//
//
// MailMessage message = new MailMessage();
// message.From = from;
// message.To = to;
// message.Subject = subject;
// message.Body = body;
// message.BodyFormat = bodyFormat;
//
//
// SmtpMail.SmtpServer = smtpServer;
// SmtpMail.Send(message);
//
// }
//
//*********************************************************************
//
// SendEmail Method
//
// Sends a single email.
//
//*********************************************************************
public static void SendEmail
(
string from,
string to,
string subject,
string body,
MailFormat bodyFormat,
string smtpServer
) {
MailMessage message = new MailMessage();
message.From = from;
message.To = to;
message.Subject = subject;
message.Body = body;
message.BodyFormat = bodyFormat;
SmtpMail.SmtpServer = smtpServer;
SmtpMail.Send(message);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -