2016-06-28 109 views
1

我正在爲酒店開發應用程序,在預訂房間時,應用程序會向用戶發送的電子郵件ID發送電子郵件。現在,我知道它會使用我的默認電子郵件客戶端之一,這是Gmail。問題在於,它顯示了gmail的撰寫郵件窗口,郵件正文中顯示了我的消息,但'to'字段爲空。任何幫助?Android:Email'to'field empty

這裏是代碼:

public void sendmail(View vw) 
    { 
     name=et1.getText().toString(); 
     to=et2.getText().toString(); 
     phone=et3.getText().toString(); 
     addr=et4.getText().toString(); 
     Log.i("Send email", ""); 
     Intent emailIntent = new Intent(Intent.ACTION_SEND); 
     emailIntent.setData(Uri.parse("mailto:")); 
      emailIntent.setType("text/plain"); 
      emailIntent.putExtra(Intent.EXTRA_EMAIL, to); 
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Thanks for using our app"); 
      emailIntent.putExtra(Intent.EXTRA_TEXT, "Hello mr"+name+"We just received an email with your details asking for a reservation:"+phone+" "+addr+"for room number"+x+""); 
      try { 
       startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
       finish(); 
       Log.i("Finished sending email...", ""); 
      } 
      catch (android.content.ActivityNotFoundException ex) { 
       Toast.makeText(getBaseContext(), "There is no email client installed.", Toast.LENGTH_SHORT).show(); 
      } 
    } 


} 
+1

請提供[mcve],顯示您正在使用的代碼。 – CommonsWare

回答

1

EXTRA_EMAILdocumentation

應該交付給的String []控股的e-mail地址。

像這樣的東西應該工作:

Intent mailIntent = new Intent(Intent.ACTION_SENDTO); 
mailIntent.setData(Uri.parse("mailto:")); 
mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{to}); 
mailIntent.putExtra(Intent.EXTRA_SUBJECT, "Thanks for using our app"); 
mailIntent.putExtra(Intent.EXTRA_TEXT, "Hello mr"+name+"We just received an email with your details asking for a reservation:"+phone+" "+addr+"for room number"+x+""); 
if (mailIntent.resolveActivity(getPackageManager()) != null) { 
    startActivity(mailIntent); 
} else { 
    // no e-mail app installed 
} 
1

試試這個:

emailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 

電子郵件意圖期待一個String數組,但你提供string.So,它不是在你的工作案件!