轉 :android gmail 添加附件

sendMail1.setOnClickListener(new OnClickListener(){
                public void onClick(View v)
                {
                        //OK,1
/*                        Uri uri = Uri.parse("mailto:[email protected]");        
                        Intent it = new Intent(Intent.ACTION_SENDTO, uri);        
                        startActivity(it);*/
                        //OK,2
//                        String[] toemail={"[email protected]", "[email protected]"};
//                        String[] ccemail={"[email protected]", "[email protected]"};
//                        String[] bccemail={"[email protected]", "[email protected]"};
//                        Intent it = new Intent(Intent.ACTION_SEND);
//                        it.putExtra(Intent.EXTRA_EMAIL, toemail);
//                        it.putExtra(Intent.EXTRA_CC, ccemail);
//                        it.putExtra(Intent.EXTRA_BCC, bccemail);
//                        it.putExtra(Intent.EXTRA_SUBJECT, "I love you baby!");
//                        it.putExtra(Intent.EXTRA_TEXT, "The email body text");
//                        it.setType("text/plain");
//                        startActivity(it);
//                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));
                        //OK,3
//                        Intent it = new Intent(Intent.ACTION_SEND);
//                        it.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
//                        it.putExtra(Intent.EXTRA_TEXT, "The email body text");
//                        it.setType("text/plain");
//                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));
                        //OK,6
                        Intent it = new Intent(Intent.ACTION_SEND);
                        it.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
                        it.putExtra(Intent.EXTRA_SUBJECT, "html test");
                        it.putExtra(Intent.EXTRA_TEXT, "http://www.tianya.cn/publicforum/content/stocks/1/776130.shtml");
                        it.setType("text/html");
                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));  
                }
        });
        Button sendMail2 = (Button)findViewById(R.id.sendMail2);
        sendMail2.setOnClickListener(new OnClickListener(){
                public void onClick(View v)
                {
                        //OK,5 實現多個附件的添加,這種方式不是標準的做法,沒有標準的做法,所以第三方軟件不會支持
                        Intent it = new Intent(Intent.ACTION_SEND);
                        it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
                        //添加格式,前面字符串爲附件的URI寫法,後面表示此附件的類型;不能搞錯,不能缺少
                        String szSelectedFiles[]={"file:///sdcard/ebook/CityCodes.xml", "text/xml","file:///sdcard/ebook/PhoneBookU.txt", "text/plain", "file:///sdcard/windy.gif", "image/gif"};
                        it.putExtra(Intent.EXTRA_STREAM, szSelectedFiles);
                        //以下這一句照抄,無論你的附件是什麼類型的
                        it.setType("audio/mp3");
                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));
                        //OK,4 實現單個附件的添加
//                        Uri uri = Uri.parse("file:///sdcard/mysong.mp3");
//                        Intent it = new Intent(Intent.ACTION_SEND);
//                        it.putExtra(Intent.EXTRA_STREAM, uri);
//                        it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
//                        it.setType("audio/mp3");
//                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));
                }
        });

sendMail1.setOnClickListener(new OnClickListener(){
                public void onClick(View v)
                {
                        //OK,1
/*                        Uri uri = Uri.parse("mailto:[email protected]");        
                        Intent it = new Intent(Intent.ACTION_SENDTO, uri);        
                        startActivity(it);*/
                        //OK,2
//                        String[] toemail={"[email protected]", "[email protected]"};
//                        String[] ccemail={"[email protected]", "[email protected]"};
//                        String[] bccemail={"[email protected]", "[email protected]"};
//                        Intent it = new Intent(Intent.ACTION_SEND);
//                        it.putExtra(Intent.EXTRA_EMAIL, toemail);
//                        it.putExtra(Intent.EXTRA_CC, ccemail);
//                        it.putExtra(Intent.EXTRA_BCC, bccemail);
//                        it.putExtra(Intent.EXTRA_SUBJECT, "I love you baby!");
//                        it.putExtra(Intent.EXTRA_TEXT, "The email body text");
//                        it.setType("text/plain");
//                        startActivity(it);
//                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));
                        //OK,3
//                        Intent it = new Intent(Intent.ACTION_SEND);
//                        it.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
//                        it.putExtra(Intent.EXTRA_TEXT, "The email body text");
//                        it.setType("text/plain");
//                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));
                        //OK,6
                        Intent it = new Intent(Intent.ACTION_SEND);
                        it.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
                        it.putExtra(Intent.EXTRA_SUBJECT, "html test");
                        it.putExtra(Intent.EXTRA_TEXT, "http://www.tianya.cn/publicforum/content/stocks/1/776130.shtml");
                        it.setType("text/html");
                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));  
                }
        });
        Button sendMail2 = (Button)findViewById(R.id.sendMail2);
        sendMail2.setOnClickListener(new OnClickListener(){
                public void onClick(View v)
                {
                        //OK,5 實現多個附件的添加,這種方式不是標準的做法,沒有標準的做法,所以第三方軟件不會支持
                        Intent it = new Intent(Intent.ACTION_SEND);
                        it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
                        //添加格式,前面字符串爲附件的URI寫法,後面表示此附件的類型;不能搞錯,不能缺少
                        String szSelectedFiles[]={"file:///sdcard/ebook/CityCodes.xml", "text/xml","file:///sdcard/ebook/PhoneBookU.txt", "text/plain", "file:///sdcard/windy.gif", "image/gif"};
                        it.putExtra(Intent.EXTRA_STREAM, szSelectedFiles);
                        //以下這一句照抄,無論你的附件是什麼類型的
                        it.setType("audio/mp3");
                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));
                        //OK,4 實現單個附件的添加
//                        Uri uri = Uri.parse("file:///sdcard/mysong.mp3");
//                        Intent it = new Intent(Intent.ACTION_SEND);
//                        it.putExtra(Intent.EXTRA_STREAM, uri);
//                        it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
//                        it.setType("audio/mp3");
//                        startActivity(Intent.createChooser(it, "請選擇Email客戶端軟件"));
                }
        });

Android通過Intent發送電子郵件含附件(另附) 如何在Android系統中發送帶附件的電子郵件呢? 其實通過Intent可以很方便的發送Email,只需要短短10行代碼就可以處理,這裏Android開發網就以在sdcard上的 android123.cwj文件爲例,通過Intent來發送電子郵件。完整代碼如下

File file = new File("\sdcard\android123.cwj"); //附件文件地址

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("subject", file.getName()); //
intent.putExtra("body", "android123 - email sender"); //正文
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); //添加附件,附件爲file對象
            if (file.getName().endsWith(".gz")) {
                intent.setType("application/x-gzip"); //如果是gz使用gzip的mime
            } else if (file.getName().endsWith(".txt")) {
                intent.setType("text/plain"); //純文本則用text/plain的mime
            } else {
                intent.setType("application/octet-stream"); //其他的均使用流當做二進制數據來發送
            }
  startActivity(intent); //調用系統的mail客戶端進行發送

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章