/// <summary> /// Sends a e-mail using default Windows mail client /// </summary> /// <param name="Subject"></param> /// Message subject /// <param name="Body"></param> /// Message body /// <param name="Originator"></param> /// Source user mail /// <param name="OriginatorName"></param> /// Source user name /// <param name="Recipient"></param> /// Destination e-mail address, multiple addresses separated by semicolon /// <param name="RecipientName"></param> /// Destination name,multiple destination names separated by semicolon /// <param name="file"></param> /// File name the destination user will see, multiple names separated by semicolon /// <param name="originalfile"></param> /// Full path to the file to send, multiple filesnames separated by semicolon /// <param name="showmessage"></param> /// Show the message to the user before sending it /// <returns></returns> public static uint SendMail(string Subject, string Body, string Originator, string OriginatorName, string Recipient, string RecipientName, string file, string originalfile, bool showmessage) { uint ulResult = 0; IntPtr hSession = IntPtr.Zero; // uint ulFlags = MAPI.MAPI_LOGON_UI | MAPI.MAPI_NEW_SESSION; ulResult = MAPI.Logon(IntPtr.Zero, null, null, 0, 0, ref hSession); if (Originator == null) { Originator = ""; } if (Recipient == null) { Recipient = ""; } if (file == null) { file = ""; } if (originalfile == null) { file = ""; } if (originalfile == "") { file = ""; } if (ulResult == MAPI.SUCCESS_SUCCESS) { MapiMessage message = new MapiMessage(); message.lpszSubject = Subject; message.lpszNoteText = Body; try { if (Originator == "") { message.lpOriginator = IntPtr.Zero; } else { message.lpOriginator = AllocOrigin(OriginatorName, Originator); } if (Recipient == "") { message.lpRecips = IntPtr.Zero; message.nRecipCount = 0; } else { uint recipcount = 1; message.lpRecips = AllocRecips(RecipientName, Recipient, ref recipcount); message.nRecipCount = recipcount; } if (file == "") { message.lpFiles = IntPtr.Zero; message.nFileCount = 0; } else { uint filecount = 1; message.lpFiles = AllocFiles(file, originalfile, ref filecount); message.nFileCount = filecount; } UInt32 sendFlags = 0; if (showmessage) { sendFlags = MAPI_DIALOG; } ulResult = MAPI.MAPISendMail(hSession, IntPtr.Zero, message, sendFlags, 0); if (ulResult == MAPI.SUCCESS_SUCCESS) { ulResult = MAPI.Logoff(hSession, IntPtr.Zero, 0, 0); } else { MAPI.Logoff(hSession, IntPtr.Zero, 0, 0); } } finally { if (message.lpOriginator != IntPtr.Zero) { Marshal.FreeHGlobal(message.lpOriginator); } if (message.lpRecips != IntPtr.Zero) { Marshal.FreeHGlobal(message.lpRecips); } if (message.lpFiles != IntPtr.Zero) { Marshal.FreeHGlobal(message.lpFiles); } } } return(ulResult); }
private static extern UInt32 MAPISendMail(IntPtr lhSession, IntPtr ulUIParam, MapiMessage lpMessage, UInt32 flFlags, UInt32 ulReserved);