从剪贴板上读取Outlook中的邮件

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            IDataObject iData = Clipboard.GetDataObject();
            MemoryStream ms = (MemoryStream)iData.GetData("Csv");
            if (ms != null)
            {
                byte[] buffer = new byte[ms.Length];
                ms.Read(buffer, 0, buffer.Length);
                string EntryId = System.Text.Encoding.Unicode.GetString(buffer);
                Process[] ps = Process.GetProcessesByName("OUTLOOK");
                Outlook.Application olApp = null;
                if (ps.Length > 0)
                {
                    olApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
                }
                else
                {
                    olApp = new Outlook.Application();
                }
                var item = olApp.Session.GetItemFromID(EntryId);
                item.SaveAs("test.msg", Outlook.OlSaveAsType.olMSGUnicode);
            }
            else
            {
                MessageBox.Show("NO");
            }
        }
    }
}



欢迎访问《许阳的红泥屋

欢迎访问《许阳的红泥屋
posted @ 2012-10-02 16:03  许阳 无锡  阅读(209)  评论(0编辑  收藏  举报