Forum Cross Platform with Xamarin

Messaging PlugIn (Carel Lotz) in PCL?

rolfMrolfM ATMember ✭✭
private void Senden_Clicked(object sender, EventArgs e)
            var emailMessenger = CrossMessaging.Current.EmailMessenger;
            if (emailMessenger.CanSendEmail)
                var email = new EmailMessageBuilder()
                  .To("[email protected]")
                  .Body("Hello there!")
                  //.WithAttachment(String FilePath, string ContentType)


So I have this button in my PCL that should directly attach an xml file I've created into a mail and send it using the Messaging PlugIn. The problem is that .WithAttachment() is not supported in PCL so I wanted to ask if I can get around this using DependencyService and if so, how? Can I just return .WithAttachment() from the UWP class (as UWP is my target platform)? Wouldn't there be a conflict because I've read that the overload of .WithAttachment() in UWP is .WithAttachment(IStorageFile file).


  • louwholouwho USMember ✭✭✭

    Fairly now at this myself, but read this...

    Basically, on a Windows 10 'Desktop' (not targeting Win 10 phone because it has about .03 of the market)...unless you have Windows 10 mail set up, it will not work. I am going to put some text in the body of the email showing where the file to attach is, so the user can attach the file themselves. If you come up with something else, please let me know. I thought to try Gmail, but MailKit does not work in PCL.

  • rolfMrolfM ATMember ✭✭

    @louwho I will be researching this weekend. I'll let you know as soon as I know something more.

  • louwholouwho USMember ✭✭✭

    I got the attachment to work, by using Windows 10 Mail, and Gmail (Obviously you need a Gmail account). First, I set up Windows 10 Mail. In my code I am using EmailMessageBuilder().

    Access the Windows 10 Start menu. Select Mail.
    Choose the 'Google' selection, and enter name\pw to get account synched with Mail.

    When I ran my app the first time after setting up Mail, it defaulted to MS Outlook (which is what I use for my own email (connected to Comcast), and, the attachment was not there. This shows you how to change the Mail default. It was set to Outlook, I changed it to the 'Mail' option.

    Then I ran my app again, and, the attachment was there! The people that will initially be using my app, use Google\Gmail, so for now using Gmail is fine. At this point and time, I do not know if Mail\Outlook will take an attachment, please let me know if you find out that it will.

  • rolfMrolfM ATMember ✭✭

    @louwho I was finally able to find a solution on StackOverflow. Here is the link

    Nick Zhu's solution helped a lot but I had to change it a little bit to work without a FileOpenPicker. I just removed this part of the code:

    var picker = new Windows.Storage.Pickers.FileOpenPicker
    ViewMode = Windows.Storage.Pickers.PickerViewMode.List,
    SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
    var file = await picker.PickSingleFileAsync();

    and replaced it with this:

    StorageFolder sf = ApplicationData.Current.LocalFolder;
    var file = await sf.GetFileAsync("data.xml");

    Of course the file has then to be created in the app's local folder instead of the DocumentsLibrary (which unfortunately has no path (=null), as it is a virtual folder).

Sign In or Register to comment.