cjlotz / Xamarin.Plugins

Cross platform Xamarin Plugins
MIT License
113 stars 56 forks source link

Trouble with attachments in UWP app #42

Closed MikeYeager closed 8 years ago

MikeYeager commented 8 years ago

When I use the EmailMessageBuilder().WithAttachment() method in a PCL library, the signature is string filePath, string contentType, but when I run it, I get a PlatformNotSupportedException that says, "User EmailMessageBuilder.WithAttachment(Windows.Storage.IStorageFile file). overload instead.

The file path I provided points to local storage. I don't know if that's part of the problem. Also, none of the docs mention contentType, so I set it to mime type text/csv.

My ultimate goal is to get this running on an iPad, so perhaps this is just an issue with UWP? Thank you in advance!

Mike image

image

MikeYeager commented 8 years ago

I see in the docs that this is only supported on Android and iOS, though CanSendEmailAttachments is returning true.

cjlotz commented 8 years ago

Hi Mike. Sorry for the delayed response. Had a quick look at the source code again. Attachments are supported for UWP apps but not through using that specific EmailMessageBuilder.WithAttachment overload. There wasn't a consistent cross-platform API that allowed me to add attachments across the different platforms from a PCL using a filePath and contentType (currently only iOS and Android can work with this). UWP for instance requires an IStorageFile. If you want to therefore send attachments using UWP you how to use the UWP specific WithAttachment overload mentioned in the exception (i.e EmailMessageBuilder.WithAttachment(Windows.Storage.IStorageFile file). As this is a platform specific API, if will not be visible in the PCL project but only in the UWP project itself.

MikeYeager commented 8 years ago

Thank you Carel. It now makes sense why that overload didn't show up in my PCL project.

ghlouwho commented 7 years ago

VS 2015, using Xamarin PCL, when testing the UWP project, got the error message "API not supported on platform. ", then added the following code to the UWP project...

public void SendWindowsEmail( StorageFile storageFile) { StorageFile sFile; try { /sFile = Task.Factory.StartNew(() => StorageFile.GetFileFromPathAsync(storageFile.Path).AsTask()) .Unwrap() .GetAwaiter() .GetResult(); /

            var emailTask = MessagingPlugin.EmailMessenger;

            if (!MyGlobals.canSendAttachments) //send email without an attachment
            {
                // Send simple e-mail to single receiver without attachments, CC, or BCC.
                emailTask.SendEmail("change@@me-noattachment", "Survey Check", MyGlobals.textInfo);
            }
            else //send email with attachment
            {
                var email = new EmailMessageBuilder()
                    .To("change@@me-withattachment")
                    .Subject("Survey Check Image")
                    .Body(MyGlobals.textInfo)
                    .WithAttachment(storageFile)
                    .Build();

                emailTask.SendEmail(email);           
            }
        }
        catch (Exception ex)
        { 
        System.Diagnostics.Debug.WriteLine("ex.Message: " + ex.Message); 
        }

}

The code builds, I run in debug and see that the 'storageFile' is pointing to the file in "C:\Users\louis\AppData\Local\Packages\dc4933a4-aecd-4798-b9d0-c4a67b72bfc8_tesrwss0qs3kr\LocalState ". I verify (through windows file explorer), that the file (.png) exists, and I can click on and view the image). Email comes up, but without the attachment. It does not matter if I use the 'storageFile', or the commented out 'sFile', as the attachment...the file is never attached to the email. What is going on?

ghlouwho commented 7 years ago

I am finding postings online that indicate that there are no problems with Windows 10 phone, but there are problems with Windows 10 desktop. I am using desktop.