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("[email protected]"); 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("[email protected]"); 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(); } }