xamarin android 邮件,Add Email Body Text and Send Email from Xamarin.Android App

开发者分享在使用Xamarin.Android创建Gmail邮件时,如何将用户输入动态添加到邮件正文的问题。寻求不硬编码整个正文的方法,讨论了现有代码片段和遇到的挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am attempting to create and send an email (using Gmail app) from user-supplied information entered into a simple Xamarin.Android mobile app. I am new to Xamarin.Android and have not been able to find clear guidance on this issue online.

Using the code below, I am able to gather text and create an email Intent that uses the user-provided email address and a hard-coded subject line. But the code for adding body text to the email is not working (the Gmail app opens and the email address and subject line are correct, but the body of the email is blank).

[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(Resource.Id.ClickIncrementer);

Button sendEmailButton = FindViewById(Resource.Id.sendEmailButton);

EditText enterEmailAddressButton = FindViewById(Resource.Id.editEmail);

EditText enterEmailTextButton = FindViewById(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 emailBody = new List

{

"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();

}

};

}

Can anyone show me how I can pre-format and include the email body text in the email Intent, without hard-coding the entire body text string? (Other email-sending critiques and guidance for Xamarin.Android apps is also welcome!)

I want to be able to include information that the user enters in the app, so I want to be able to include variables in the body text (I do not want to hard-code the body text).

Thank you!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值