static Task PlatformComposeAsync(EmailMessage message) { var msg = default(NativeMethods.MapiMessage); try { var recipCount = (message.To is object?message.To.Count : 0) + (message.Cc is object?message.Cc.Count : 0) + (message.Bcc is object?message.Bcc.Count : 0); if (!string.IsNullOrEmpty(message.Subject)) { msg.lpszSubject = message.Subject; } if (!string.IsNullOrEmpty(message.Body)) { msg.lpszNoteText = message.Body; } // recipients if (recipCount > 0) { var recipdesclen = Marshal.SizeOf <NativeMethods.MapiRecipDescW>(); msg.lpRecips = Marshal.AllocHGlobal(recipdesclen * recipCount); msg.nRecipCount = recipCount; var currentRecip = 0; if (message.To is object) { foreach (var r in message.To) { var rd = new NativeMethods.MapiRecipDescW(r, r, NativeMethods.RecipientClass.MAPI_TO); Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false); currentRecip++; } } if (message.Cc is object) { foreach (var r in message.Cc) { var rd = new NativeMethods.MapiRecipDescW(r, r, NativeMethods.RecipientClass.MAPI_CC); Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false); currentRecip++; } } if (message.Bcc is object) { foreach (var r in message.Bcc) { var rd = new NativeMethods.MapiRecipDescW(r, r, NativeMethods.RecipientClass.MAPI_BCC); Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false); currentRecip++; } } } // attachments if (message.Attachments is object) { if (message.Attachments.Count > 0) { var fileDescLen = Marshal.SizeOf <NativeMethods.MapiFileDescW>(); msg.lpFiles = Marshal.AllocHGlobal(fileDescLen * message.Attachments.Count); msg.nFileCount = message.Attachments.Count; for (var i = 0; i < message.Attachments.Count; i++) { var f = new NativeMethods.MapiFileDescW(message.Attachments[i].FullPath, message.Attachments[i].FileName); Marshal.StructureToPtr(f, IntPtr.Add(msg.lpFiles, fileDescLen * i), false); } } } var result = NativeMethods.MAPISendMailW(IntPtr.Zero, IntPtr.Zero, ref msg, 0xd, 0); } finally { FreeMapiMessage(msg); } return(Task.CompletedTask); }
private static void ShowComposeNewEmailImpl(EmailMessage message) { bool useAnsi = global::System.Environment.OSVersion.Version < new Version(6, 2); int recipCount = message.To.Count + message.CC.Count + message.Bcc.Count; NativeMethods.MapiMessage msg = new NativeMethods.MapiMessage(); if (useAnsi) { if (!string.IsNullOrEmpty(message.Subject)) { msg.lpszSubject = Marshal.StringToHGlobalAnsi(message.Subject); } if (!string.IsNullOrEmpty(message.Body)) { msg.lpszNoteText = Marshal.StringToHGlobalAnsi(message.Body); } } else { if (!string.IsNullOrEmpty(message.Subject)) { msg.lpszSubject = Marshal.StringToHGlobalUni(message.Subject); } if (!string.IsNullOrEmpty(message.Body)) { msg.lpszNoteText = Marshal.StringToHGlobalUni(message.Body); } } // recipients if (recipCount > 0) { int recipdesclen = Marshal.SizeOf <NativeMethods.MapiRecipDesc>(); msg.lpRecips = Marshal.AllocHGlobal(recipdesclen * recipCount); msg.nRecipCount = recipCount; int currentRecip = 0; foreach (EmailRecipient r in message.To) { if (useAnsi) { var rd = new NativeMethods.MapiRecipDesc(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_TO); Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false); } else { var rd = new NativeMethods.MapiRecipDescW(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_TO); Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false); } currentRecip++; } foreach (EmailRecipient r in message.CC) { if (useAnsi) { var rd = new NativeMethods.MapiRecipDesc(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_CC); Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false); } else { var rd = new NativeMethods.MapiRecipDescW(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_CC); Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false); } currentRecip++; } foreach (EmailRecipient r in message.Bcc) { if (useAnsi) { var rd = new NativeMethods.MapiRecipDesc(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_BCC); Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false); } else { var rd = new NativeMethods.MapiRecipDescW(r.Address, r.Name, NativeMethods.RecipientClass.MAPI_BCC); Marshal.StructureToPtr(rd, IntPtr.Add(msg.lpRecips, recipdesclen * currentRecip), false); } currentRecip++; } } // attachments if (message.Attachments.Count > 0) { int fileDescLen = Marshal.SizeOf <NativeMethods.MapiFileDesc>(); msg.lpFiles = Marshal.AllocHGlobal(fileDescLen * message.Attachments.Count); msg.nFileCount = message.Attachments.Count; for (int i = 0; i < message.Attachments.Count; i++) { if (useAnsi) { NativeMethods.MapiFileDesc f = new Email.EmailManager.NativeMethods.MapiFileDesc(message.Attachments[i].Data.Path, message.Attachments[i].FileName); Marshal.StructureToPtr(f, IntPtr.Add(msg.lpFiles, fileDescLen * i), false); } else { NativeMethods.MapiFileDescW f = new Email.EmailManager.NativeMethods.MapiFileDescW(message.Attachments[i].Data.Path, message.Attachments[i].FileName); Marshal.StructureToPtr(f, IntPtr.Add(msg.lpFiles, fileDescLen * i), false); } } } if (useAnsi) { uint result = NativeMethods.MAPISendMail(IntPtr.Zero, IntPtr.Zero, ref msg, 0xd, 0); } else { uint result = NativeMethods.MAPISendMailW(IntPtr.Zero, IntPtr.Zero, ref msg, 0xd, 0); } FreeMapiMessage(msg); }