发送带有附件的邮件

时间:2020-03-30 03:58:12

标签: xamarin.android

使用此代码向我发送电子邮件并与我一起工作。 我现在想要的是同时发送带有消息的文件附件。

  var email = new Intent(Android.Content.Intent.ActionSend);
            email.PutExtra(Android.Content.Intent.ExtraEmail, new string[] {
            "susairajs@outlook.com",
            "susairajs18@gmail.com"
        });
            email.PutExtra(Android.Content.Intent.ExtraCc, new string[] {
            "susairajs18@live.com"
        });
            email.PutExtra(Android.Content.Intent.ExtraSubject, "Hello Xamarin");
            email.PutExtra(Android.Content.Intent.ExtraText, "Hello Xamarin This is My Test Mail...!");
            email.SetType("message/rfc822");
            StartActivity(email);

2 个答案:

答案 0 :(得分:1)

最简单的方法如下:

        string filename = "file.ext";
        var filelocation = new File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, filename);
        Uri path = Android.Net.Uri.FromFile(filelocation);
        Intent emailIntent = new Intent(Intent.ActionSend);
        emailIntent.PutExtra(Intent.ExtraStream, path);

具有以下using语句:

using File = Java.IO.File;
using Uri = Android.Net.Uri;

如果您有任何疑问,Goodluck随时可以还原

答案 1 :(得分:1)

Assets文件夹中的

sample.txt。将“构建操作”设置为AndroidAsset。

enter image description here

txt文件的路径。

//android_asset/sample.txt

用法:

btnSend.Click += delegate
        {
            Android.Net.Uri file = Android.Net.Uri.FromFile(new 
Java.IO.File("//android_asset/sample.txt"));
            var email = new Intent(Android.Content.Intent.ActionSend);
            email.PutExtra(Android.Content.Intent.ExtraEmail,
            new string[] { "xxxxxx@xxxxx.com" });
            //email.PutExtra(Android.Content.Intent.ExtraCc,
            //new string[] { "something@gmail.com" });
            email.PutExtra(Android.Content.Intent.ExtraSubject, "Awesome File");
            email.PutExtra(Android.Content.Intent.ExtraText, "See attached file");
            email.PutExtra(Android.Content.Intent.ExtraStream, file);
            email.SetType("message/rfc822");
            StartActivity(Intent.CreateChooser(email, "Send email..."));
        };

enter image description here