群发邮件功能的完善
郵件有需要加密的地方,提供一個加密方法類
public static class DesSet
{
??? /// <summary>
??? /// 加密方法 Key 必須為8位
??? /// </summary>
??? /// <param name="pToEncrypt"></param>
??? /// <param name="sKey"></param>
??? /// <returns></returns>
??? public static string Encrypt(string pToEncrypt, string sKey)
??? {
??????? DESCryptoServiceProvider des = new DESCryptoServiceProvider();
??????? byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
??????? des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
??????? des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
??????? MemoryStream ms = new MemoryStream();
??????? CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
??????? cs.Write(inputByteArray, 0, inputByteArray.Length);
??????? cs.FlushFinalBlock();
??????? StringBuilder ret = new StringBuilder();
??????? foreach (byte b in ms.ToArray())
??????? {
??????????? ret.AppendFormat("{0:X2}", b);
??????? }
??????? ret.ToString();
??????? return ret.ToString();
??? }
??? /// <summary>
??? /// 解密方法 Key 必須為8位
??? /// </summary>
??? /// <param name="pToDecrypt"></param>
??? /// <param name="sKey"></param>
??? /// <returns></returns>
??? public static string Decrypt(string pToDecrypt, string sKey)
??? {
??????? try
??????? {
??????????? DESCryptoServiceProvider des = new DESCryptoServiceProvider();
??????????? byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
??????????? for (int x = 0; x < pToDecrypt.Length / 2; x++)
??????????? {
??????????????? int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
??????????????? inputByteArray[x] = (byte)i;
??????????? }
??????????? des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
??????????? des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
??????????? MemoryStream ms = new MemoryStream();
??????????? CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
??????????? cs.Write(inputByteArray, 0, inputByteArray.Length);
??????????? //如果數據為空字符串會報不正確的數據
??????????? cs.FlushFinalBlock();
??????????? StringBuilder ret = new StringBuilder();
??????????? return System.Text.Encoding.Default.GetString(ms.ToArray());
??????? }
??????? catch
??????? {
??????????? return null;
??????? }
??? }
??? /// <summary>
??? /// 解密方法 Key 必須為8位
??? /// </summary>
??? /// <param name="pToDecrypt"></param>
??? /// <param name="sKey"></param>
??? /// <returns></returns>
??? public static string qxDecrypt(string pToDecrypt, string sKey)
??? {
??????? DESCryptoServiceProvider des = new DESCryptoServiceProvider();
??????? byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
??????? for (int x = 0; x < pToDecrypt.Length / 2; x++)
??????? {
??????????? int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
??????????? inputByteArray[x] = (byte)i;
??????? }
??????? des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
??????? des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
??????? MemoryStream ms = new MemoryStream();
??????? CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
??????? cs.Write(inputByteArray, 0, inputByteArray.Length);
??????? //如果數據為空字符串會報不正確的數據
??????? cs.FlushFinalBlock();
??????? StringBuilder ret = new StringBuilder();
??????? return System.Text.Encoding.Default.GetString(ms.ToArray());
??? }
}
********************************************************************************
群發上千封郵件,發送太快,會出現可能被服務器拒絕的情況。使用線程控制發送間隔
??? protected void btn_sendMail_Click(object sender, EventArgs e)
??? {
??????? ThreadStart mailThread = new ThreadStart(SendMail);
??????? Thread sendMail = new Thread(mailThread);
??????? sendMail.Name = "thread send mail";
??????? sendMail.Start();
??? }
??? private void SendMail()
??? {
??????? Mails mySendMail = new Mails();
??????? for (int i = 0; i < lists.Count; i++)
??????? {
??????????? mySendMail = new Mails("nihao",lists[i].ToString(), lists[i]);
??????????? mySendMail.SendMail();
??????????? if ((i + 1) % 50 == 0)
??????????? {
??????????????? Thread.Sleep(60000);//歇一分鐘再發吧。。
??????????? }
??????? }
??? }
*****************************************************************
Mails類
??? public void SendMail()
??? {
??????? lock (this)
??????? {
??????????? Thread.Sleep(3000);
??????????? //創建smtpclient對象
??????????? System.Net.Mail.SmtpClient client = new SmtpClient();
??????????? client.Host = smtp;
??????????? client.UseDefaultCredentials = false;
??????????? client.Credentials = new System.Net.NetworkCredential(from,pwd);
??????????? client.DeliveryMethod = SmtpDeliveryMethod.Network;
??????????? //創建mailMessage對象?
??????????? System.Net.Mail.MailMessage message = new MailMessage(from,to);
??????????? message.Subject = subject;
??????????? message.SubjectEncoding = Encoding.UTF8;
??????????? message.Body = body;
??????????? message.BodyEncoding = System.Text.Encoding.UTF8;
??????????? message.IsBodyHtml = true;
??????????? try
??????????? {
??????????????? client.Send(message);
??????????????? StreamWriter sw = new StreamWriter("c:/message.txt", true);
??????????????? sw.Write(this.to+"? 發送成功" + "\r\n");
??????????????? sw.Close();
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? StreamWriter sw = new StreamWriter("c:/message.txt", true);
??????????????? sw.Write(this.to+"? 發送失敗。失敗原因:"+ex.Message + "\r\n");
??????????????? sw.Close();
??????????? }
??????? }
??? }
總結
- 上一篇: 白羽鸡(说一说白羽鸡的简介)
- 下一篇: 五叶神香烟价格表(五叶神香烟价格)