zoukankan      html  css  js  c++  java
  • 群发邮件功能的完善

    邮件有需要加密的地方,提供一个加密方法类

    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+"  发送成功" + " ");
                    sw.Close();
                }
                catch (Exception ex)
                {
                    StreamWriter sw = new StreamWriter("c:/message.txt", true);
                    sw.Write(this.to+"  发送失败。失败原因:"+ex.Message + " ");
                    sw.Close();
                }
            }
        }

  • 相关阅读:
    c# 三种取整方法 向上取整 向下取整 四舍五入
    Lambda表达式对DataRow处理
    Dapper数据库字段和model属性映射
    union limit
    北邮五十题
    搜索____深搜 学易错点
    动态规划____有重叠子问题的搜索,都可以转为记忆化搜索
    64位 __int 与 long long写法
    做做 卡特兰数 与 卡米歇尔数
    vector 有点麻烦啊 能简单点么?
  • 原文地址:https://www.cnblogs.com/soundcode/p/3661830.html
Copyright © 2011-2022 走看看