<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 如何在 Android 中發送帶有附件的電子郵件 > 原文: [https://javatutorial.net/send-email-with-attachments-android](https://javatutorial.net/send-email-with-attachments-android) 在本教程中,您將學習如何在 Android 中以編程方式發送電子郵件以及如何附加文件,圖像或視頻。 電子郵件通常用于專業和個人對話。 許多應用程序需要電子郵件選項才能將數據發送給其他人。 您可以使用 Android 應用程序輕松發送電子郵件。 附件呢? 不幸的是,Android 不允許我們附加許多不同的文件格式,但是您可以添加其中的一些格式,例如照片,音頻,視頻,pdf 和 Word 文檔。 ## 電子郵件附件的示例 首先創建一個允許用戶通過電子郵件發送數據的應用程序。`Intent.ACTION_SEND`用于通過手機中的現有客戶端發送電子郵件。 類似地,`Intent.ACTION_GET_CONTENT`用于附件。 ## `Intent.ACTION_SEND` `ACTION_SEND`用于將數據發送到您應用中的其他活動或其他應用。 兩者都很容易理解和使用。 如果要將數據發送到應用程序中的其他活動,則只需指定數據及其類型。 Android 操作系統將完成剩下的工作……它將找到兼容的活動并顯示數據。 同樣,您可以將數據發送到其他應用程序。 將內容類型設置為簡單的純文本。 這是應該如何編碼的 ```java final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { email }); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject); ``` ## `Intent.ACTION_GET_CONTENT` `ACTION_GET_CONTENT`用于選擇多種類型的數據并返回。 您可以通過將 MIME 類型設置為此類數據來直接選擇所需的內容類型。 或者,您可以選擇所需的任何類型的數據。 為此,請使用選擇器包裝您的意圖,該選擇器將啟動一個新活動,并讓用戶選擇所需的內容。 這是怎么做的 ```java Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); intent.putExtra("return-data", true); startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY); ``` ## 向清單文件添加權限 向清單文件添加以下權限。 ```java <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> ``` 打開 [Android Studio](https://javatutorial.net/install-configure-android-studio) 并創建一個新項目。 創建主要活動并將以下代碼粘貼到`activity_main.xml`中 ```java <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" tools:context=".MainActivity" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:padding="5dp" > <EditText android:id="@+id/et_to" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_margin="5dp" android:hint="Receiver's Email Address!" android:inputType="textEmailAddress" android:singleLine="true" /> <EditText android:id="@+id/et_subject" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/et_to" android:layout_margin="5dp" android:hint="Enter Subject" android:singleLine="true" /> <EditText android:id="@+id/et_message" android:layout_width="match_parent" android:layout_height="200dp" android:layout_below="@id/et_subject" android:layout_margin="5dp" android:gravity="top|left" android:hint="Compose Email" android:inputType="textMultiLine" /> <Button android:id="@+id/bt_send" android:layout_width="80dp" android:layout_height="50dp" android:layout_below="@id/et_message" android:layout_margin="5dp" android:text="Send" /> <Button android:id="@+id/bt_attachment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="attachment" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> </ScrollView> ``` 現在這里是`MainActivity.java` ```java package com.example.admin.emailattachmentexample; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText et_email; EditText et_subject; EditText et_message; Button Send; Button Attachment; String email; String subject; String message; String attachmentFile; Uri URI = null; private static final int PICK_FROM_GALLERY = 101; int columnIndex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_email = (EditText) findViewById(R.id.et_to); et_subject = (EditText) findViewById(R.id.et_subject); et_message = (EditText) findViewById(R.id.et_message); Attachment = (Button) findViewById(R.id.bt_attachment); Send = (Button) findViewById(R.id.bt_send); //send button listener Send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendEmail(); } }); //attachment button listener Attachment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openFolder(); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); cursor.moveToFirst(); columnIndex = cursor.getColumnIndex(filePathColumn[0]); attachmentFile = cursor.getString(columnIndex); Log.e("Attachment Path:", attachmentFile); URI = Uri.parse("file://" + attachmentFile); cursor.close(); } } public void sendEmail() { try { email = et_email.getText().toString(); subject = et_subject.getText().toString(); message = et_message.getText().toString(); final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { email }); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject); if (URI != null) { emailIntent.putExtra(Intent.EXTRA_STREAM, URI); } emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); this.startActivity(Intent.createChooser(emailIntent,"Sending email...")); } catch (Throwable t) { Toast.makeText(this, "Request failed try again: " + t.toString(),Toast.LENGTH_LONG).show(); } } public void openFolder() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); intent.putExtra("return-data", true); startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY); } } ``` 運行您的應用,這是輸出 ![Email with attachment example](https://img.kancloud.cn/2b/57/2b579be6961f116c7d0cded95ce926ed_576x1024.jpg) 帶附件的電子郵件示例 點擊“添加附件” ![add attachment](https://img.kancloud.cn/ed/97/ed97284eb1145133019fcad20f8a2b39_576x1024.jpg) 添加附件 現在點擊“發送”按鈕 ![send email](https://img.kancloud.cn/23/ac/23ace6e42ac63c9ebd0bf604d57434c9_576x1024.jpg) 發電子郵件 您可以通過單擊[鏈接](https://github.com/JavaTutorialNetwork/Tutorials/blob/master/EmailAttachmentExample.rar)下載此項目。 ## 參考文獻 [Android `ACTION_SEND` javadoc](https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND) [Android `ACTION_GET_CONTENT` javadoc](https://developer.android.com/reference/android/content/Intent.html#ACTION_GET_CONTENT)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看