locked
Mail attachment missing when sending with EmailManager.ShowComposeNewEmailAsync RRS feed

  • Question

  • I am porting my existing app from Silverlight for WP 8.1 to the universal platform. The app is creating an e-mail attachment, which works with the Silverlight API, but not with the universal API. The code for creating the attachment is

    // 'CreateStream' returns a MemoryStream
    using (var s = await CreateStream()) { #if WINDOWS_PHONE_APP var msg = new EmailMessage(); var i = new InMemoryRandomAccessStream(); await s.CopyToAsync(i.AsStream()); var rr = RandomAccessStreamReference.CreateFromStream(i); var att = new EmailAttachment("file.txt", rr); msg.Attachments.Add(att); await EmailManager.ShowComposeNewEmailAsync(msg); #endif // WINDOWS_PHONE_APP }

    The code seems to work, but then the following weird thing happens: The mail app opens and shows the attachment (the only thing that is different to the Silverlight version is that no file size is shown), but if I send the mail, the attachment disappears. The mail that I am receiving does not have an attachment.

    What is going wrong here?

    Sunday, November 1, 2015 4:31 PM

Answers

  • The call to i.AsStream() looks suspicious. It seems like you are could be creating a new Stream object from 'i', writing to it using s.CopyToAsync, and then tossing that new Stream object away (since you don't have a reference to it), and thus will have no data in your InMemoryRandomAccessStream. Try rewriting your code to copy the data into the InMemoryRandomAccessStream in a way that you are sure it is copied.


    Visual C++ MVP | http://bit.ly/bobtaco (website) | http://bit.ly/mikebmcl (Twitter: @mikebmcl)

    Sunday, November 1, 2015 8:34 PM
  • For the record: The following works on the universal runtime, too.

    using System.Runtime.InteropServices.WindowsRuntime;
    
    // 'CreateStream' returns a MemoryStream
    using (var s = (MemoryStream) await CreateStream())
    using (var i = new InMemoryRandomAccessStream()) {
    #if WINDOWS_PHONE_APP
        var msg = new EmailMessage();
    
        await i.WriteAsync(s.GetWindowsRuntimeBuffer());
        var rr = RandomAccessStreamReference.CreateFromStream(i);
        var att = new EmailAttachment("file.txt", rr);
        msg.Attachments.Add(att);
    
        await EmailManager.ShowComposeNewEmailAsync(msg);
    #endif // WINDOWS_PHONE_APP
    }

    Saturday, November 7, 2015 1:03 PM

All replies

  • What is your default mail tool?

    http://pauliom.wordpress.com

    Sunday, November 1, 2015 7:21 PM
  • What is your default mail tool?

    http://pauliom.wordpress.com


    Windows Phone's default mail app. The problem occurs on the device and in the simulator. Btw, I am testing on WP 8.1, not 10.
    Sunday, November 1, 2015 7:31 PM
  • The call to i.AsStream() looks suspicious. It seems like you are could be creating a new Stream object from 'i', writing to it using s.CopyToAsync, and then tossing that new Stream object away (since you don't have a reference to it), and thus will have no data in your InMemoryRandomAccessStream. Try rewriting your code to copy the data into the InMemoryRandomAccessStream in a way that you are sure it is copied.


    Visual C++ MVP | http://bit.ly/bobtaco (website) | http://bit.ly/mikebmcl (Twitter: @mikebmcl)

    Sunday, November 1, 2015 8:34 PM
  • Hi Christoph Müller,

    Welcome to the Developing Universal Windows apps forum!

    As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags

    Besides, please try to use the following code to send a Mail attachment, it works well on the Windows Phone Silverlight and Universal app:

    EmailMessage emailMessage = new EmailMessage();
                emailMessage.To.Add(new EmailRecipient("amypfang@163.com"));
                string messageBody = "Hello World";
                emailMessage.Body = messageBody;
                StorageFolder MyFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                StorageFile attachmentFile =await MyFolder.GetFileAsync("file.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);           
                }
            }

    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, November 2, 2015 2:22 AM
  • Hi Mike,

    after I did some further research, I think you are right: the extension method does not create an alias for the InMemoryRandomAccessStream. The latter is empty after the copy operation. The code was a workaround for another problem anyway. My original solution, which worked on Silverlight for WP8 was:

    var rr = RandomAccessStreamReference.CreateFromStream(s.AsRandomAccessStream());

    However, this code raises an exception in the universal runtime with the weird message "This IRandomAccessStream does not support the CloneStream method because it requires cloning and this stream does not support cloning." However, the underlying MemoyStream supports cloning and seeking. It seems I need to search for another workaround.

    Best regards,
    Christoph

    Saturday, November 7, 2015 12:55 PM
  • For the record: The following works on the universal runtime, too.

    using System.Runtime.InteropServices.WindowsRuntime;
    
    // 'CreateStream' returns a MemoryStream
    using (var s = (MemoryStream) await CreateStream())
    using (var i = new InMemoryRandomAccessStream()) {
    #if WINDOWS_PHONE_APP
        var msg = new EmailMessage();
    
        await i.WriteAsync(s.GetWindowsRuntimeBuffer());
        var rr = RandomAccessStreamReference.CreateFromStream(i);
        var att = new EmailAttachment("file.txt", rr);
        msg.Attachments.Add(att);
    
        await EmailManager.ShowComposeNewEmailAsync(msg);
    #endif // WINDOWS_PHONE_APP
    }

    Saturday, November 7, 2015 1:03 PM