我正在尝试使用用户提供的信息创建并发送电子邮件(使用Gmail应用),该信息输入到简单的Xamarin.Android移动应用中。我是Xamarin.Android的新手,并且无法在线找到关于此问题的明确指导。
使用下面的代码,我可以收集文本并创建一个使用用户提供的电子邮件地址和硬编码主题行的电子邮件Intent。但是,将正文添加到电子邮件中的代码无效(Gmail应用程序打开,电子邮件地址和主题行正确,但电子邮件的正文是空白的。)
[Activity(Label = @"HelloWorld-TestMobileApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
public int ClickCount { get; set; } = 0;
public string EmailAddress { get; set; }
public string EmailText { get; set; }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our buttons from the layout resource,
// and attach a events to them
Button clickIncrementerButton = FindViewById<Button>(Resource.Id.ClickIncrementer);
Button sendEmailButton = FindViewById<Button>(Resource.Id.sendEmailButton);
EditText enterEmailAddressButton = FindViewById<EditText>(Resource.Id.editEmail);
EditText enterEmailTextButton = FindViewById<EditText>(Resource.Id.emailText);
clickIncrementerButton.Click += delegate
{
ClickCount++;
clickIncrementerButton.Text = string.Format("{0} clicks!", ClickCount);
};
enterEmailAddressButton.TextChanged += delegate
{
EmailAddress = enterEmailAddressButton.Text;
};
enterEmailTextButton.TextChanged += delegate
{
EmailText = enterEmailTextButton.Text;
};
sendEmailButton.Click += delegate
{
List<string> emailBody = new List<string>
{
"Hello from Xamarin.Android!\n",
"Number of clicks = " + ClickCount + "\n",
EmailText
};
var email = new Intent(Intent.ActionSend);
email.PutExtra(Intent.ExtraEmail, new string[] {EmailAddress}); // Working 09/24/2016
email.PutExtra(Intent.ExtraSubject, "Hello World Email"); // Working 09/24/2016
email.PutStringArrayListExtra(Intent.ExtraText, emailBody); // NOT Working 09/24/2016
email.SetType("message/rfc822");
try
{
StartActivity(email);
}
catch (Android.Content.ActivityNotFoundException ex)
{
Toast.MakeText(this, "There are no email applications installed.", ToastLength.Short).Show();
}
};
}
任何人都可以告诉我如何预先格式化并在电子邮件中包含电子邮件正文文本,而无需对整个正文文本字符串进行硬编码吗?(其他电子邮件发送批评和指导Xamarin.Android应用程序也欢迎!)
我希望能够包含用户在应用中输入的信息,因此我希望能够在正文中包含变量(我不想对正文进行硬编码)。
谢谢!
答案 0 :(得分:1)
问题在于身体也必须是文本。只需将行更改为:
,即可将字符串列表组合为一个字符串email.PutExtra(Intent.ExtraText, string.Join("", emailBody));
BTW:您不需要注册TextViews的更改。打开意图时,您可以直接读取它们的值。我会这样写:
[Activity(Label = @"HelloWorld-TestMobileApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity2 : Activity
{
private Button _incrementerButton;
private Button _emailButton;
private EditText _addressField;
private EditText _emailField;
public int ClickCount { get; set; } = 0;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our buttons from the layout resource,
// and attach a events to them
_incrementerButton = FindViewById<Button>(Resource.Id.ClickIncrementer);
_emailButton = FindViewById<Button>(Resource.Id.sendEmailButton);
_addressField = FindViewById<EditText>(Resource.Id.editEmail);
_emailField = FindViewById<EditText>(Resource.Id.emailText);
}
protected override void OnStart()
{
base.OnStart();
// register event handlers
_incrementerButton.Click += IncrementerButtonOnClick;
_emailButton.Click += SendEmailButtonOnClick;
}
protected override void OnStop()
{
//deregister event handlers
_emailButton.Click -= SendEmailButtonOnClick;
_incrementerButton.Click -= IncrementerButtonOnClick;
base.OnStop();
}
private void IncrementerButtonOnClick(object sender, EventArgs eventArgs)
{
ClickCount++;
_incrementerButton.Text = string.Format("{0} clicks!", ClickCount);
}
private void SendEmailButtonOnClick(object sender, EventArgs eventArgs)
{
// build the body
var emailText = new StringBuilder();
emailText.AppendLine("Hello from Xamarin.Android!");
emailText.AppendFormat("Number of clicks = {0}", ClickCount);
emailText.AppendLine();
emailText.Append(_emailField.Text);
// build the intent
var email = new Intent(Intent.ActionSend);
email.PutExtra(Intent.ExtraEmail, new string[] { _addressField.Text });
email.PutExtra(Intent.ExtraSubject, "Hello World Email");
email.PutExtra(Intent.ExtraText, emailText.ToString());
email.SetType("message/rfc822");
try
{
StartActivity(email);
}
catch (Android.Content.ActivityNotFoundException ex)
{
Toast.MakeText(this, "There are no email applications installed.", ToastLength.Short).Show();
}
}
}
我正在使用StringBuilder
来创建电子邮件正文。并在方法中实现事件处理程序而不是匿名委托。原因是,我希望能够通过-=
中的OnStop
取消注册事件处理程序。这一点很重要,因为未注销的事件处理程序将导致内存泄漏。