Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My post is not an answer to your questions, but provides a solution (if you like it).<br> In the past I've used this class (mapi compliant).<br> This class doesn't use native <code>SmtpClient</code> class (that sends an email but doesn't interact with your default mailing app) but is able to invoke your email application and popup email window (filled with every detail you need) so your customer can modify it as needed.<br> More, email sent remains is "sent email" folder...</p> <pre><code>enum HowTo { MAPI_ORIG = 0, MAPI_TO, MAPI_CC, MAPI_BCC }; public class MAPI { [DllImport("MAPI32.DLL")] static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv); [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; } List&lt;MapiRecipDesc&gt; m_recipients = new List&lt;MapiRecipDesc&gt;(); List&lt;string&gt; m_attachments = new List&lt;string&gt;(); private int m_lastError = 0; private const int MAPI_LOGON_UI = 0x00000001; private const int MAPI_DIALOG = 0x00000008; private const int maxAttachments = 20; #region Private methods private bool AddRecipients(HowTo howTo, params string[] emails) { bool ret = true; foreach (string email in emails) { try { MapiRecipDesc recipient = new MapiRecipDesc(); recipient.recipClass = (int)howTo; recipient.name = email; m_recipients.Add(recipient); } catch { ret = false; } } return ret; } private 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 &gt; 1) MessageBox.Show("MAPISendMail failed! " + LastError, "MAPISendMail"); Cleanup(ref msg); return m_lastError; } private 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; } private IntPtr GetAttachments(out int fileCount) { fileCount = 0; if (m_attachments == null) return IntPtr.Zero; if ((m_attachments.Count &lt;= 0) || (m_attachments.Count &gt; 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; } private 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 &lt; 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 &lt; msg.fileCount; i++) { Marshal.DestroyStructure((IntPtr)ptr, typeof(MapiFileDesc)); ptr += size; } Marshal.FreeHGlobal(msg.files); } m_recipients.Clear(); m_attachments.Clear(); m_lastError = 0; } #endregion #region Public methods public bool AddTo(params string[] emails) { return AddRecipients(HowTo.MAPI_TO, emails); } public bool AddCC(params string[] emails) { return AddRecipients(HowTo.MAPI_CC, emails); } public bool AddBCC(params string[] emails) { return AddRecipients(HowTo.MAPI_BCC, emails); } 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); } public int SendMailDirect(string strSubject, string strBody) { return SendMail(strSubject, strBody, MAPI_LOGON_UI); } public string LastError { get { switch (m_lastError) { case 0: return "OK"; case 1: return "User abort [1]"; case 2: return "General MAPI failure [2]"; case 3: return "MAPI login failure [3]"; case 4: return "Disk full [4]"; case 5: return "Insufficient memory [5]"; case 6: return "Access denied [6]"; case 7: return "-unknown- [7]"; case 8: return "Too many sessions [8]"; case 9: return "Too many files were specified [9]"; case 10: return "Too many recipients were specified [10]"; case 11: return "A specified attachment was not found [11]"; case 12: return "Attachment open failure [12]"; case 13: return "Attachment write failure [13]"; case 14: return "Unknown recipient [14]"; case 15: return "Bad recipient type [15]"; case 16: return "No messages [16]"; case 17: return "Invalid message [17]"; case 18: return "Text too large [18]"; case 19: return "Invalid session [19]"; case 20: return "Type not supported [20]"; case 21: return "A recipient was specified ambiguously [21]"; case 22: return "Message in use [22]"; case 23: return "Network failure [23]"; case 24: return "Invalid edit fields [24]"; case 25: return "Invalid recipients [25]"; case 26: return "Not supported [26]"; default: return String.Format("MAPI error [{0}]", m_lastError); } } } #endregion } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload