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]")
                  .Subject("Test")
                  .Body("Hello there!")
                  //.WithAttachment(String FilePath, string ContentType)
                  .Build();
                emailMessenger.SendEmail(email);
            }

        }

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).

Answers

  • louwholouwho USMember ✭✭✭

    Fairly now at this myself, but read this...https://social.msdn.microsoft.com/Forums/azure/en-US/5a539ff6-ce81-4f05-83a0-8bf430d827f8/uwpwindows-10-c-universal-windows-app-email-with-attachment?forum=wpdevelop

    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.

    https://support.office.com/en-us/article/Set-up-email-in-Mail-for-Windows-10-7ff79e8b-439b-4b47-8ff9-3f9a33166c60...
    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.

    http://www.officetooltips.com/office/tips/how_to_modify_default_mail_client_in_windows_10.html. 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 http://stackoverflow.com/questions/42580752/messaging-plugin-carel-lotz-in-xamarin-pcl

    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
    };
    picker.FileTypeFilter.Add("*");
    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.