【问题标题】:Send an email +attachment using dependency services DROID Xamarin forms使用依赖服务 DROID Xamarin 表单发送电子邮件+附件
【发布时间】:2017-12-07 17:29:51
【问题描述】:

我在尝试发送带有附件的电子邮件时遇到了这个错误。有什么帮助吗?谢谢你

****从 Activity 上下文之外调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?****

这是我的代码:

[assembly: Dependency(typeof(sendEmail))]
namespace myapp.Droid
{
    public class sendEmail : IEmailTask
    {
        public sendEmail()
        {

        }

        public void SendEmail () 
        {
            var sqlliteFilname = "test.3gpp";
            string documentsPath = System.Environment.GetFolderPath(
            Environment.SpecialFolder.Personal);
            var stringPath = Path.Combine(documentsPath, sqlliteFilname);

            var path = Android.Net.Uri.FromFile(new 
           Java.IO.File(stringPath));

            Intent emailIntent = new Intent(Intent.ActionSend);
            // set the type to 'email'
            emailIntent.SetData(Android.Net.Uri.Parse("mailto:"));

            String[] to = { "youremail@mail.com" };

            emailIntent.PutExtra(Intent.ExtraEmail, to);
            // the attachment
            emailIntent.PutExtra(Intent.ExtraStream, path);
            // the mail subject
            emailIntent.PutExtra(Intent.ExtraSubject, "Subject");

           Android.App.Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "Send email..."));


        }
    }
}

页面上的代码是:

 void btnSendingHandle_Clicked(object sender, System.EventArgs e)
        {
            var getEmail = DependencyService.Get<IEmailTask>();

            getEmail.SendEmail();
        }

【问题讨论】:

  • 由于这是一个表单应用程序,我会尝试将您的上下文更改为 Forms.Context.StartActivity (Intent.CreateChooser(emailIntent, "Send email..."));

标签: android forms xamarin dependencies email-attachments


【解决方案1】:

就像尼克说的

  • 您可以使用Forms.Context 开始活动。

或者

  • 您可以在您的emailIntent. 中添加emailIntent.SetFlags(ActivityFlags.NewTask);

在Android中,每个Activity都应该有自己的任务栈,你可以使用taskAffinity来定义它,包的名字是默认值。但是如果你使用Application.Context启动activity,你的activity没有task stack,所以建议你使用FLAG_ACTIVITY_NEW_TASK flag,这个flag会为你的activity创建一个task stack。

【讨论】:

  • 感谢您的回复。我尝试了这两种解决方案,但我仍然得到同样的错误。我没有选择添加 Forms.context , Android.App.Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "Send email..."));
  • 我应该在哪里调用这个任务? emailIntent.SetFlags(ActivityFlags.NewTask);
  • 不是Forms.context,是Forms.Context,确定是Xamarin.Forms.Forms.Context,还有this is the document about task stack in Androidthis is about the error in Android
【解决方案2】:

检查您是否有要发送的附件。 *替换 if 块并更改它

Intent emailIntent = new Intent(Intent.ActionSend);
//change it to 


Intent emailIntent = new Intent(Intent.ActionSendto);

Android.App.Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "发送电子邮件..."));

if (emailIntent.ResolveActivity(Android.App.Application.Context.PackageManager) != null)
            {
                Android.App.Application.Context.StartActivity(emailIntent);
            }
            else
            {

                    string tag = "MY-EMAIL";
                    Log.Info(tag, "no attachment found");
            }

我的问题取决于附件本身。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2014-10-27
    • 2019-07-31
    • 2011-04-18
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多