一个Windows Form获取Inbox下目录及邮件并另存邮件为msg文件的例程

这是根据一个客户要求写的简单例程用来替代Outlook view control的(Outlook view control的龟速实在令人不爽)由于没多大价值但是随便扔了又显得可惜,所以贴在这里希望能抛砖引玉给大家一个交流的机会。

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 System.Runtime.InteropServices;
using System.Diagnostics;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Security.Cryptography;

namespace WindowsOutlookIssue
{
    public partial class Form1 : Form
    {
        private Outlook.Application OlApp = null;
        private Outlook.MAPIFolder OlInbox = null;
        private string SavePath = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Process[] ps = Process.GetProcessesByName("OUTLOOK");
            if (ps.Length > 0)
            {
                OlApp = Marshal.GetActiveObject("Outlook.Application") as 
                    Outlook.Application;
            }
            else
            {
                OlApp = new Outlook.Application();
            }
            if (OlApp != null)
            {
                Outlook.NameSpace OlSession = OlApp.Session;
                Outlook.OlDefaultFolders d = Outlook.OlDefaultFolders
                    .olFolderInbox;
                OlInbox = OlSession.GetDefaultFolder(d);
                TreeNode root = new TreeNode("Inbox");
                treeView1.Nodes.Add(root);
                IterateInbox(OlInbox, root);
                treeView1.ExpandAll();
                ListMails(OlInbox);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (OlApp != null)
                Marshal.ReleaseComObject(OlApp);
        }

        private void IterateInbox(Outlook.MAPIFolder folder, TreeNode root)
        {
            if (folder.Folders.Count > 0)
            {
                foreach (Outlook.MAPIFolder f in folder.Folders)
                {
                    TreeNode child = new TreeNode(f.Name);
                    root.Nodes.Add(child);
                    IterateInbox(f, child);
                }
            }
        }

        private void treeView1_NodeMouseClick(object sender, 
            TreeNodeMouseClickEventArgs e)
        {
            Outlook.MAPIFolder currentFolder = GetCurrentFolder(e.Node
                .FullPath);
            ListMails(currentFolder);
        }

        private Outlook.MAPIFolder GetCurrentFolder(string Path)
        {
            Outlook.MAPIFolder result = null;
            string[] list = Path.Split('\\');
            if (list.Length == 1)
                result = OlInbox;
            if (list.Length > 1)
            {
                Outlook.MAPIFolder temp = null;
                result = OlInbox;
                for (int i = 1; i < list.Length; i++)
                {
                    temp = result.Folders[list[i]];
                    result = temp;
                }
            }
            return result;
        }

        private void ListMails(Outlook.MAPIFolder folder)
        {
            listView1.Items.Clear();
            string labe = "/" + folder.Items.Count.ToString();
            for (int i = 1; i <= folder.Items.Count; i++)
            {
                var obj = folder.Items[i];
                if (obj is Outlook.MailItem)
                {
                    Outlook.MailItem mail = (Outlook.MailItem)obj;
                    ListViewItem li = new ListViewItem(mail.ReceivedTime
                        .ToLongDateString());
                    li.SubItems.Add(mail.Subject);
                    li.SubItems.Add(mail.EntryID);
                    listView1.Items.Add(li);
                }
                Text = i.ToString() + labe;
            }
        }

        private void sToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            SavePath = fbd.SelectedPath;
        }

        private void saveCurrentItemToolStripMenuItem_Click(object sender,
            EventArgs e)
        {
            if (SavePath != null)
            {
                if (listView1.SelectedItems.Count != 1)
                {
                    MessageBox.Show("Please select only one item");
                }
                else
                {
                    string EntryID = listView1.SelectedItems[0].SubItems[2]
                        .Text;
                    Outlook.MailItem mail = OlApp.Session
                        .GetItemFromID(EntryID);
                    if (mail != null)
                    {
                        mail.SaveAs(SavePath + "\\"
                            + FileNameMaker(mail.ReceivedTime, mail.Subject)
                            + ".msg", Outlook.OlSaveAsType.olMSGUnicode);
                    }
                }
            }
            else
            {
                MessageBox.Show("Please set Save Path first!");
            }
        }

        private string FileNameMaker(DateTime received, String subject)
        {
            string result = null;
            using (MD5 md5Hash = MD5.Create())
            {
                byte[] buffer = md5Hash.ComputeHash(Encoding.UTF8
                .GetBytes(subject + "||" + received.ToLongDateString()));
                StringBuilder builder = new StringBuilder();
                for (int j = 0; j < buffer.Length; j++)
                {
                    builder.Append(buffer[j].ToString("x2"));
                }
                result = builder.ToString();
            }
            return result;
        }

        private void saveAllItemsToolStripMenuItem_Click(object sender,
            EventArgs e)
        {
            if (SavePath != null)
            {
                foreach (ListViewItem li in listView1.Items)
                {
                    string EntryID = li.SubItems[2].Text;
                    Outlook.MailItem mail = OlApp.Session
                        .GetItemFromID(EntryID);
                    if (mail != null)
                    {
                        mail.SaveAs(SavePath + "\\"
                            + FileNameMaker(mail.ReceivedTime, mail.Subject)
                            + ".msg", Outlook.OlSaveAsType.olMSGUnicode);
                    }
                }
            }
            else
            {
                MessageBox.Show("Please set Save Path first!");
            }
        }
    }
}



posted @ 2012-03-06 11:59  许阳 无锡  阅读(291)  评论(0编辑  收藏  举报