int SendMail(string subject, string body, int how) { IntPtr vsWindow; IVsUIShell shell = VsAppShell.Current.GetGlobalService<IVsUIShell>(typeof(SVsUIShell)); shell.GetDialogOwnerHwnd(out vsWindow); MapiMessage msg = new MapiMessage(); msg.subject = subject; msg.noteText = body; msg.recips = GetRecipients(out msg.recipCount); msg.files = GetAttachments(out msg.fileCount); ThreadParam p = new ThreadParam() { Message = msg, VsWindow = vsWindow, MapiFlags = how }; bool success = false; Thread t = null; try { _completed.Reset(); t = new Thread(ThreadProc, 8192); t.Start(p); success = _completed.Wait(5000); } catch (Exception) { } if (!success) { if (t != null) { t.Abort(); } } Cleanup(ref msg); return _result; }
static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv);
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); } _recipients.Clear(); _attachments.Clear(); _result = (int)MapiErrorCode.MAPI_SUCCESS; }