Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I popup the compose / create mail dialog using the user's default email client?

Tags:

c#

email

The use case is simple. At a certain point of time, I need to be able to show the user his familiar compose email dialog (Outlook or other) with

  • fields like from, to, Subject already filled up with certain application determined values.
  • The email would also have an attachment along with it.

The mail should not be sent unless the user explicitly okays it.

I did this once back in the ol' VB6 days.. can't figure out how now.. I just remember that it was quite easy.

Managed app, C#, .net 3.0+

Update#1: Yeah seems like mailto removed support for attachments (as a security risk?). I tried

You need to include ShellExecute signature as described here. All I got from this was a 5 SE_ERR_ACCESSDENIED and a 2 just for some variety

string sMailToLink = @"mailto:[email protected]?subject=Hey&body= yeah yeah yeah";
IntPtr result = ShellExecute(IntPtr.Zero, "open", sMailToLink, "", "", ShowCommands.SW_SHOWNORMAL);
Debug.Assert(result.ToInt32() > 32, "Shell Execute failed with return code " + result.ToInt32());

The same MailtoLink works perfectly with Process.Start... but as long as thou shalt not mention attachments.

System.Diagnostics.Process.Start(sMailToLink);

The other options are using the Outlook Object model to do this.. but I've been told that this requires you to add assembly references based to the exact version of Outlook installed. Also this would blow up if the user doesn't prefer MS for email.

The next option are Mapi and something called Mapi33.. Status still IN PROGRESS. Ears still open to suggestions.

like image 823
Gishu Avatar asked Jan 16 '09 11:01

Gishu


People also ask

How do I open the default mail client in C#?

But you can use mailto:[email protected] link in asp.net or you can start process in windows application with passing mailto:[email protected].

How do I set Outlook as my default email client for mailto links?

Open Outlook. On the Tools menu, click Options, and then click the Other tab. Under General, select the Make Outlook the default program for E-mail, Contacts, and Calendar check box. Click OK.

What is a default email handler?

An email handler is a program that is run when a user clicks on an email link. It is the default or configured email client or the program associated with email links.

How do I set the default mail client in Windows 10?

Windows 10 In the search bar or search icon on the bottom left of the desktop, begin typing Default App Settings. Once You see the Default App Settings option, click it. Click the Mail option, then select the program you wish to make default.


1 Answers

You can create a process object and have it call "mailto:[email protected]?subject=My+New+Subject". This will cause the system to act on the mailto with its default handler, however, while you can set subjects and such this wont handle adding an attachment. I'll freely admit im not entirely sure how you'd go about forcing an attachment without writing some mail plugin.

The process code is:

System.Diagnostics.Process.Start("mailto:[email protected]?subject=My+New+Subject");
like image 180
Wolfwyrd Avatar answered Sep 28 '22 23:09

Wolfwyrd