在主窗体的一个按钮下, 执行如下事件:private void btnSend_Click(object sender, EventArgs e)
        {
            MAPI mapi = new MAPI();
            mapi.AddAttachment(@"D:\test.txt");
            mapi.SendMailPopup("subject", "body");
        }事件的功能就是启动OutLook的新建邮件的窗体,并添加附件.执行这个事件后,启动新建邮件窗体成功, 但在这个窗体不关闭的情况下,主窗体被阻塞锁定, 如何能做到启动新建邮件窗体后, 不关闭该窗体的情况下,主窗体不被阻塞锁定???? 哪位高人能指点一下????MAPI代码如下:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SendFileTo;namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load_1(object sender, EventArgs e)
        {
            EnableButtons();
        }        private void EnableButtons()
        {
            btnSend.Enabled = edtSubject.Text.Length > 0 && edtAttachment.Text.Length > 0;
        }        private void OnTextChanged(object sender, EventArgs e)
        {
            EnableButtons();
        }        private void OnBrowseClicked(object sender, MouseEventArgs e)
        {
            if (DialogResult.OK == openFileDialog1.ShowDialog())
                edtAttachment.Text = openFileDialog1.FileName;
        }        private void btnSend_Click(object sender, EventArgs e)
        {
            MAPI mapi = new MAPI();
            mapi.AddAttachment(edtAttachment.Text);
            mapi.SendMailPopup(edtSubject.Text, edtAttachment.Text);
        }        private void button1_Click(object sender, EventArgs e)
        {
            MAPI mapi = new MAPI();            mapi.AddAttachment("c:\\temp\\file1.txt");
            mapi.AddAttachment("c:\\temp\\file2.txt");
            mapi.AddRecipientTo("[email protected]");
            mapi.AddRecipientTo("[email protected]");
            mapi.SendMailPopup("testing", "body text");
       }        private void button2_Click(object sender, EventArgs e)
        {
            MAPI mapi = new MAPI();            mapi.AddAttachment("c:\\temp\\file1.txt");
            mapi.AddAttachment("c:\\temp\\file2.txt");
            mapi.AddRecipientTo("[email protected]");
            mapi.AddRecipientTo("[email protected]");
            mapi.SendMailPopup("testing", "body text");
        }    }
}

解决方案 »

  1.   

    time_is_life(今夜太冷:http://timeislife.blog.sohu.com)兄:程序执行到这一句被阻塞, 
    mapi.SendMailPopup("subject", "body");你说的Form.Active()没有用的啊
      

  2.   

    spkl1(柏芝你走吧我放不下韩红就叫我朝伟吧虽然歌唱得像学友)兄,如何开个子线程,能否帮写段代码?
      

  3.   

    private void btnSend_Click(object sender, EventArgs e)
    {
    Thread t=new Thread(send);
    t.Start();
    }
    void send()
    {
    MAPI mapi = new MAPI();
    mapi.AddAttachment(@"D:\test.txt");
    mapi.SendMailPopup("subject", "body");
    }
      

  4.   

    你可以建一个后台线程:BackgroundWorker
    用后台线程启动OutLook的新建邮件的窗体
      

  5.   

    wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) ( 一星(中级)) 兄, 
    private void btnSend_Click(object sender, EventArgs e)
    {
    Thread t=new Thread(send);
    t.Start();
    }
    void send()
    {
    MAPI mapi = new MAPI();
    mapi.AddAttachment(@"D:\test.txt");
    mapi.SendMailPopup("subject", "body");
    }我试了一下,这样调用竟然失败了,我直接
    private void btnSend_Click(object sender, EventArgs e)
    {
    MAPI mapi = new MAPI();
    mapi.AddAttachment(@"D:\test.txt");
    mapi.SendMailPopup("subject", "body");
    }
    调用是成功的,不知为何??
      

  6.   

    wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) 兄:新建邮件窗体没有启动m_lastError = MAPISendMail(new IntPtr(0), new IntPtr(0), msg, how, 0);这一句返回  2; 错误信息: General MAPI failure
      

  7.   

    MAPI代码如下:
    using System;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace SendFileTo
    {
        class MAPI
        {
            public bool AddRecipientTo(string email)
            {
                return AddRecipient(email, HowTo.MAPI_TO);
            }        public bool AddRecipientCC(string email)
            {
                return AddRecipient(email, HowTo.MAPI_TO);
            }        public bool AddRecipientBCC(string email)
            {
                return AddRecipient(email, HowTo.MAPI_TO);
            }        public void AddAttachment(string strAttachmentFileName)
            {
                m_attachments.Add(strAttachmentFileName);
            }        public int SendMailPopup(string strSubject, string strBody)
            {
                return SendMail(strSubject, strBody, MAPI_LOGON_UI | MAPI_DIALOG | MAPI_NEW_SESSION);
            }        public int SendMailDirect(string strSubject, string strBody)
            {
                return SendMail(strSubject, strBody, MAPI_LOGON_UI);
            }
            [DllImport("MAPI32.DLL")]
            static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv);        int SendMail(string strSubject, string strBody, int how)
            {
                MapiMessage msg = new MapiMessage();
                msg.subject = strSubject;
                msg.noteText = strBody;            msg.recips = GetRecipients(out msg.recipCount);
                msg.files = GetAttachments(out msg.fileCount);            m_lastError = MAPISendMail(new IntPtr(0), new IntPtr(0), msg, how, 0);
                if (m_lastError > 1)
                    MessageBox.Show("MAPISendMail failed! " + GetLastError(), "MAPISendMail");            Cleanup(ref msg);
                return m_lastError;
            }        bool AddRecipient(string email, HowTo howTo)
            {
        MapiRecipDesc recipient = new MapiRecipDesc();            recipient.recipClass = (int)howTo;
         recipient.name = email;
        m_recipients.Add(recipient);            return true;
            }        IntPtr GetRecipients(out int recipCount)
            {
                recipCount = 0;
                if (m_recipients.Count == 0)
                    return IntPtr.Zero;            int size = Marshal.SizeOf(typeof(MapiRecipDesc));
                IntPtr intPtr = Marshal.AllocHGlobal(m_recipients.Count * size);            int ptr = (int)intPtr;
                foreach (MapiRecipDesc mapiDesc in m_recipients)
                {
                    Marshal.StructureToPtr(mapiDesc, (IntPtr)ptr, false);
                    ptr += size;
                }            recipCount = m_recipients.Count;
                return intPtr;
            }        IntPtr GetAttachments(out int fileCount)
            {
                fileCount = 0;
                if (m_attachments == null)
                    return IntPtr.Zero;            if ((m_attachments.Count <= 0) || (m_attachments.Count > maxAttachments))
                    return IntPtr.Zero;            int size = Marshal.SizeOf(typeof(MapiFileDesc));
                IntPtr intPtr = Marshal.AllocHGlobal(m_attachments.Count * size);            MapiFileDesc mapiFileDesc = new MapiFileDesc();
                mapiFileDesc.position = -1;
                int ptr = (int)intPtr;
                
                foreach (string strAttachment in m_attachments)
                {
                    mapiFileDesc.name = Path.GetFileName(strAttachment);
                    mapiFileDesc.path = strAttachment;
                    Marshal.StructureToPtr(mapiFileDesc, (IntPtr)ptr, false);
                    ptr += size;
                }            fileCount = m_attachments.Count;
                return intPtr;
            }        void Cleanup(ref MapiMessage msg)
            {
                int size = Marshal.SizeOf(typeof(MapiRecipDesc));
                int ptr = 0;            if (msg.recips != IntPtr.Zero)
                {
                    ptr = (int)msg.recips;
                    for (int i = 0; i < msg.recipCount; i++)
                    {
                        Marshal.DestroyStructure((IntPtr)ptr, typeof(MapiRecipDesc));
                        ptr += size;
                    }
                    Marshal.FreeHGlobal(msg.recips);
                }            if (msg.files != IntPtr.Zero)
                {
                    size = Marshal.SizeOf(typeof(MapiFileDesc));                ptr = (int)msg.files;
                    for (int i = 0; i < msg.fileCount; i++)
                    {
                        Marshal.DestroyStructure((IntPtr)ptr, typeof(MapiFileDesc));
                        ptr += size;
                    }
                    Marshal.FreeHGlobal(msg.files);
                }
                
                m_recipients.Clear();
                m_attachments.Clear();
                m_lastError = 0;
            }
            
            public string GetLastError()
    {
        if (m_lastError <= 26)
        return errors[ m_lastError ];
        return "MAPI error [" + m_lastError.ToString() + "]";
    }     readonly string[] errors = new string[] {
    "OK [0]", "User abort [1]", "General MAPI failure [2]", "MAPI login failure [3]",
    "Disk full [4]", "Insufficient memory [5]", "Access denied [6]", "-unknown- [7]",
    "Too many sessions [8]", "Too many files were specified [9]", "Too many recipients were specified [10]", "A specified attachment was not found [11]",
    "Attachment open failure [12]", "Attachment write failure [13]", "Unknown recipient [14]", "Bad recipient type [15]",
    "No messages [16]", "Invalid message [17]", "Text too large [18]", "Invalid session [19]",
    "Type not supported [20]", "A recipient was specified ambiguously [21]", "Message in use [22]", "Network failure [23]",
    "Invalid edit fields [24]", "Invalid recipients [25]", "Not supported [26]" 
    };
            List<MapiRecipDesc> m_recipients = new List<MapiRecipDesc>();
            List<string> m_attachments = new List<string>();
            int m_lastError = 0;        const int MAPI_LOGON_UI = 0x00000001;
            const int MAPI_DIALOG = 0x00000008;
            const int MAPI_NEW_SESSION = 0x00000002;
            const int maxAttachments = 20;        enum HowTo{MAPI_ORIG=0, MAPI_TO, MAPI_CC, MAPI_BCC};
        }    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public class MapiMessage
        {
            public int reserved;
            public string subject;
            public string noteText;
            public string messageType;
            public string dateReceived;
            public string conversationID;
            public int flags;
            public IntPtr originator;
            public int recipCount;
            public IntPtr recips;
            public int fileCount;
            public IntPtr files;
        }    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public class MapiFileDesc
        {
            public int reserved;
            public int flags;
            public int position;
            public string path;
            public string name;
            public IntPtr type;
        }    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
        public class MapiRecipDesc
    {
        public int reserved;
        public int recipClass;
        public string name;
        public string address;
        public int eIDSize;
        public IntPtr entryID;
    }
    }
      

  8.   

    上面的这个类就是
    private void btnSend_Click(object sender, EventArgs e)
    {
    MAPI mapi = new MAPI();
    mapi.AddAttachment(@"D:\test.txt");
    mapi.SendMailPopup("subject", "body");
    }中使用的那个类, 大家可以试试.
      

  9.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using SendFileTo;
    using System.Threading;namespace WindowsApplication11
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Thread t = new Thread(Send);
                t.Start();
            }        private void Send()
            {
                MAPI mapi = new MAPI();
                mapi.AddAttachment(@"D:\test.txt");
                mapi.SendMailPopup("subject", "body");
            }
        }
    }
    我的很正常,没有出任何异常!而且主窗口没有卡死!
      

  10.   

    MAPI的类就是你贴的那个,我一个字都没改!
      

  11.   

    wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴)兄,可我这里每次都出错,你能否把你的工程发给我,我试下,谢谢[email protected]
      

  12.   

    wzd24(牧野)(衣带渐宽终不悔,为伊消得人憔悴) 你真耐心``呵呵``
      

  13.   

    把sned()放委托里试试``
    调用的时候直接`用线程调委托``
      

  14.   

    spkl1(柏芝你走吧我放不下韩红就叫我朝伟吧虽然歌唱得像学友)兄,能否帮写段调用委托的代码?
      

  15.   

    Thread t = new Thread(p);
    t.Start();
    void p()
    {
    delegate void Send_Delegate();
    Send_Delegate Send_D=new Send_Delegate(send);
    send_D();
    }
      

  16.   

    要不我给你一个思路,你另外再设计一个Form2,在Form2的里面调用这段代码。using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using SendFileTo;
    using System.Threading;namespace WindowsApplication11
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Thread t = new Thread(Send);
                t.Start();
            }        private void Send()
            {
                //MAPI mapi = new MAPI();
                //mapi.AddAttachment(@"D:\test.txt");
                //MessageBox.Show(mapi.SendMailPopup("subject", "body").ToString());
                Form2 frm = new Form2();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using SendFileTo;namespace WindowsApplication11
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();             MAPI mapi = new MAPI();
                mapi.AddAttachment(@"D:\test.txt");
                mapi.SendMailPopup("subject", "body");
           }        protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);        }
        }
    }