zoukankan      html  css  js  c++  java
  • 定时扫描邮件系统代码整理

    Outlook定时扫描代码

    private void timer1_Tick(object sender, EventArgs e)
            {
                try
                {
                    var app = new Microsoft.Office.Interop.Outlook.Application();
                    var ns = app.GetNamespace("MAPI");
                    ns.Logon("Outlook", Type.Missing, false, false);
                    MAPIFolder inbox = null;
    
                    for (int j = 1; j <= ns.Folders.Count; j++)
                    {
                        inbox = ns.Folders[j].Folders[1];
                        string adds = inbox.FolderPath.Replace("\\", "").Replace("\", "").Replace("收件箱", "").Trim();
                        for (int i = 1; i <= inbox.Items.Count; i++)
                        {
                            if ((inbox.Items[i] as MailItem).UnRead)
                            {
                                MailItem item = inbox.Items[i] as MailItem;
    
                                SqlParameter[] prms = new SqlParameter[9];
    
                                prms[0] = new SqlParameter("@username", adds);
                                prms[1] = new SqlParameter("@cc", item.CC == null ? "" : item.CC);
                                prms[2] = new SqlParameter("@bcc", item.BCC == null ? "" : item.BCC);
                                prms[3] = new SqlParameter("@subject", item.Subject);
                                prms[4] = new SqlParameter("@content", item.HTMLBody);
                                prms[5] = new SqlParameter("@sender", item.SenderEmailAddress);
                                //prms[6] = new SqlParameter("@atth", item.Attachments.Count);
                                prms[7] = new SqlParameter("@send_datetime", item.CreationTime);
                                prms[8] = new SqlParameter("@to_address", item.To);
    
                                string atth = this.SaveAtth(item, adds);
    
                                 prms[6] = new SqlParameter("@atth", atth);
    
                                int num = DBHelper.ExecuteCommand("uptMail", prms);
    
                                if (num > 0)
                                {
                                    item.UnRead = false;
                                }
                            }
                        }
                    }
    
                    ns.Logoff();
                    Marshal.ReleaseComObject(inbox);
                    Marshal.ReleaseComObject(ns);
                    Marshal.ReleaseComObject(app);
                    inbox = null;
                    ns = null;
                    app = null;
                }
                catch (System.Exception ex)
                {
                    Utility.SendMail("wp126king@126.com");
    
                    if (File.Exists("D:\MailLog\MailLog_" + DateTime.Now.ToString("yyyyMMdd") + ".log"))
                    {
                        string str = "";
                        FileStream fs1 = new FileStream("D:\MailLog\MailLog_" + DateTime.Now.ToString("yyyyMMdd") + ".log", FileMode.Open, FileAccess.Read);
                        StreamReader sr = new StreamReader(fs1);
    
                        str = sr.ReadToEnd();
    
                        sr.Close();
                        fs1.Close();
    
                        FileStream fs = new FileStream("D:\MailLog\MailLog_" + DateTime.Now.ToString("yyyyMMdd") + ".log", FileMode.Open, FileAccess.Write);
                        StreamWriter sw = new StreamWriter(fs);
                        sw.WriteLine(str + ex.Message);
                        sw.Close();
                        fs.Close();
                    }
                    else
                    {
                        FileStream fs = new FileStream("D:\MailLog\MailLog_" + DateTime.Now.ToString("yyyyMMdd") + ".log", FileMode.Create, FileAccess.Write);
                        StreamWriter sw = new StreamWriter(fs);
                        sw.WriteLine(ex.Message);
                        sw.Close();
                        fs.Close();
                    }
    
                    
    
                }
                finally
                {
    
    
                }
            }

    最小化代码

    private void Form1_SizeChanged(object sender, EventArgs e)
            {
                //判断是否选择的是最小化按钮 
                if (WindowState == FormWindowState.Minimized)
                {
                    //托盘显示图标等于托盘图标对象 
                    //注意notifyIcon1是控件的名字而不是对象的名字 
                    notifyIcon1.Icon = new Icon(System.Windows.Forms.Application.StartupPath + "\ico.ico");
                    //隐藏任务栏区图标 
                    this.ShowInTaskbar = false;
                    //图标显示在托盘区 
                    notifyIcon1.Visible = true;
                }
            }
    
            private void notifyIcon1_DoubleClick(object sender, EventArgs e)
            {
                //判断是否已经最小化于托盘 
                if (WindowState == FormWindowState.Minimized)
                {
                    //还原窗体显示 
                    WindowState = FormWindowState.Normal;
                    //激活窗体并给予它焦点 
                    this.Activate();
                    //任务栏区显示图标 
                    this.ShowInTaskbar = true;
                    //托盘区图标隐藏 
                    notifyIcon1.Visible = false;
                }
            }

    Outlook保存附件代码

    public string SaveAtth(MailItem item, string adds) 
            {
                string res = "";
                string path = "D:\work\DecisionWeb\DecisionWeb\PrecompiledWeb\DecisionWeb\MailFile" + adds + "\" + DateTime.Now.ToString("yyyyMMddHHmmss");
                DirectoryInfo di = new DirectoryInfo(path);
                if (!di.Exists) 
                {
                    di.Create();
                }
    
                for (int i = 1; i <= item.Attachments.Count; i++) 
                {
                    item.Attachments[i].SaveAsFile(path + "\" + item.Attachments[i].FileName);
                    res += path + "\" + item.Attachments[i].FileName + "&" + item.Attachments[i].FileName + ";";
                }
    
                if (res.Length > 0) 
                {
                    res = res.Substring(0, res.Length - 1);
                }
    
                return res;
            }

    定时扫描Tomcat代码

    private void timer1_Tick(object sender, EventArgs e)
            {
                try
                {
                    this.timer1.Stop();
                    DataTable dt = Utility.getUser();
                    foreach (DataRow dr in dt.Rows)
                    {
                        HttpWebRequest request = WebRequest.Create(@"http://192.168.1.108:8080/mail/getmail.jsp") as HttpWebRequest;
    
                        request.Method = "post";
    
                        SymmetricMethod sm = new SymmetricMethod();
    
                        // 拼接成请求参数串,并进行编码,成为字节  
                        string postData = "username=" + dr["email"].ToString() + "&pass=" + sm.Decrypto(dr["password"].ToString()) + "&server=" + dr["server"].ToString() + "&port=" + dr["port"].ToString() + "&ssl=" + dr["ssl"].ToString();
                        ASCIIEncoding encoding = new ASCIIEncoding();
                        byte[] byte1 = encoding.GetBytes(postData);
                        // 设置请求的参数形式  
                        request.ContentType = "application/x-www-form-urlencoded";
                        request.Headers.Add("content", "text/html; charset=gbk");
    
                        // 设置请求参数的长度.  
                        request.ContentLength = byte1.Length;
    
                        // 取得发向服务器的流  
                        System.IO.Stream newStream = request.GetRequestStream();
    
                        // 使用 POST 方法请求的时候,实际的参数通过请求的 Body 部分以流的形式传送  
                        newStream.Write(byte1, 0, byte1.Length);
    
                        // 完成后,关闭请求流.  
                        newStream.Close();
    
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                        // 然后可以得到以流的形式表示的回应内容  
                        System.IO.Stream receiveStream
                            = response.GetResponseStream();
    
                        // 还可以将字节流包装为高级的字符流,以便于读取文本内容   
                        // 需要注意编码  
                        System.IO.StreamReader readStream
                            = new System.IO.StreamReader(receiveStream, Encoding.GetEncoding("GB2312"));
    
                        string res = readStream.ReadToEnd();
    
                        // 完成后要关闭字符流,字符流底层的字节流将会自动关闭  
                        response.Close();
                        readStream.Close();
    
                        if (res.Trim().Equals("error"))
                        {
                            this.SendError("ERROR:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "__" + dr["email"].ToString());
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    this.SendError(ex.Message);
                }
                finally 
                {
                    this.timer1.Start();
                }
            }
    
            public void SendError(string ex) 
            {
                Utility.SendMail("wp126king@126.com");
    
                if (File.Exists("D:\MailLog\MailLog_" + DateTime.Now.ToString("yyyyMMdd") + ".log"))
                {
                    string str = "";
                    FileStream fs1 = new FileStream("D:\MailLog\MailLog_" + DateTime.Now.ToString("yyyyMMdd") + ".log", FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs1);
    
                    str = sr.ReadToEnd();
    
                    sr.Close();
                    fs1.Close();
    
                    FileStream fs = new FileStream("D:\MailLog\MailLog_" + DateTime.Now.ToString("yyyyMMdd") + ".log", FileMode.Open, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(str + ex);
                    sw.Close();
                    fs.Close();
                }
                else
                {
                    FileStream fs = new FileStream("D:\MailLog\MailLog_" + DateTime.Now.ToString("yyyyMMdd") + ".log", FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(ex);
                    sw.Close();
                    fs.Close();
                }
            }
  • 相关阅读:
    java在线聊天项目0.7版 连接多个客户端问题,开启多个客户端后服务器端只接收到一个 对各种异常的补充处理
    java在线聊天项目0.6版 解决客户端关闭后异常问题 dis.readUTF()循环读取已关闭的socket
    指针 引用 举例:通过指针变量访问整型变量
    字符串数组 输入3个字符串,要求按由小到大的字母顺序输出; 输入n个学生的姓名和学号到字符串数组中,在输入一个姓名,如果班级有该生则返回其信息,否则返回本班无此人
    java在线聊天项目0.5版 解决客户端向服务器端发送信息时只能发送一次问题 OutputStreamWriter DataOutputStream socket.getOutputStream()
    java在线聊天项目0.4版本 制作服务端接收连接,客户端连接功能 新增客户端窗口打开时光标指向下边文本域功能,使用WindowListener监听WindowAdapter
    java在线聊天项目0.3版本 制作客户端窗体,实现发送按钮和回车发送信息功能,使用ActionListener监听事件中actionPerformed方法(用内部类和匿名内部类两种方法)
    java在线聊天项目0.2版本 制作客户端窗体,使用swing(用户界面开发工具包)和awt(抽象窗口工具包) BorderLayout布局与GridLayout布局不同之处 JPanel设置大小
    java在线聊天项目0.1版本 制作客户端窗体,使用swing(用户界面开发工具包)和awt(抽象窗口工具包)
    HashCode的作用原理和实例解析
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/3740584.html
Copyright © 2011-2022 走看看