C#对Outlook2010 编程

来源:互联网 发布:mac make alias 编辑:程序博客网 时间:2024/05/16 16:59
 

需求内容:

1.由于某同事的邮件从08-11年很多,现不再存放于本地邮件文件中.pst,而是另存到本地文件夹中 .msg格式

2.将其邮件分年月保存在本地文件夹中

3.将其邮件存入数据库中,含发件人,主题,收件人,收件人数目,抄送人,发送时间,用于统计的年月时间

//4.将数据库的数据 生成趋势图

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.IO;

namespace J_Mail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ApplicationClass ac = new ApplicationClass();
        NameSpace ns;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ns = ac.GetNamespace("MAPI");
                //使用对应账号,密码登陆相应邮箱
                //ns.Logon("", "", false, true);
            }
            catch (System.Exception error)
            {
                throw new System.Exception("登陆Outlook失败"+error.Message);
            }

            MAPIFolder inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            //邮箱中的所有数目
            int count = inbox.Items.Count;
            //发件人
            string SenderName = "";
            //邮箱主题
            string Subject = "";
            //接收时间
            DateTime ReceivedTime = DateTime.Now;
            //邮件内容
            //string Body = "";
            //收件人
            string Receiver="";    //当前收件人
            Recipients recipients; //全部收件人集合
            //邮件抄送人
            string ReceiverCC = "";
            //邮件暗送人
            string ReceiverBCC = "";
            //邮件附件
            //收件人数目
            int intReceiverCount;
            //全部收件人
            string ReceiverAll = "";

            #region MailItem
            foreach (Object obj in inbox.Items)
            {

                //普通非会议邮件
                MailItem item = obj as MailItem;
                if (item != null)
                {
                    //发件人
                    SenderName = item.SenderName;
                    if (SenderName != "****")
                    {
                        continue;
                    }
                    //主题
                    if (item.Subject != null)
                    {
                        Subject = item.Subject.Replace("'", "‘");
                    }
                    //接收时间
                    ReceivedTime = item.ReceivedTime;
                    string dateReceivedMonth = ReceivedTime.ToString("yyyyMM");
                    string dateReceivedMinute = ReceivedTime.ToString("yyyyMMddHHMM");
                    //Body = item.Body;
                    //当前收件人
                    Receiver = item.ReceivedByName;
                    //收件人群体集合
                    recipients = item.Recipients;
                    //收件人数目
                    intReceiverCount = recipients.Count;
                    ReceiverAll = "";
                    foreach (Recipient receipient in recipients)
                    {
                        ReceiverAll = ReceiverAll + receipient.Name + ";";
                    }
                    ReceiverAll = ReceiverAll.ToString().Replace("'", "‘").Replace("''", "");
                    //抄送人
                    if (item.CC != null)
                    {
                        ReceiverCC = item.CC.ToString().Replace("'", "‘");
                    }
                    //暗送人
                    ReceiverBCC = item.BCC;
                    DateTime dt=item.CreationTime;

                    //对标题进行正则表单式除去特殊字符,用于生成文件
                    string title = Regex.Replace(Subject, @"[\\\/\:\?\<\>\*\|\\\""]", "_");

                    //插入数据库用于后续MSChart趋势分析
                    SqlConnection connection = new SqlConnection("Data Source=***;Initial Catalog=J_Mail;User ID=***;Password=***");
                    connection.Open();

                    string strSql = "insert into Jenny(SenderName,Subject,Receiver,ReceiverCount,ReceiverCC,ReceiverBCC,SendedTime,TongjiTime) "
                        + " values('" + SenderName + "','" + Subject + "','" + ReceiverAll + "','" + intReceiverCount + "','" + ReceiverCC + "','" + ReceiverBCC + "','" + ReceivedTime + "','" + dateReceivedMonth + "')";

                    SqlCommand cmd = new SqlCommand(strSql, connection);
                    cmd.ExecuteNonQuery();
                    connection.Close();

                    //保存附件到指定目录
                    string JPath = @"D:\Jenny\";
                    if (!Directory.Exists(JPath + dateReceivedMonth))  //文件夹格式为年月
                    {
                        Directory.CreateDirectory(JPath + dateReceivedMonth);
                    }
                    //生成随即数用于区别同时间同主题的邮件
                    string strJPath;
                    Random ran=new Random();
                    string strRan = ran.Next(100).ToString();

                    //保存
                    strJPath = JPath + dateReceivedMonth + "\\" + dateReceivedMinute + "_" + strRan + '_' + title + ".msg";
                    item.SaveAs(strJPath, 3);

                    //将邮箱删除至已删除邮件
                    item.Delete(); 
                }          
            }
            #endregion

            #region MeetingItem
            foreach (Object obj in inbox.Items)
            {

                //会议类邮件
                MeetingItem item = obj as MeetingItem;
                if (item != null)
                {
                    //发件人
                    SenderName = item.SenderName;
                    if (SenderName != "****")
                    {
                        continue;
                    }
                    //主题
                    if (item.Subject != null)
                    {
                        Subject = item.Subject.Replace("'", "‘");
                    }
                    //接收时间
                    ReceivedTime = item.ReceivedTime;
                    string dateReceivedMonth = ReceivedTime.ToString("yyyyMM");
                    string dateReceivedMinute = ReceivedTime.ToString("yyyyMMddHHMM");
                    //Body = item.Body;
                    //当前收件人
                    //Receiver = item.ReceivedByName;
                    //收件人群体集合
                    recipients = item.Recipients;
                    //收件人数目
                    intReceiverCount = recipients.Count;
                    ReceiverAll = "";
                    foreach (Recipient receipient in recipients)
                    {
                        ReceiverAll = ReceiverAll + receipient.Name + ";";
                    }
                    ReceiverAll = ReceiverAll.Replace("'", "‘").Replace("'''", "’‘‘");
                    //抄送人
                    //ReceiverCC = item.CC;
                    ////暗送人
                    //ReceiverBCC = item.BCC;
                    DateTime dt = item.CreationTime;

                    //对标题进行正则表单式除去特殊字符,用于生成文件
                    string title = Regex.Replace(Subject, @"[\\\/\:\?\<\>\*\|\\\""]", "_");

                    //插入数据库用于后续MSChart趋势分析
                   SqlConnection connection = new SqlConnection("Data Source=***;Initial Catalog=J_Mail;User ID=***;Password=***");
                    connection.Open();

                    string strSql = "insert into Jenny(SenderName,Subject,Receiver,ReceiverCount,ReceiverCC,ReceiverBCC,SendedTime,TongjiTime) "
                        + " values('" + SenderName + "','" + Subject + "','" + ReceiverAll + "','" + intReceiverCount + "','" + ReceiverCC + "','" + ReceiverBCC + "','" + ReceivedTime + "','" + dateReceivedMonth + "')";

                    SqlCommand cmd = new SqlCommand(strSql, connection);
                    cmd.ExecuteNonQuery();
                    connection.Close();

                    //保存附件到指定目录
                    string JPath = @"D:\Jenny\";
                    if (!Directory.Exists(JPath + dateReceivedMonth))  //文件夹格式为年月
                    {
                        Directory.CreateDirectory(JPath + dateReceivedMonth);
                    }
                    //生成随即数用于区别同时间同主题的邮件
                    string strJPath;
                    Random ran = new Random();
                    string strRan = ran.Next(100).ToString();

                    //保存
                    strJPath = JPath + dateReceivedMonth + "\\" + dateReceivedMinute + "_" + strRan + '_' + title + ".msg";
                    item.SaveAs(strJPath, 3);

                    //将邮箱删除至已删除邮件
                    item.Delete();
                }
            }
            #endregion
        }

    }
}

原创粉丝点击