| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NativeMethods.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | |
| | | 10 | | #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value |
| | | 11 | | #pragma warning disable SA1401 // FieldsMustBePrivate |
| | | 12 | | #pragma warning disable SA1202 // ElementsMustBeOrderedByAccess |
| | | 13 | | #pragma warning disable CA5392 |
| | | 14 | | |
| | | 15 | | // ReSharper disable All |
| | | 16 | | namespace MyNet.Platform.Windows.Mail; |
| | | 17 | | |
| | | 18 | | public static partial class NativeMethods |
| | | 19 | | { |
| | | 20 | | [DllImport("MAPI32.DLL")] |
| | | 21 | | private static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv); |
| | | 22 | | |
| | 0 | 23 | | public static bool SendMail(string addr, string title) => SendMail(addr, title, string.Empty, string.Empty); |
| | | 24 | | |
| | 0 | 25 | | public static bool SendMail(string addr, string title, string body) => SendMail(addr, title, body, string.Empty); |
| | | 26 | | |
| | | 27 | | public static bool SendMail(string addr, string title, string body, string fileName) |
| | | 28 | | { |
| | 0 | 29 | | var recipientPointer = IntPtr.Zero; |
| | 0 | 30 | | var attachmentPointer = IntPtr.Zero; |
| | | 31 | | |
| | | 32 | | try |
| | | 33 | | { |
| | 0 | 34 | | recipientPointer = GetRecipient(addr); |
| | 0 | 35 | | var msg = new MapiMessage |
| | 0 | 36 | | { |
| | 0 | 37 | | recips = recipientPointer, |
| | 0 | 38 | | recipCount = 1, |
| | 0 | 39 | | subject = title, |
| | 0 | 40 | | noteText = body, |
| | 0 | 41 | | fileCount = 0, |
| | 0 | 42 | | files = IntPtr.Zero |
| | 0 | 43 | | }; |
| | | 44 | | |
| | 0 | 45 | | if (!string.IsNullOrEmpty(fileName)) |
| | | 46 | | { |
| | 0 | 47 | | attachmentPointer = GetAttachment(fileName); |
| | 0 | 48 | | msg.files = attachmentPointer; |
| | 0 | 49 | | msg.fileCount = 1; |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | var result = MAPISendMail(IntPtr.Zero, IntPtr.Zero, msg, 0x1 | 0x8, 0); |
| | 0 | 53 | | return result is <= 1 and >= 0; |
| | | 54 | | } |
| | 0 | 55 | | catch |
| | | 56 | | { |
| | 0 | 57 | | return false; |
| | | 58 | | } |
| | | 59 | | finally |
| | | 60 | | { |
| | 0 | 61 | | if (recipientPointer != IntPtr.Zero) |
| | | 62 | | { |
| | 0 | 63 | | Marshal.FreeHGlobal(recipientPointer); |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | if (attachmentPointer != IntPtr.Zero) |
| | | 67 | | { |
| | 0 | 68 | | Marshal.FreeHGlobal(attachmentPointer); |
| | | 69 | | } |
| | 0 | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | private static IntPtr GetRecipient(string address) |
| | | 74 | | { |
| | 0 | 75 | | var recipient = new MapiRecipDesc |
| | 0 | 76 | | { |
| | 0 | 77 | | recipClass = 1, |
| | 0 | 78 | | name = address |
| | 0 | 79 | | }; |
| | | 80 | | |
| | 0 | 81 | | var size = Marshal.SizeOf<MapiRecipDesc>(); |
| | 0 | 82 | | var intPtr = Marshal.AllocHGlobal(size); |
| | | 83 | | |
| | 0 | 84 | | Marshal.StructureToPtr(recipient, intPtr, false); |
| | | 85 | | |
| | 0 | 86 | | return intPtr; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private static IntPtr GetAttachment(string fileName) |
| | | 90 | | { |
| | 0 | 91 | | var size = Marshal.SizeOf<MapiFileDesc>(); |
| | 0 | 92 | | var intPtr = Marshal.AllocHGlobal(size); |
| | | 93 | | |
| | 0 | 94 | | var mapiFileDesc = new MapiFileDesc |
| | 0 | 95 | | { |
| | 0 | 96 | | // An integer used to indicate where in the message text to render the attachment. |
| | 0 | 97 | | position = -1, |
| | 0 | 98 | | |
| | 0 | 99 | | name = System.IO.Path.GetFileName(fileName), |
| | 0 | 100 | | path = fileName |
| | 0 | 101 | | }; |
| | 0 | 102 | | Marshal.StructureToPtr(mapiFileDesc, intPtr, false); |
| | | 103 | | |
| | 0 | 104 | | return intPtr; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] |
| | | 108 | | private sealed class MapiMessage |
| | | 109 | | { |
| | | 110 | | public int reserved; |
| | | 111 | | |
| | 0 | 112 | | public string subject = string.Empty; |
| | | 113 | | |
| | 0 | 114 | | public string noteText = string.Empty; |
| | 0 | 115 | | public string messageType = string.Empty; |
| | 0 | 116 | | public string dateReceived = string.Empty; |
| | 0 | 117 | | public string conversationID = string.Empty; |
| | | 118 | | public int flags; |
| | | 119 | | public IntPtr originator; |
| | | 120 | | |
| | | 121 | | public int recipCount; |
| | | 122 | | |
| | | 123 | | public IntPtr recips; |
| | | 124 | | |
| | | 125 | | public int fileCount; |
| | | 126 | | public IntPtr files; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] |
| | | 130 | | private sealed class MapiRecipDesc |
| | | 131 | | { |
| | | 132 | | public int reserved; |
| | | 133 | | |
| | | 134 | | public int recipClass; |
| | | 135 | | |
| | 0 | 136 | | public string name = string.Empty; |
| | 0 | 137 | | public string address = string.Empty; |
| | | 138 | | public int eIDSize; |
| | | 139 | | public IntPtr entryID; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] |
| | | 143 | | private sealed class MapiFileDesc |
| | | 144 | | { |
| | | 145 | | public int reserved; |
| | | 146 | | public int flags; |
| | | 147 | | |
| | | 148 | | public int position; |
| | | 149 | | |
| | 0 | 150 | | public string path = string.Empty; |
| | | 151 | | |
| | 0 | 152 | | public string name = string.Empty; |
| | | 153 | | public IntPtr type; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |