A community member has associated this post with a similar question:
Problem with default email client

Only moderators can edit this content.

Opening the default e-email app

Eduardo Gomez 3,416 Reputation points
2023-01-23T23:00:17.3166667+00:00

I use this code to open the default application and attach the file.

But this code tries to open the Outlook app.

User's image

For example, my default email app is the Windows 11 main app, but some other people in window seven, or XP, might be another app

So, this code doesn't work for me

private void ShareAction(FileItem file) {
 
           string subject = string.Empty;
           string body = string.Empty;
           string recipient = string.Empty;
 
           MapiMessage msg = new()
           {
               subject = subject,
               noteText = body,
               recipCount = 1,
               recips = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MapiRecipDesc)))
           };
           Marshal.StructureToPtr(new MapiRecipDesc(recipient), msg.recips, false);
           msg.files = IntPtr.Zero;
           msg.fileCount = 0;
           int result = MAPISendMail(new IntPtr(0), new IntPtr(0), msg, 0x00000001, 0);
           if (result > 0) {
               throw new Exception("Error sending email. Error code: " + result);
           }
       }
 
       [DllImport("MAPI32.dll")]
       public 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 MapiRecipDesc {
           public int reserved;
           public int recipClass;
           public string name;
           public string address;
           public int eIDSize;
           public IntPtr entryID;
 
           public MapiRecipDesc(string recip) {
               reserved = 0;
               recipClass = 1; // MAPI_TO
               name = recip;
               address = recip;
               eIDSize = 0;
               entryID = IntPtr.Zero;
           }
 
       }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 81,831 Reputation points
    2023-01-24T07:29:41.76+00:00

    You must load dynamically the right MAPI32.DLL, like the LoadDefaultMailProvider in the SDK

    Otherwise, you can test the code with IDataObject I just posted in this thread : Opening the default e-email app