< Summary

Information
Class: MyNet.Platform.Windows.Mail.NativeMethods
Assembly: MyNet.Platform.Windows
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Platform.Windows/Mail/NativeMethods.cs
Tag: 342_31701152804
Line coverage
0%
Covered lines: 0
Uncovered lines: 58
Coverable lines: 58
Total lines: 156
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SendMail(...)100%210%
SendMail(...)100%210%
SendMail(...)0%7280%
GetRecipient(...)100%210%
GetAttachment(...)100%210%
.ctor()100%210%
.ctor()100%210%
.ctor()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Platform.Windows/Mail/NativeMethods.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="NativeMethods.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using 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
 16namespace MyNet.Platform.Windows.Mail;
 17
 18public 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
 023    public static bool SendMail(string addr, string title) => SendMail(addr, title, string.Empty, string.Empty);
 24
 025    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    {
 029        var recipientPointer = IntPtr.Zero;
 030        var attachmentPointer = IntPtr.Zero;
 31
 32        try
 33        {
 034            recipientPointer = GetRecipient(addr);
 035            var msg = new MapiMessage
 036            {
 037                recips = recipientPointer,
 038                recipCount = 1,
 039                subject = title,
 040                noteText = body,
 041                fileCount = 0,
 042                files = IntPtr.Zero
 043            };
 44
 045            if (!string.IsNullOrEmpty(fileName))
 46            {
 047                attachmentPointer = GetAttachment(fileName);
 048                msg.files = attachmentPointer;
 049                msg.fileCount = 1;
 50            }
 51
 052            var result = MAPISendMail(IntPtr.Zero, IntPtr.Zero, msg, 0x1 | 0x8, 0);
 053            return result is <= 1 and >= 0;
 54        }
 055        catch
 56        {
 057            return false;
 58        }
 59        finally
 60        {
 061            if (recipientPointer != IntPtr.Zero)
 62            {
 063                Marshal.FreeHGlobal(recipientPointer);
 64            }
 65
 066            if (attachmentPointer != IntPtr.Zero)
 67            {
 068                Marshal.FreeHGlobal(attachmentPointer);
 69            }
 070        }
 071    }
 72
 73    private static IntPtr GetRecipient(string address)
 74    {
 075        var recipient = new MapiRecipDesc
 076        {
 077            recipClass = 1,
 078            name = address
 079        };
 80
 081        var size = Marshal.SizeOf<MapiRecipDesc>();
 082        var intPtr = Marshal.AllocHGlobal(size);
 83
 084        Marshal.StructureToPtr(recipient, intPtr, false);
 85
 086        return intPtr;
 87    }
 88
 89    private static IntPtr GetAttachment(string fileName)
 90    {
 091        var size = Marshal.SizeOf<MapiFileDesc>();
 092        var intPtr = Marshal.AllocHGlobal(size);
 93
 094        var mapiFileDesc = new MapiFileDesc
 095        {
 096            // An integer used to indicate where in the message text to render the attachment.
 097            position = -1,
 098
 099            name = System.IO.Path.GetFileName(fileName),
 0100            path = fileName
 0101        };
 0102        Marshal.StructureToPtr(mapiFileDesc, intPtr, false);
 103
 0104        return intPtr;
 105    }
 106
 107    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
 108    private sealed class MapiMessage
 109    {
 110        public int reserved;
 111
 0112        public string subject = string.Empty;
 113
 0114        public string noteText = string.Empty;
 0115        public string messageType = string.Empty;
 0116        public string dateReceived = string.Empty;
 0117        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
 0136        public string name = string.Empty;
 0137        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
 0150        public string path = string.Empty;
 151
 0152        public string name = string.Empty;
 153        public IntPtr type;
 154    }
 155}
 156