How to open new email with attachment in Windows 1

2020-05-19 07:26发布

问题:

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

回答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)));


回答2:

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.