2016-09-24 10 views
0

シンプルなXamarin.Androidモバイルアプリに入力されたユーザー提供の情報からメールを作成して送信しようとしています(Gmailアプリを使用しています)。私はXamarin.Androidには新しく、オンラインでこの問題に関する明確なガイダンスを見つけることができませんでした。メール本文を追加してXamarin.Android Appからメールを送信

以下のコードを使用して、テキストを収集し、ユーザー提供のメールアドレスとハードコードされた件名を使用するメールインテントを作成できます。しかし、電子メールに本文を追加するコードは機能していません(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アプリのその他のメール送信の批評と指導も歓迎です)

ユーザーがアプリに入力する情報を含めることができますので、本文テキスト(私は本文テキストをハードコードしたくありません)。

ありがとうございました!

答えて

1

問題は本文がテキストでなければならないということです。

ところで
email.PutExtra(Intent.ExtraText, string.Join("", emailBody)); 

::ちょうどにラインを変更することにより、1つの文字列に、あなたの文字列リストを組み合わせたあなたは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でイベントハンドラの登録を解除できるようにするためです。登録されていないイベントハンドラはメモリリークを引き起こさないため、これは常に重要です。

+0

あなたの提案は私のために働いた。メモリリークを防ぐために、メソッドやイベントハンドラを使用する方法についてのアドバイスも感謝します。ありがとうございました! – TechAust10

関連する問題