15

I am trying to add a feature to my C# / .Net app for a user to email a file. When a user has Outlook installed, I can successfully use the Outlook interop APIs to do exactly what I want. However on a new Windows 10 install, I cannot work out how to open an email with an attachment in the default Mail app, which is from the Windows Store.

I have tried:

  1. Using EML files, as per https://stackoverflow.com/a/25586282/2102158

    • The Mail app does not register itself to open EML files
  2. Using the MAPI32.dll etc. (I used the code from https://github.com/metageek-llc/inSSIDer-2/blob/master/MetaScanner/UnhandledException/MapiMailMessage.cs)

    • A dialog box pops up saying there is no email program registered. It seems the mail app does not interact with MAPI
  3. Using mailto: links.

    • The mail program opens, but it does not respect Attachment= or Attach= parameters

Also

  • Windows.ApplicationModel.Email.EmailMessage seems to be only availble on phones.

  • I do not want to use SMTP to send the message server side.

  • I also tried the MS-UNISTORE_EMAIL: and OUTLOOKMAIL: url schemes, which are associated to the Mail app, they seemed to behave the same as mailto:

  • There does not seem to be any way to start the Mail app from the command line

Community
  • 1
  • 1
Jon N
  • 1,439
  • 1
  • 18
  • 29

2 Answers2

1

Try this:

a href='mailto:yourname@domain.com?Subject=yoursubject&Body=yourbody&Attachment=file path '

Or try by using file upload to attach the file in mail:

Msg.Attachments.Add(new Attachment(FileUpload1.FileContent, System.IO.Path.GetFileName(FileUpload1.FileName)));
anothernode
  • 5,100
  • 13
  • 43
  • 62
Antony Raj
  • 71
  • 10
0

Please try the following example

 private async void SendEmailButton_Click(object sender, RoutedEventArgs e)
        {
            EmailMessage emailMessage = new EmailMessage();
            emailMessage.To.Add(new EmailRecipient("***@***.com"));
            string messageBody = "Hello World";
            emailMessage.Body = messageBody;
            StorageFolder MyFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFile attachmentFile =await MyFolder.GetFileAsync("MyTestFile.txt");
            if (attachmentFile != null)
            {
                var stream = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(attachmentFile);
                var attachment = new Windows.ApplicationModel.Email.EmailAttachment(
                         attachmentFile.Name,
                         stream);
                emailMessage.Attachments.Add(attachment);
            }
            await EmailManager.ShowComposeNewEmailAsync(emailMessage);           
        }

The ShowComposeNewEmailAsny(...) part is the magic part.

STORM
  • 4,005
  • 11
  • 49
  • 98
  • Windows.ApplicationModel.Email.EmailAttachment etc is available on windows 10 (not phone) now? – Jon N Jul 17 '17 at 22:37
  • ? I do not understand. I think you are using Windows 10? – STORM Jul 18 '17 at 07:28
  • yeah windows 10 desktop. When I asked the question (almost 2 years ago) classes such as " Windows.ApplicationModel.Email.EmailAttachment" - I think all of the Windows.ApplicationModel.Email namespace, were only available on the Windows Phone version of windows. – Jon N Jul 19 '17 at 05:29
  • I had the same need once and I could not find a way so I used a form prompted to gather the customizable content to put into the body and subject and the name of attachment as weel. Once the user adjusted/confirmed the text I used smtp to send it, due to being windows mail synching the sent folder from the mail server the final result was to have message sent and view it within the sent folder on windows Mail. Further info at: https://code.msdn.microsoft.com/windowsdesktop/Create-and-send-an-email-d3b4765b/sourcecode?fileId=159161&pathId=260977603 – A. Lion Aug 21 '18 at 10:07
  • 1
    Not supported in desktop (non-UWP) apps. – Simon Buchan Aug 06 '20 at 04:32