当前位置: 首页 > news >正文

群发邮件功能的完善

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

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 ,如需转载请自行联系原作者

相关文章:

  • VHDL——4层电梯系统设计
  • sql注入——避开过滤
  • oracle 截取字符串
  • C# 延时函数
  • MongoDB 学习笔记之 $or与索引关系
  • AS3 角度算法与实现
  • iText pdf 中文问题
  • 项目实战之集成邮件开发
  • 初中毕业水平,英语自学也能达到高级口译的水平(原创)
  • CISCO路由器配置总结(八)
  • postman 简单教程-实现简单的接口测试
  • UNIX中C语言的管道
  • BREW SDK 九大功能之其他服务
  • nodejs调试方法
  • JetSpeed 2.0安装步骤
  • ➹使用webpack配置多页面应用(MPA)
  • Angular 2 DI - IoC DI - 1
  • Angular数据绑定机制
  • Docker下部署自己的LNMP工作环境
  • Golang-长连接-状态推送
  • HashMap剖析之内部结构
  • interface和setter,getter
  • jquery ajax学习笔记
  • KMP算法及优化
  • Mac转Windows的拯救指南
  • Protobuf3语言指南
  • Python中eval与exec的使用及区别
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • 对象管理器(defineProperty)学习笔记
  • 聊聊flink的TableFactory
  • 聊聊springcloud的EurekaClientAutoConfiguration
  • 前端知识点整理(待续)
  • 双管齐下,VMware的容器新战略
  • 学习使用ExpressJS 4.0中的新Router
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • 云大使推广中的常见热门问题
  • Linux权限管理(week1_day5)--技术流ken
  • Prometheus VS InfluxDB
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • ​第20课 在Android Native开发中加入新的C++类
  • #etcd#安装时出错
  • #include到底该写在哪
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • #vue3 实现前端下载excel文件模板功能
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (5)STL算法之复制
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (论文阅读笔记)Network planning with deep reinforcement learning
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转)关于pipe()的详细解析
  • (转)为C# Windows服务添加安装程序
  • .halo勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复