Xam.Plugins.Messaging Email attachments

I'm having a problems, maybe I don't get something, but I did follow the readme instruction and added the AndroidManifest.xml entry:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="REMOVED TO POST" android:versionCode="1" android:versionName="1.0" package="com.companyname.GF_Trace" android:installLocation="auto"> <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="27" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:label="GF_Trace.Android"> <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.companyname.GF_Trace.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data> </provider> </application> </manifest>

Into the Resources, I created an xml folder that content file_pathd.xml Here I did try a few things to try to get it working:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="REMOVED TO POST"> <external-files-path name="exttempopdf" path="tempo.pdf" /> <external-path name="extpdfs" path="." /> <cache-path name="cachepdf" path="." /> <external-cache-path name="extcachepdf" path="." /> </paths>

This code reside into .NET Standard 2.0 library But the caller is inside Xamarin.Android application:
`public static bool SendEmail(string destination, string subject, string body, IEnumerable attachment_paths)
{
var emailMessenger = CrossMessaging.Current.EmailMessenger;
if (!emailMessenger.CanSendEmail)
return false;

        EmailMessageBuilder builder = new EmailMessageBuilder().To(destination).Subject(subject);

        if (emailMessenger.CanSendEmailBodyAsHtml)
            builder.BodyAsHtml(body);
        else
            builder.Body(body);

        // Attach files to the email, this is platform specific call
        foreach(string attachment in attachment_paths)
        {
            builder.WithAttachment(attachment, MimeTypeMap.GetMimeType(Path.GetExtension(attachment)));
        }
        IEmailMessage email = builder.Build();
        emailMessenger.SendEmail(email);
        return true;

}`

But my attachment I provide the file path is:
/data/user/0/com.companyname.GF_Trace/files/.local/share/tempo.pdf
The find mime is application/pdf which is right.
When I SendEmail() I get the followign exception:
string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); string file = @"tempo.pdf"; string path = System.IO.Path.Combine(folder, file);
But the exception raise when sending the email is:
Java.Lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.companyname.GF_Trace/files/.local/share/tempo.pdf occurred

So it seem to miss something, I inspected the path, into the email builder and it still the /data/user/0/com.companyname.GF_Trace/files/.local/share/tempo.pdf
But I guess the /data/user/0 is a symlink to /data/data and this should work.
Any help or working example would be welcome.

There is a thread but I don't get the same problems.

Best Answer

  • edited December 2018 Accepted Answer

    Finally found the problem, it was a typo into the file_path.xml:
    <external-files-path name="exttempopdf" path="tempo.pdf" />
    should have been
    <external-files-path name="exttempopdf" path="." />

Answers

  • JeromeGodboutAmotusJeromeGodboutAmotus Member ✭✭
    edited December 2018 Accepted Answer

    Finally found the problem, it was a typo into the file_path.xml:
    <external-files-path name="exttempopdf" path="tempo.pdf" />
    should have been
    <external-files-path name="exttempopdf" path="." />

Sign In or Register to comment.