Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How do I send an email using Xamarin.Essentials with an attachment from a memory file?

EasyGoingPatEasyGoingPat GBMember ✭✭✭
edited September 1 in Xamarin.Forms

The subject line says it all really: how do I send an email using Xamarin.Essentials.EmailMessage with an attachment from a memory file?

I found this code on SO that does it for 'normal' .NET: [](Email File From MemoryStream). But I am not sure how to map this onto the Xamarin.Essentials.EmailMessage class because of the way the Mime type is constructed.

Can anyone offer any advice or suggestions?

Incidentally, the file I am attaching is for the foreseeable future (famous last words) text-only. So a solution or workaround that works for text would be great!

Kind wishes - Patrick

Best Answer


  • EasyGoingPatEasyGoingPat GBMember ✭✭✭


    Thank you for the link.

    Looking at your link and the code on SO, I can't easily see a way to do what I want in memory, so your way of using the cache directory is an adequate workaround.

    Because of the high level of decoupling I am trying to maintain, it isn't ideal to pass a file-path as a parameter. I suspect my code could be made much more 'correct' if I could figure out what the relationship is between a Xamarin FileBase and the standard .NET File / FileStream etc. types is, because I note that Xamarin.Essentials provides a way to create an email attachment from a FileBase object. Is it possible to do this conversion somehow?

    • Patrick
  • JarvanJarvan Member, Xamarin Team Xamurai

    I can't easily see a way to do what I want in memory, so your way of using the cache directory is an adequate workaround.

    The code of the tutorial saves the file in the cache directory. Please check my posted code, it just loads the file from the path.

    var file = Path.Combine("file_folder", "file_name");//get the file path
    message.Attachments.Add(new EmailAttachment(file));

    What is the 'memory file'? Do you mean the file which is stored in the application? In Android, it corresponds to the Internal Storage. IOS does not distinguish storage types, all files are saved in the application path。

    To access the file of Internal Storage on Android, check the tutorial:

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭


    I was thinking of the .NET type MemoryStream, which on a normal PC would probably sit in RAM, though maybe not if it got too big and was paged to disk. A MemoryStream can be made to write to or read from a FileStream: [](MemoryStream to-from FileStream).

    I was just wondering if there were a was any relationship between the FileStream type and the Xamarin FileBase type, as that is one way an email attachment can be constructed. (I'm not actually sure what a FileBase is.)

    But are you are quite correct that on a mobile device, all of these boundaries between what is RAM and what is 'disk' get blurred, and, having looked at all this, I think I was wrong: even if I did find some convoluted way to do all of this, my solution would probably be no faster and would be much more complex.

    I have reworked my code so that your caching solution is a good fit and marked it as answer.

    Thank you for your help!

    Kind wishes - Patrick

Sign In or Register to comment.