能'通过Android应用程序发送电子邮件

时间:2016-03-09 15:25:40

标签: c# android xamarin

我在Visual Studio中使用C#,我想通过我的应用发送电子邮件,但我总是收到错误

这是我的代码

Button button = FindViewById<Button>(Resource.Id.myButton);

button.Click += delegate
{
    var email = new Intent(Android.Content.Intent.ActionSend);
    email.PutExtra(Android.Content.Intent.ExtraEmail, new string[] { "person1@gmail.com", "person2@gmail.com" });
    email.PutExtra(Android.Content.Intent.ExtraCc, new string[] { "person3@gmail.com" });
    email.PutExtra(Android.Content.Intent.ExtraSubject, "Hello Email");
    email.PutExtra(Android.Content.Intent.ExtraText, "Hello user");
    email.SetType("message/rfc822");
    StartActivity(email);
};

我总是得到这个错误

  

Android.Content.ActivityNotFoundException:找不到处理Intent的活动{act = android.intent.action.SEND typ = message / rfc822 flg = 0x1(has clip)(有额外内容)}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

我的意思是它无法找到处理SEND操作的活动。 你在模拟器中的代码上运行吗?在物理设备上试用(确保它安装了邮件客户端)。

另外,我会将您的代码包含在 try / catch 块中,以避免在没有可用于处理您意图的活动时崩溃。

**更新**

这是一个例子。将try / catch块放在代理中。

try
{
    var email = new Intent(Android.Content.Intent.ActionSend);
        email.PutExtra(Android.Content.Intent.ExtraEmail, new string[] { "person1@gmail.com", "person2@gmail.com" });
        email.PutExtra(Android.Content.Intent.ExtraCc, new string[] { "person3@gmail.com" });
        email.PutExtra(Android.Content.Intent.ExtraSubject, "Hello Email");
        email.PutExtra(Android.Content.Intent.ExtraText, "Hello user");
        email.SetType("message/rfc822");
        StartActivity(email);
}
catch (Exception ex)
{
    // Either ignore or log the error.
}