使用依赖服务 DROID Xamarin 表单发送电子邮件 + 附件
Send an email +attachment using dependency services DROID Xamarin forms
我在尝试发送带附件的电子邮件时遇到了这个错误。有帮助吗?谢谢
**** 从 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
来启动 activity。
或者
- 您可以在
emailIntent.
中添加 emailIntent.SetFlags(ActivityFlags.NewTask);
In Android, every Activity
should have its own task stack which you can use taskAffinity to define it, the package's name is the default value. But if you use Application.Context
to start activity, there is not task stack for your activity, so it suggests you to use FLAG_ACTIVITY_NEW_TASK
flag, this flag will create a task stack for you activity.
检查您是否有要发送的附件。
*要替换 if 块并更改此
Intent emailIntent = new Intent(Intent.ActionSend);
//change it to
Intent emailIntent = new Intent(Intent.ActionSendto);
Android.App.Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "Send email..."));
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");
}
我的问题取决于附件本身。
我在尝试发送带附件的电子邮件时遇到了这个错误。有帮助吗?谢谢
**** 从 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
来启动 activity。
或者
- 您可以在
emailIntent.
中添加
emailIntent.SetFlags(ActivityFlags.NewTask);
In Android, every
Activity
should have its own task stack which you can use taskAffinity to define it, the package's name is the default value. But if you useApplication.Context
to start activity, there is not task stack for your activity, so it suggests you to useFLAG_ACTIVITY_NEW_TASK
flag, this flag will create a task stack for you activity.
检查您是否有要发送的附件。 *要替换 if 块并更改此
Intent emailIntent = new Intent(Intent.ActionSend);
//change it to
Intent emailIntent = new Intent(Intent.ActionSendto);
Android.App.Application.Context.StartActivity(Intent.CreateChooser(emailIntent, "Send email..."));
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");
}
我的问题取决于附件本身。