locked
[UWP]Windows 10 C# Universal Windows App Email with Attachment RRS feed

  • Question

  • I have a Windows 10 C# Universal Windows App and would like to send an email with a simple text file attachment.

    I am using Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync to create the message and cause the Email client (Windows Live Mail under Windows 10 Pro) to be displayed.

    The message is created OK with a body, title and recipient. The problem is that the attachment is not attached.

    The file to be attached is created ok and added to the message using this code:

    var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();

    emailMessage.Body = messageBody;

    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);
    }

    The message is created OK, the attachment is created and added to the emailMessage.Attachments List.

    But then the attachment just disappears when the Windows Live email client is displayed. No error messages, no exceptions, the attachment is just not attached to the email.

    Any thoughts about why the attachment might not be attaching would be appreciated.

    Thanks.


    Friday, September 4, 2015 8:44 PM

Answers

  • Locking this thread due to spam. 10 content-free bumps in a few hours is not helpful. 

    If you want to request or vote on feature requests please do so at http://wpdev.uservoice.com

    If Outlook crashes then that is an Outlook bug separate from this thread. Please report it through Outlook's Feedback menu

    As noted, EmailMessage is not plumbed through to Outlook 2016. There is no UWP compliant way to get there directly. As Andy points out, there are ways for side-loaded or full-trust apps to call the Office integration API from outside of the UWP context.


    Tuesday, October 3, 2017 6:30 PM

All replies

  • Hi BrianB15,

    Welcome to the Developing Universal Windows apps forum!

    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools

    >>No error messages, no exceptions, the attachment is just not attached to the email.

    I wonder if there is something wrong about the definition of the attachmentFile in your code, since you do not post all your code here, so I have created the following example about how to send email with EmailAttachment in UWP app, please try to check it and make sure that you do not miss something:

     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 result:
    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Monday, September 7, 2015 7:55 AM
  • Amy, thanks for the reply.

    I just used your exact code and the problem remains - no attachment on the created email.

    I am guessing the problem is related to the fact that I am using the Windows Live email client. I might be able to get it working with the default Windows mail client but I would still like to be able to work with other clients that users of my App might have installed. Other than the missing attachment it works ok, the client pops up with the correct recipient and email body.

    Thursday, September 10, 2015 4:01 PM
  • Just a quick note to confirm the previous guess.

    The attachment works correctly if I set the default mail client to Windows 10 Mail.

    The attachment does not work if the default email client is set to Windows Live Mail. The Windows Live Mail client is started and the email is created but the attachment just disappears. This is a problem for any users of the App who use Windows Live Mail.

    Friday, September 11, 2015 2:36 PM
  • Hi BrianB15,

    Since this issue is related to the Windows Live Mail, please try to post your question in this forum:
    https://social.microsoft.com/Forums/en-US/home?category=windowslive .

    Thanks for your understanding.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 15, 2015 9:29 AM
  • I have the same problem using Outlook 2016. So the problem is not just Live mail and shouldn't be disregarded as a mail issue.

    This is pretty serious. Please dig deeper!

    Thanks,

    Gerald


    Gerald Maffeo

    Friday, October 16, 2015 12:00 AM
  • I have the same issue with Outlook 2013 running on Windows 10. It works correctly in the Win10 Mail client. This is same code running on same machine.
    Wednesday, November 11, 2015 10:26 PM
  • So continuing the problem posted above, the attachment is lost when the email client is not the standard supplied with Windows 10. Because with the standard client, my email which is created programmatically gets an attachment. But if I use a different client, let's say Outlook 2016, then the attachment will not appear at all. The same goes for Outlook 2013, Windows Live Mail, and possibly any and all other email clients, with the exception of Windows 10 Mail. I too built my email programmatically as is presented above. Which works fine with Windows 10 Mail, so that is NOT the issue, the issue is that for some reason no attachments can be added to any and all other email clients. You can understand that this might be really big issue, and we don't want clients to miss their attachments which contains invoices and etc. So please fix this issue...

    EDIT: Thank you, Rob Caplan, for merging and reopening this question. :)

    Thursday, February 25, 2016 3:23 PM
  • Here is what I have figured out so far in my personal experimentation:

    Attachments will NOT work on "win32" email clients, eg. Outlook 2010/2016.

    Attachments DO work on Phone10 (and its version of Outlook).

    Body text is truncated to <2k on Win32-Outlook and Phone10, even when you use the stream body API.

    (I have not tried Windows 10 Mail myself)

    HTH.

    Wednesday, March 2, 2016 9:06 PM
  • What are the plans to fix this issue?  I can't give my users the ability to "send results" if they happen to use Outlook on their device.  Outlook is pushed as the default email client for many people and this makes fixing the functionality critical.


    Friday, April 1, 2016 1:28 AM
  • Well I am going to copy the desired body to the clipboard, then fire up the mail with a body of "Press Paste Now!". I can think of no better way, given these inexcusable IMHO limitations.
    Friday, April 1, 2016 10:49 PM
  • Andy, this is a known issue, and yes I don't buy the reasons why it doesn't work - especially for 2016.

    http://pauliom.wordpress.com

    Saturday, April 2, 2016 8:31 PM
  • I have a Stream how to attack Stream in EmailMessage. I tried convert Stream to IRandomAccessStream and then I create IRandomAccessStreamReference from IRandomAccessStream. But it not working.

     public async void ComposeEmail(string recipient, string contain, Stream attactment)
            {
                EmailMessage emailMessage = new EmailMessage();
                emailMessage.To.Add(new EmailRecipient(recipient));
                emailMessage.Body = contain;
                if (attactment != null)
                {
                    IRandomAccessStream randomAccessStream = attactment.AsRandomAccessStream();
                    var streamReference = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromStream(randomAccessStream);
                    var Attachment = new Windows.ApplicationModel.Email.EmailAttachment(
                        "sharebyemail.html", streamReference);
                    emailMessage.Attachments.Add(Attachment);
                }
                await EmailManager.ShowComposeNewEmailAsync(emailMessage);
            }

    Wednesday, May 25, 2016 6:58 AM
  • Hi,

    I tried lot , but It's not working in my application.

    It is only opening chrome browser with new windows.

    Please help me.

     Thanks

    Thursday, May 26, 2016 7:22 AM
  • Hi @salgarsb  it is opening in chrome browser, because the default mail client app is either not set or set as chrome. Just change default mail client app to outlook or the mailApp from windows and it should all work ..however, dont expect attachments to work when attaching to outlook.
    Monday, October 3, 2016 9:27 PM
  • Has there been any resolve to this issue.  For UWP to work, they have to have all the moving parts working.  The commitment level with Microsoft is a bit frustrating.  This is over a year old and I'm assuming there is no fix yet?
    Monday, November 7, 2016 2:41 PM
  • I have been waiting a year for this to be fixed. To say it is a 'bit frustrating' is an understatement!  Microsoft, we need some direction here!!  Please give us some information here, workaround, anything.

    Thursday, January 5, 2017 4:17 PM
  • I'm developing UWP and I would like to send an email with a file attachment.  

    private async Task ComposeEmail(Windows.ApplicationModel.Contacts.Contact recipient,
        string messageBody,
        StorageFile attachmentFile
    )
    {
        var emailMessage = new Windows.ApplicationModel.Email.EmailMessage();
        emailMessage.Body = messageBody;
        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);
        }
        var email = recipient.Emails.FirstOrDefault<Windows.ApplicationModel.Contacts.ContactEmail>();
        if (email != null)
        {
            var emailRecipient = new Windows.ApplicationModel.Email.EmailRecipient(email.Address);
            emailMessage.To.Add(emailRecipient);
        }
        await Windows.ApplicationModel.Email.EmailManager.ShowComposeNewEmailAsync(emailMessage);
    }

    The attachment works correctly if I set the default mail client to Windows 10 Mail, but I need OUTLOOK. In Outlook the message is created OK with a body, title and recipient. The problem is that the attachment is not attached.

    Any solution?

    Thanks.


    • Edited by David_FF Wednesday, February 8, 2017 7:17 AM title tag
    • Merged by David_FF Thursday, March 2, 2017 3:16 AM same issue
    Tuesday, February 7, 2017 10:24 AM
  • Hi Miquel,

    As Alan said in the following thread, it is a known issue and email attachment with ShowComposeNewEmailAsync only works on mail store app at this time.
    http://stackoverflow.com/questions/33053358/sending-email-attachments-via-uwp-emailmanager-not-working 

    Best Regards,
    David

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Proposed as answer by David_FF Thursday, March 2, 2017 2:58 AM
    • Edited by David_FF Thursday, March 2, 2017 3:27 AM
    Wednesday, February 8, 2017 2:12 PM
  • Same here ... trying attach a TextFile to an email, but if Outlook 2016 ist Standard Mail Client, the Attachment gets lost...

    So, answer ist from david, that says, this only works with Mail Store Apps... exists other methodes for create an email with attachment?without using ShowComposeNewEmailAsync ?

    Thanks.

    Thursday, March 30, 2017 6:40 PM
  • I have it on very good authority that my employer will soon have a blog post describing how to do email with attachments and HTML formatting from a UWP app. It wont use the API we have all been struggling with here, because it doesn't work with win32 email clients, as we have figured out the hard way. Instead it will use the Office interop assemblies to go "straight to" Outlook.
    Tuesday, October 3, 2017 2:31 AM
  • I am experiencing same issue with Outlook 2016 (but any outlook should work really but it is not).

    If my Outlook is started, New Email dialog opens, recipient, subject and body is added but there is no attachment.

    If my Outlook is not started, it is started, New Email dialog opens, recipient, subject, body is added, then Outlook crashes few seconds later.

    Event logger shows

    Faulting application name: OUTLOOK.EXE, version: 16.0.8431.2079, time stamp: 0x59b2598a
    Faulting module name: clr.dll, version: 4.7.2110.0, time stamp: 0x597bd742
    Exception code: 0xc00000fd
    Fault offset: 0x00451a8d
    Faulting process id: 0x3258
    Faulting application start time: 0x01d33c66c2757e75
    Faulting application path: C:\Program Files (x86)\Microsoft Office\Root\Office16\OUTLOOK.EXE
    Faulting module path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Report Id: 63be9da0-5158-4964-94f3-4fd5d0fd0a64
    Faulting package full name: 
    Faulting package-relative application ID: 


    -- d.

    Tuesday, October 3, 2017 4:53 PM
  • @David_FF  It has been known issue for 2 years now at least??  It is still the same identical problem happening and Outlook will even crash.

    Do you really think our customers will change their emailing system because your framework has so many issues?

    How do you see it looks like for companies developing software telling their customers, "hey, we develop using Microsoft technology and their own technology does not work with their own technology, it is not our fault."?

    Can't you at least update documentation to say how it works and what it does not work with like this one here https://docs.microsoft.com/en-us/windows/uwp/contacts-and-calendar/sending-email


    -- d.


    • Edited by _db_ Tuesday, October 3, 2017 6:01 PM
    Tuesday, October 3, 2017 4:57 PM
  • Locking this thread due to spam. 10 content-free bumps in a few hours is not helpful. 

    If you want to request or vote on feature requests please do so at http://wpdev.uservoice.com

    If Outlook crashes then that is an Outlook bug separate from this thread. Please report it through Outlook's Feedback menu

    As noted, EmailMessage is not plumbed through to Outlook 2016. There is no UWP compliant way to get there directly. As Andy points out, there are ways for side-loaded or full-trust apps to call the Office integration API from outside of the UWP context.


    Tuesday, October 3, 2017 6:30 PM