群发邮件功能的完善

简介:

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

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();
            }
        }
    }

本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/3661830.html ,如需转载请自行联系原作者
相关文章
|
3月前
|
API 开发工具 Python
钉钉有没有获取群消息内容的接口?怎么提工单(bug反馈或技术答疑)?
钉钉有没有获取群消息内容的接口?怎么提工单(bug反馈或技术答疑)?【1月更文挑战第6天】【1月更文挑战第29篇】
111 1
|
自然语言处理 应用服务中间件 容器
钉钉技术群收集
钉钉技术群收集
617 0
|
自然语言处理 算法 数据安全/隐私保护
教你如何用代码自动群发邮件(邮件轰炸机)
教你如何用代码自动群发邮件(邮件轰炸机)
1177 0
教你如何用代码自动群发邮件(邮件轰炸机)
|
1月前
|
SQL DataWorks 机器人
DataWorks常见问题之导致钉钉群的机器人发报警消息如何解决
DataWorks是阿里云提供的一站式大数据开发与管理平台,支持数据集成、数据开发、数据治理等功能;在本汇总中,我们梳理了DataWorks产品在使用过程中经常遇到的问题及解答,以助用户在数据处理和分析工作中提高效率,降低难度。
45 7
|
7月前
钉钉中,流程功能
钉钉中,流程功能
59 1
|
9月前
|
存储 监控 机器人
不论微信钉钉还是什么软件,我写了个通用的消息监控处理机器人
不论微信钉钉还是什么软件,我写了个通用的消息监控处理机器人
|
机器人 Linux 网络安全
利用GrayLog告警功能实现钉钉群机器人定时工作提醒
利用GrayLog告警功能实现钉钉群机器人定时工作提醒
293 0
利用GrayLog告警功能实现钉钉群机器人定时工作提醒
|
文字识别 监控 安全
《钉钉连接平台速成手册》全新发布!
钉钉连接平台,通过简单的低代码配置,帮助企业迅捷实现系统集成和连接。原本应用之间的连接需要技术人员去开发才能实现,通过连接平台,非专业技术同学也能够通过简单的低代码或者零代码的方式就实现应用互联互通,帮助企业降低集成实施的周期和成本。
《钉钉连接平台速成手册》全新发布!
|
存储 网络协议 算法
把 14 亿人拉到一个微信群,如何实现?
把 14 亿中国人民都拉到一个微信群里在技术上能实现吗?
把 14 亿人拉到一个微信群,如何实现?

热门文章

最新文章