bool AddRecipient(string email, HowTo howTo) { MapiRecipDesc recipient = new MapiRecipDesc(); recipient.recipClass = (int)howTo; recipient.name = email; m_recipients.Add(recipient); return(true); }
private void DoSimpleMAPI() { IntPtr session = IntPtr.Zero; string messageID = String.Empty; MapiMessage message = new MapiMessage(); MapiRecipDesc recipient = new MapiRecipDesc(); AddinExpress.Outlook.SecurityManager securityManager2 = new AddinExpress.Outlook.SecurityManager(); tbInfo.Clear(); if (MAPILogon(0, "", "", MAPI_LOGON_UI, 0, out session) == 0) { if (rbOFF.Checked) { securityManager2.DisableSMAPIWarnings = true; } try { IntPtr mID = Marshal.AllocHGlobal(2048); if (mID != IntPtr.Zero) { try { MAPIFindNext(session, 0, "", "", MAPI_LONG_MSGID, 0, mID); IntPtr lpMessage = IntPtr.Zero; MAPIReadMail(session, 0, mID, MAPI_ENVELOPE_ONLY | MAPI_PEEK | MAPI_SUPPRESS_ATTACH, 0, ref lpMessage); if (lpMessage != IntPtr.Zero) { message = (MapiMessage)Marshal.PtrToStructure(lpMessage, typeof(MapiMessage)); recipient = (MapiRecipDesc)Marshal.PtrToStructure(message.lpOriginator, typeof(MapiRecipDesc)); string[] lines = new string[4]; lines[0] = ("*** First message in Microsoft Outlook inbox (Outlook Object Model) ***"); lines[1] = ""; lines[2] = "From: " + recipient.lpszName; lines[3] = "Subject: " + message.lpszSubject; tbInfo.Lines = lines; MAPIFreeBuffer(lpMessage); } } catch { } finally { Marshal.FreeHGlobal(mID); } } } finally { if (rbOFF.Checked) { securityManager2.DisableSMAPIWarnings = false; } MAPILogoff(session, 0, 0, 0); } } }
public MapiRecipDesc GetMapiRecipDesc() { var recipDesc = new MapiRecipDesc(); if (this._displayName == null) { recipDesc.Name = this._emailAddress; } else { recipDesc.Name = this._displayName; recipDesc.Address = this._emailAddress; } recipDesc.RecipientClass = (int)this._mailRecipientType; return recipDesc; }
/// <summary> /// Resolves a recipient name using the Simple MAPI address book.</summary> /// <param name="name"> /// Recipient name to be resolved. This name may be incomplete, misspelled, or otherwise /// ambiguous.</param> /// <returns> /// A <see cref="MapiAddress"/> holding the unambiguous address book name that is the /// closest match for the specified <paramref name="name"/>, and the corresponding default /// e-mail address.</returns> /// <exception cref="ArgumentNullOrEmptyException"> /// <paramref name="name"/> is a null reference or an empty string.</exception> /// <exception cref="MapiException"> /// <see cref="Mapi.MAPIResolveName"/> indicated an error.</exception> /// <remarks><para> /// <b>ResolveName</b> determines the unambiguous address book entry of the specified /// recipient, using the Win32 API call <see cref="Mapi.MAPIResolveName"/> which is part of /// the Simple MAPI protocol. /// </para><para> /// If no match is found or the specified <paramref name="name"/> resolves to more than one /// address book entry, the user is presented with an address book dialog and asked to /// select an entry. User cancellation generates a <see cref="MapiException"/> whose <see /// cref="MapiException.Code"/> may be <see cref="MapiException.Abort"/> but also any other /// <see cref="MapiError"/> code, depending on the MAPI server. /// </para><note type="caution"> /// Non-Microsoft e-mail clients rarely support <b>MAPIResolveName</b>. Some might report an /// error while others might return empty strings or invalid data. There is no workaround /// other than using a Microsoft program as the default e-mail client.</note></remarks> public static MapiAddress ResolveName(string name) { if (String.IsNullOrEmpty(name)) { ThrowHelper.ThrowArgumentNullOrEmptyException("name"); } // remember current working directory string currentDir = Directory.GetCurrentDirectory(); // prepare recipient structure SafeMapiHandle lpRecip = null; try { // invoke MAPIResolveName to resolve this name MapiFlags flags = MapiFlags.MAPI_DIALOG | MapiFlags.MAPI_LOGON_UI; MapiError code = Mapi.MAPIResolveName(UIntPtr.Zero, UIntPtr.Zero, name, flags, 0, out lpRecip); // check for null recipient (buggy MAPI server) if (code == MapiError.SUCCESS_SUCCESS && (lpRecip == null || lpRecip.IsInvalid)) { code = MapiError.MAPI_E_FAILURE; } // throw exception if MAPI reports failure if (code != MapiError.SUCCESS_SUCCESS) { ThrowMapiException(code); } // retrieve unmanaged memory block on success MapiRecipDesc recip = (MapiRecipDesc)lpRecip.GetMemory(0, typeof(MapiRecipDesc)); // return recipient name and address return(new MapiAddress(recip.lpszName, recip.lpszAddress)); } finally { // release unmanaged memory block if (lpRecip != null) { lpRecip.Dispose(); } // restore original working directory Directory.SetCurrentDirectory(currentDir); } }
public void CreateEmail(string subject, string body, EmailBodyFormat bodyFormat, EmailRecipient[] recipients, string[] attachments, bool addSignature) { MapiMessage msg = new MapiMessage(); msg.subject = subject; if (bodyFormat == EmailBodyFormat.Html) { body = body.Replace("<p>", "\r\n\r\n"); body = body.Replace("<P>", "\r\n\r\n"); body = body.Replace("<br>", "\r\n"); body = body.Replace("<BR>", "\r\n"); body = HtmlTools.StripHTML(body); body = HtmlTools.SafeHtmlDecode(body); } if (addSignature && Core.SettingStore.ReadBool("MailFormat", "UseSignature", false)) { body += "\r\n"; body += Core.SettingStore.ReadString("MailFormat", "Signature"); } msg.noteText = body; if (recipients != null && recipients.Length > 0) { ArrayList recipArray = new ArrayList(); foreach (EmailRecipient recipient in recipients) { MapiRecipDesc recip = new MapiRecipDesc(); recip.recipClass = MapiTO; recip.name = recipient.Name; recip.address = BuildAddressWithType(recipient.EmailAddress); recipArray.Add(recip); } msg.recips = AllocRecips(recipArray); msg.recipCount = recipArray.Count; } if (attachments != null && attachments.Length > 0) { msg.files = AllocAttachs(attachments); msg.fileCount = attachments.Length; } try { int rc = MAPISendMail(IntPtr.Zero, Core.MainWindow.Handle, msg, MAPI_DIALOG, 0); if (rc != 0 && rc != MAPI_E_USER_ABORT) { ReportError(rc); } } catch (Exception) { MessageBox.Show(Core.MainWindow, "Failed to send e-mail (unknown error)", "Send E-mail", MessageBoxButtons.OK); } if (msg.recips != IntPtr.Zero) { int runptr = (int)msg.recips; for (int i = 0; i < msg.recipCount; i++) { Marshal.DestroyStructure((IntPtr)runptr, typeof(MapiRecipDesc)); runptr += Marshal.SizeOf(typeof(MapiRecipDesc)); } Marshal.FreeHGlobal(msg.recips); } if (msg.files != IntPtr.Zero) { Type ftype = typeof(MapiFileDesc); int fsize = Marshal.SizeOf(ftype); int runptr = (int)msg.files; for (int i = 0; i < msg.fileCount; i++) { Marshal.DestroyStructure((IntPtr)runptr, ftype); runptr += fsize; } Marshal.FreeHGlobal(msg.files); } }
bool AddRecipient(string email, HowTo howTo) { MapiRecipDesc recipient = new MapiRecipDesc(); recipient.recipClass = (int)howTo; recipient.name = email; m_recipients.Add(recipient); return true; }
/// <summary> /// Allows the user to select recipients from the Simple MAPI address book.</summary> /// <returns> /// An <see cref="Array"/> of <see cref="MapiAddress"/> instances holding the selected /// address book names and the corresponding e-mail addresses.</returns> /// <exception cref="MapiException"> /// <see cref="Mapi.MAPIAddress"/> indicated an error.</exception> /// <remarks><para> /// <b>Address</b> shows a dialog allowing the user to select e-mail recipients from an /// address book, using the Win32 API call <see cref="Mapi.MAPIAddress"/> which is part of /// the Simple MAPI protocol. Only the "To" recipient type is available. /// </para><para> /// <b>Address</b> returns an empty array if the user cancelled the dialog or did not select /// any recipients. No <see cref="MapiException"/> is generated in this case. /// </para><note type="caution"> /// <para>Non-Microsoft e-mail clients rarely support <b>MAPIAddress</b>. Some might report /// an error while others might show a nonfunctional dialog or return invalid data. There is /// no workaround other than using a Microsoft program as the default e-mail client. /// </para><para> /// Also note that Simple MAPI does not specify the order in which <b>MAPIAddress</b> /// returns the selected recipients. <b>Address</b> maintains the selection order for /// Outlook Express, but the order may be reversed or completely arbitrary for other e-mail /// clients.</para></note></remarks> public static MapiAddress[] Address() { // remember current working directory string currentDir = Directory.GetCurrentDirectory(); // prepare recipient list data uint nNewRecips = 0; SafeMapiHandle lpNewRecips = null; try { // invoke MAPIAddress to create a recipient list MapiFlags flags = MapiFlags.MAPI_LOGON_UI; MapiError code = Mapi.MAPIAddress(UIntPtr.Zero, UIntPtr.Zero, null, 1, null, 0, IntPtr.Zero, flags, 0, out nNewRecips, out lpNewRecips); // check for user cancellation if (code == MapiError.MAPI_E_USER_ABORT) { return(new MapiAddress[0]); } // check for null recipient list (buggy MAPI server) if (code == MapiError.SUCCESS_SUCCESS && nNewRecips > 0 && (lpNewRecips == null || lpNewRecips.IsInvalid)) { code = MapiError.MAPI_E_FAILURE; } // throw exception if MAPI reports failure if (code != MapiError.SUCCESS_SUCCESS) { ThrowMapiException(code); } // create (possibly empty) output recipient array MapiAddress[] entries = new MapiAddress[nNewRecips]; if (nNewRecips == 0) { return(entries); } // prepare managed recipient object int size = Marshal.SizeOf(typeof(MapiRecipDesc)); MapiRecipDesc recip = new MapiRecipDesc(); // retrieve unmanaged memory blocks for (int i = 0; i < nNewRecips; i++) { // copy current array element to managed memory lpNewRecips.GetMemory(i * size, recip); // HACK: Outlook Express inverts the order of selected entries. // We re-invert the order since OE is the most likely client. int index = (int)(nNewRecips - i - 1); // store entry name and address entries[index] = new MapiAddress(recip.lpszName, recip.lpszAddress); } return(entries); } finally { // release unmanaged memory block if (lpNewRecips != null) { lpNewRecips.Dispose(); } // restore original working directory Directory.SetCurrentDirectory(currentDir); } }
/// <summary> /// Creates and sends an e-mail message using the Simple MAPI protocol.</summary> /// <param name="subject"> /// The subject line of the e-mail message.</param> /// <param name="noteText"> /// The text of the e-mail message.</param> /// <param name="recipients"> /// An <see cref="Array"/> of <see cref="MapiAddress"/> instances holding the display names /// and SMTP addresses of all message recipients. The protocol identifier "smtp:" is /// automatically prepended to any non-empty addresses without this prefix.</param> /// <param name="attachments"> /// An <see cref="Array"/> of <see cref="MapiAddress"/> instances holding the display names /// and fully qualified local file paths of any attachment files sent to the <paramref /// name="recipients"/>.</param> /// <exception cref="MapiException"> /// <see cref="Mapi.MAPISendMail"/> indicated an error.</exception> /// <remarks><para> /// <b>SendMail</b> creates and sends an e-mail message with optional file attachments, /// using the Win32 API call <see cref="Mapi.MAPISendMail"/> which is part of the Simple /// MAPI protocol. The originator is left undefined which will cause Simple MAPI to assert /// the user’s default e-mail account as the originator. /// </para><para> /// The <paramref name="subject"/> and <paramref name="noteText"/> parameters may be a null /// reference or an empty string to leave the corresponding field blank. The <paramref /// name="recipients"/> and <paramref name="attachments"/> parameters may be null references /// or empty arrays to create a message without recipients or file attachments, /// respectively. /// </para><para> /// The e-mail message is presented to the user who can choose to edit (filling in any blank /// fields or adding text as desired), send, or cancel the message. User cancellation /// generates a <see cref="MapiException"/> whose <see cref="MapiException.Code"/> is <see /// cref="MapiException.Abort"/>.</para></remarks> public static void SendMail(string subject, string noteText, MapiAddress[] recipients, MapiAddress[] attachments) { // remember current working directory string currentDir = Directory.GetCurrentDirectory(); // construct MAPI message descriptor MapiMessage message = new MapiMessage(); message.lpszSubject = subject; message.lpszNoteText = noteText; try { // create any specified recipients if (recipients != null && recipients.Length > 0) { // store count of recipient descriptors int count = recipients.Length; message.nRecipCount = (uint)count; // allocate memory for recipient descriptors int size = Marshal.SizeOf(typeof(MapiRecipDesc)); message.lpRecips.AllocateHandle(count * size); // construct recipient descriptors MapiRecipDesc recip = new MapiRecipDesc(); for (int i = 0; i < count; i++) { // prepend "smtp:" to address if not present string address = recipients[i].Address; if (!String.IsNullOrEmpty(address) && !address.StartsWith("smtp:", StringComparison.OrdinalIgnoreCase)) { address = "smtp:" + address; } // create MAPI recipient descriptor recip.ulRecipClass = MapiRecipClass.MAPI_TO; recip.lpszName = recipients[i].Name; recip.lpszAddress = address; // copy recipient descriptor to unmanaged memory message.lpRecips.SetMemory(recip, i * size, false); } } // create any specified attachments if (attachments != null && attachments.Length > 0) { // store count of attachment descriptors int count = attachments.Length; message.nFileCount = (uint)count; // allocate memory for attachment descriptors int size = Marshal.SizeOf(typeof(MapiFileDesc)); message.lpFiles.AllocateHandle(count * size); // construct attachment descriptors MapiFileDesc fileDesc = new MapiFileDesc(); for (int i = 0; i < count; i++) { // create MAPI file attachment descriptor fileDesc.lpszFileName = attachments[i].Name; fileDesc.lpszPathName = attachments[i].Address; fileDesc.nPosition = 0xffffffff; // don’t embed files // copy attachment descriptor to unmanaged memory message.lpFiles.SetMemory(fileDesc, i * size, false); } } // invoke MAPISendMail to deliver this message MapiFlags flags = MapiFlags.MAPI_DIALOG | MapiFlags.MAPI_LOGON_UI; MapiError code = Mapi.MAPISendMail(UIntPtr.Zero, UIntPtr.Zero, message, flags, 0); // throw exception if MAPI reports failure if (code != MapiError.SUCCESS_SUCCESS) { ThrowMapiException(code); } } finally { // release unmanaged memory blocks message.Dispose(); // restore original working directory Directory.SetCurrentDirectory(currentDir); } }