2011-02-03 128 views

回答

22
Intent email = new Intent(android.content.Intent.ACTION_SEND); 
        email.setType("application/octet-stream");  

編輯:
你可以嘗試設置類型"message/rfc822"爲好。

試試這個....

+3

WiFi和藍牙仍然可見。 – efeyc 2012-04-03 11:44:04

+0

如果我必須使用特殊附件類型的類型? – 2013-09-02 08:10:21

+1

@ N-JOY爲什麼「application/octet-stream」,而不是「message/rfc822」? – 2013-10-22 14:52:56

2

第一種解決方法:試着在你的Intent參數中更具體。添加郵件收件人例如

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

解決方法二:使用package manager找到能夠發送郵件的所有應用程序,並選擇您要使用的只有這些。

17

@Ganapathy:嘗試顯示Gmail的

Intent gmail = new Intent(Intent.ACTION_VIEW); 
       gmail.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail"); 
       gmail.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); 
       gmail.setData(Uri.parse("[email protected]")); 
       gmail.putExtra(Intent.EXTRA_SUBJECT, "enter something"); 
       gmail.setType("plain/text"); 
       gmail.putExtra(Intent.EXTRA_TEXT, "hi android jack!"); 
       startActivity(gmail); 
13

這會幫助你。

On your button click : 

Intent email = new Intent(Intent.ACTION_SEND); 
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});   
email.putExtra(Intent.EXTRA_SUBJECT, "subject"); 
email.putExtra(Intent.EXTRA_TEXT, "message"); 
email.setType("message/rfc822"); 
startActivity(Intent.createChooser(email, "Choose an Email client :")); 
15

我沒有拿這個答案的功勞,但我相信它給出了這篇文章的最佳答案。

使用text/plain或text/html是一種常見的誤解。這將觸發任何可以處理純文本或HTML文本文件而無需任何上下文的應用程序,包括Google Drive,Dropbox,Evernote和Skype。

改用ACTION_SENDTO,提供mailto:烏里

intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")); 

然後,您可以使用選擇器通過其他答案建議進行。

回答@PaulLammertsma這裏 Android email chooser

0

最好的代碼來限制它只能發送電子郵件。將此類型設置爲僅發送電子郵件。 i.setType("message/rfc822");

 Intent i = new Intent(Intent.ACTION_SEND); 
     i.setType("message/rfc822"); 
     i.putExtra(Intent.EXTRA_EMAIL , new String[]{"[email protected]"}); 
     i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
     i.putExtra(Intent.EXTRA_TEXT , "body of email"); 
     try { 
      startActivity(Intent.createChooser(i, "Send mail...")); 
     } catch (android.content.ActivityNotFoundException ex) { 
      Toast.makeText(Firstclass.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
     } 
2

我注意到,這是一個很老的問題,但它是第一個結果爲「發送郵件」的解決方案進行搜索時,所有答案都有一個共同的問題:

使用Intent.ACTION_SENDintent.setType("message/rfc822")會導致選擇器不僅顯示郵件應用程序,還會顯示所有可通過message/rfc822處理任何 MIME類型支持的應用程序,例如.mhtml,.mht,.mime。除了郵件應用程序,這可能是Google Drive,Dropbox,Evernote等

我發現將選擇器限制爲僅適用於郵件應用程序的唯一解決方案是使用Intent。ACTION_SENDTO代替:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","[email protected]", null)); 
intent.putExtra(Intent.EXTRA_SUBJECT, "My Mail"); 
intent.putExtra(Intent.EXTRA_TEXT , "My Message"); 

try { 
    startActivity(Intent.createChooser(i, "Send mail...")); 
} catch (android.content.ActivityNotFoundException ex) { 
    Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
} 
0

這是傑克Dsilva和Jignesh Mayani解決方案的組合:

try 
    { 
     Intent gmailIntent = new Intent(Intent.ACTION_SEND); 
     gmailIntent.setType("text/html"); 

     final PackageManager pm = _activity.getPackageManager(); 
     final List<ResolveInfo> matches = pm.queryIntentActivities(gmailIntent, 0); 
     String gmailActivityClass = null; 

     for (final ResolveInfo info : matches) 
     { 
      if (info.activityInfo.packageName.equals("com.google.android.gm")) 
      { 
       gmailActivityClass = info.activityInfo.name; 

       if (gmailActivityClass != null && !gmailActivityClass.isEmpty()) 
       { 
        break; 
       } 
      } 
     } 

     gmailIntent.setClassName("com.google.android.gm", gmailActivityClass); 
     gmailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); 
     gmailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
     gmailIntent.putExtra(Intent.EXTRA_CC, "[email protected]"); // if necessary 
     gmailIntent.putExtra(Intent.EXTRA_TEXT, "Email message"); 
     gmailIntent.setData(Uri.parse("[email protected]")); 
     this._activity.startActivity(gmailIntent); 
    } 

    catch (Exception e) 
    { 
     Intent i = new Intent(Intent.ACTION_SEND); 
     i.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); 
     i.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
     i.putExtra(Intent.EXTRA_CC, "[email protected]"); // if necessary 
     i.putExtra(Intent.EXTRA_TEXT, "Email message"); 
     i.setType("plain/text"); 
     this._activity.startActivity(i); 
    } 

因此,在首先它會嘗試打開Gmail應用程序,並在情況下,用戶沒有它那麼第二種方法將被執行。

5

我和我的應用程序有類似的問題。我最近發現這個鏈接形式的官方Android開發人員網站,真的有幫助! Common Intents: Email

TL; DR:

Intent intent = new Intent(Intent.ACTION_SENDTO); 
intent.setData(Uri.parse("mailto:")); 

現在,你將只顯示電子郵件客戶端!

您可以通過這樣添加主題和正文:

intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
intent.putExtra(Intent.EXTRA_TEXT, "Body"); 
3

[解決方案API等級> = 15]

我終於succeded在發送電子郵件帶有附件的僅電子郵件客戶端。 我寫在這裏,因爲它花了我很多時間,對別人來說可能是有用的。

的問題是:

  • Intent.ACTION_SENDTO需要數據URI(所以你可以指定 「電子郵件地址:」 模式),但它不接受意向:EXTRA_STREAM。

  • Intent.ACTION_SEND接受意向:EXTRA_STREAM(這樣你就可以添加 附件),但只需要鍵入(不包括Data URI所以你不能 指定「電子郵件地址:」模式)。

所以Intent.ACTION_SEND允許用戶從幾個活動可供選擇,即使你的setType(「信息/ RFC822」),因爲該應用程序/活動可以管理所有文件類型(tipically的GDrive/Dropbox的應用程序)等甚至是電子郵件文件。

解決方案在setSelector方法中。 使用此方法,您可以使用Intent.ACTION_SENDTO來選擇活動,但然後發送Intent.ACTION_SEND Intent。

這裏我的解決方案代碼(附件來自一個FileProvider,但它可以是任何文件):

{ 
    Intent emailSelectorIntent = new Intent(Intent.ACTION_SENDTO); 
    emailSelectorIntent.setData(Uri.parse("mailto:")); 

    final Intent emailIntent = new Intent(Intent.ACTION_SEND); 
    emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    emailIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
    emailIntent.setSelector(emailSelectorIntent); 

    Uri attachment = FileProvider.getUriForFile(this, "my_fileprovider", myFile); 
    emailIntent.putExtra(Intent.EXTRA_STREAM, attachment); 

    if(emailIntent.resolveActivity(getPackageManager()) != null) 
     startActivity(emailIntent); 
} 
0

這救了我的一天。它直接發送複合文本消息到Gmail應用程序:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
         "mailto","togmail.com", null)); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Report message"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, edt_msg.getText().toString()); 
startActivity(Intent.createChooser(emailIntent, "Send email..."));