cjlotz / Xamarin.Plugins

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

Cannot add attachment on iOS #109

Open pirhano opened 5 years ago

pirhano commented 5 years ago

If you are creating an issue for a BUG please fill out this information. If you are asking a question or requesting a feature you can delete the sections below.

Failure to fill out this information will result in this issue being closed. If you post a full stack trace in a bug it will be closed, please post it to http://gist.github.com and then post the link here.

Bug Information

Version Number of Plugin: 5.2.0

Device Tested On: iPad v 12

Simulator Tested On:

Version of VS: 2017

Version of Xamarin: 4.11

Versions of other things you are using:

Steps to reproduce the Behavior

Expected Behavior

pop up email with attachment

Actual Behavior

the email setup without attachment

Code snippet

      _emailMessenger = CrossMessaging.Current.EmailMessenger;

        var email = new EmailMessageBuilder().To(to)
            .Subject(subject)
            .Body(body)
            .WithAttachment(zipPath, "application/zip")
            .Build();
        var fileEx = File.Exists(zipPath);
        if (_emailMessenger.CanSendEmail && _emailMessenger.CanSendEmailAttachments)
        {
            _emailMessenger.SendEmail(email);
        }

Screenshots

Axemasta commented 5 years ago

Firstly you should have verified the files existance before adding it to the email (are you 200% sure it exists). Secondly does the zip file have any spaces in it's name? I noticed an issue when trying to attach files with spaces in their name.

Here are examples on both platforms of valid & invalid file paths:

Valid

iOS : /var/mobile/Containers/Data/Application/9C259542-E55C-471C-A0E0-E8B1D8F638F6/Documents/TimesheetMobileLogs.txt

Android : /data/user/0/com.protirus.TimesheetApp/files/TimesheetMobileLogs.txt

Invalid

iOS : /var/mobile/Containers/Data/Application/9C259542-E55C-471C-A0E0-E8B1D8F638F6/Documents/Timesheet Mobile Logs.txt

Android : /data/user/0/com.protirus.TimesheetApp/files/Timesheet Mobile Logs.txt

I was completely unable to attach files with spaces in the name. Once I've acquired the file path I convert it to a URI and use the AbsolutePath property. Here is the method I am using to build the email.

public void OpenEmailWithAttachments(EmailFiles[] files)
{
    var filePaths = GetFilePaths(files);

    var emailer = CrossMessaging.Current.EmailMessenger;

    var email = new EmailMessageBuilder()
    .To("email@domain.com")
    .Subject(FormatEmailSubject())
    .Body(FormatEmailBody(files));

    foreach (var filePath in filePaths)
    {
        if (File.Exists(filePath.AbsolutePath))
        {
            email.WithAttachment(filePath.AbsolutePath, "file");       
        }
    }

    var builtEmail = email.Build();

    emailer.SendEmail(builtEmail);
}