背景:現在幾乎每個企業都要用到郵箱,而在大客戶量情況下,為我們的不同等級的客戶送上節日關懷,以及把我們的喜訊、新品通知到他們是我們急需解決的問題。效果如圖
思路:
1、靜態化網頁模版,首先考慮需要發送什麼,把需要的東西做成小而精美的html靜態化頁面以待發送時候用到;
2、關於發送問題,點擊“發送郵件”彈出群發郵件的框,填寫主題、html的服務器所在地址、收件人(多個);
3、群發的實現。
代碼C#:
/// <summary>
/// 發送失敗的郵箱地址搜集參數
/// </summary>
private string errorMail;
群發郵件點擊事件
protected void BtnSendMail_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txTopic.Value))
{
MessageShow("郵件主題必填");
return;
}
if (string.IsNullOrEmpty(txUrl.Value))
{
MessageShow("URL內容必填");
return;
}
if (string.IsNullOrEmpty(txTo.Value))
{
MessageShow("收件人必填");
return;
}
string email = ConfigurationManager.AppSettings["ManagerSendMailUserName"].ToString(); //登錄用戶郵箱
string emailpwd = ConfigurationManager.AppSettings["ManagerSendMailUserPwd"].ToString(); //登錄用戶郵箱密碼
string s = GetHtmlCode(txUrl.Value, "gb2312");
bool restr = Sendmail(email, txTo.Value, txTopic.Value, true, s, "smtp.exmail.qq.com", email, emailpwd);
if (restr)
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('全部發送成功!');</script>");
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "",
"<script>alert('已發送,但以下地址發送失敗:" + errorMail + "可能是地址不存在!');</script>");
}
}
/// <summary>
/// 發送郵件
/// </summary>
/// <param name="from">發送人郵件地址</param>
/// <param name="to">接收人郵件地址</param>
/// <param name="subject">郵件主題</param>
/// <param name="isBodyHtml">是否是Html</param>
/// <param name="body">郵件體</param>
/// <param name="smtpHost">SMTP服務器地址,例如:smtp.163.com</param>
/// <param name="userName">用戶名</param>
/// <param name="password">密碼</param>
/// <returns>是否成功</returns>
public bool Sendmail(string from, string to, string subject, bool isBodyHtml, string body, string smtpHost, string userName, string password)
{
bool isSuccess = true;
string[] ts = null;
if (!string.IsNullOrEmpty(to))
{
if (to.Contains(";"))
ts = to.Split(';');
else
ts = new string[] { to };
foreach (string t in ts)
{
if (!string.IsNullOrEmpty(t))
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress(from);
mm.To.Add(new MailAddress(t));
mm.Subject = subject;
mm.IsBodyHtml = isBodyHtml;
mm.Body = body;
SmtpClient sc = new SmtpClient();
sc.Host = smtpHost;
sc.Port = 25;
sc.EnableSsl = false;
sc.Credentials = new NetworkCredential(userName, password);
sc.UseDefaultCredentials = true;
sc.Credentials = new System.Net.NetworkCredential(userName, password);
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
sc.Send(mm);
}
catch (Exception x)
{
errorMail += t + ";";
isSuccess = false;
continue;
}
}
}
}
return isSuccess;
}
/// <summary>
/// 獲取指定路徑下的模板的HTML源代碼
/// </summary>
/// <param name="TemplatePath">模板的路徑</param>
/// <param name="EncodingType">網頁類型(有些是UTF8,有些是GB2312)</param>
/// <returns>源代碼</returns>
private string GetHtmlCode(string TemplatePath, string EncodingType)
{
try
{
if (TemplatePath != string.Empty)
{
string ForesideUriPath = string.Empty;
if (!TemplatePath.ToLower().StartsWith("http://"))
ForesideUriPath = HttpContext.Current.Server.MapPath("~/") + TemplatePath;
else
ForesideUriPath = TemplatePath;
WebClient webClient = new WebClient();
//設置網絡憑證為系統憑證
webClient.Credentials = CredentialCache.DefaultCredentials;
//獲取指定URI的網頁的源代碼
byte[] byteDataBuffer = webClient.DownloadData(ForesideUriPath);
string htmlCode = "";
if (EncodingType == "UTF8")
{
htmlCode = Encoding.UTF8.GetString(byteDataBuffer);
}
else
{
htmlCode = Encoding.GetEncoding(EncodingType).GetString(byteDataBuffer);
}
htmlCode = Regex.Replace(htmlCode, @"<!DOCTYPE\s*HTML\s*PUBLIC[^>]+>", "", RegexOptions.Singleline);
htmlCode = Regex.Replace(htmlCode, @"\s+", " ", RegexOptions.Singleline);
return htmlCode;
}
else
{
return "";
}
}
catch (Exception ee)
{
throw (ee);
}
}
總結:這個功能還是非常有用的,大家可以擴展,定時發送什麼,實現各種爛漫的功能。最後說一聲,祝大家羊年大吉,心想事成!