• Android Video Tutorials

Android - 傳送電子郵件



電子郵件 是透過網路從一個系統使用者以電子方式分發給一個或多個收件人的訊息。

在開始電子郵件活動之前,您必須瞭解使用 Intent 的電子郵件功能,Intent 用於在應用程式內或應用程式外將資料從一個元件傳輸到另一個元件。

要從您的應用程式傳送電子郵件,您不必從頭開始實現電子郵件客戶端,而是可以使用現有的客戶端,例如 Android 提供的預設電子郵件應用程式、Gmail、Outlook、K-9 Mail 等。為此,我們需要編寫一個使用隱式 Intent(具有正確的操作和資料)啟動電子郵件客戶端的 Activity。在此示例中,我們將使用啟動現有電子郵件客戶端的 Intent 物件從我們的應用程式傳送電子郵件。

以下部分解釋了傳送電子郵件所需的 Intent 物件的不同部分。

Intent 物件 - 傳送電子郵件的操作

您將使用ACTION_SEND操作來啟動安裝在 Android 裝置上的電子郵件客戶端。以下是使用 ACTION_SEND 操作建立 Intent 的簡單語法。

Intent emailIntent = new Intent(Intent.ACTION_SEND);

Intent 物件 - 傳送電子郵件的資料/型別

要傳送電子郵件,您需要使用 setData() 方法指定mailto:作為 URI,並將資料型別設定為text/plain,如下所示:

emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");

Intent 物件 - 傳送電子郵件的額外資料

Android 內建支援新增 TO、SUBJECT、CC、TEXT 等欄位,這些欄位可以在將 Intent 傳送到目標電子郵件客戶端之前附加到 Intent。您可以在您的電子郵件中使用以下額外欄位:

序號 額外資料和描述
1

EXTRA_BCC

包含應密送的電子郵件地址的 String[]。

2

EXTRA_CC

包含應抄送的電子郵件地址的 String[]。

3

EXTRA_EMAIL

包含應傳送到的電子郵件地址的 String[]。

4

EXTRA_HTML_TEXT

與 Intent 關聯的常量字串,與 ACTION_SEND 一起使用,用於提供作為 HTML 格式文字的 EXTRA_TEXT 的替代項。

5

EXTRA_SUBJECT

包含所需郵件主題行的常量字串。

6

EXTRA_TEXT

與 Intent 關聯的常量 CharSequence,與 ACTION_SEND 一起使用,用於提供要傳送的文字資料。

7

EXTRA_TITLE

與 ACTION_CHOOSER 一起使用時,提供給使用者的 CharSequence 對話方塊標題。

以下是一個示例,向您展示如何將額外資料分配到您的 Intent:

emailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"Recipient"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
emailIntent.putExtra(Intent.EXTRA_TEXT   , "Message Body");

以上程式碼的輸出如下圖所示

Email

電子郵件示例

示例

以下示例將向您展示如何在實踐中使用 Intent 物件啟動電子郵件客戶端以將電子郵件傳送給指定的收件人。

要使用此示例進行電子郵件實驗,您需要配備最新 Android 作業系統的實際移動裝置,否則您可能會難以使用模擬器,模擬器可能無法正常工作。其次,您需要在裝置上安裝電子郵件客戶端,例如 Gmail(預設情況下,每個 Android 版本都帶有 Gmail 客戶端應用程式)或 K9mail。
步驟 描述
1 您將使用 Android Studio 建立一個 Android 應用程式,並將其命名為Tutorialspoint,包名為com.example.tutorialspoint
2 修改src/MainActivity.java檔案並新增所需程式碼以處理傳送電子郵件。
3 修改佈局 XML 檔案res/layout/activity_main.xml,如果需要,新增任何 GUI 元件。我添加了一個簡單的按鈕來啟動電子郵件客戶端。
4 修改res/values/strings.xml以定義所需的常量值
5 修改AndroidManifest.xml,如下所示
6 執行應用程式以啟動 Android 模擬器並驗證對應用程式所做更改的結果。

以下是修改後的主活動檔案src/com.example.Tutorialspoint/MainActivity.java的內容。

package com.example.tutorialspoint;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      Button startBtn = (Button) findViewById(R.id.sendEmail);
      startBtn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            sendEmail();
         }
      });
   }
	
   protected void sendEmail() {
      Log.i("Send email", "");
      String[] TO = {""};
      String[] CC = {""};
      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_CC, CC);
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
      emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
      
      try {
         startActivity(Intent.createChooser(emailIntent, "Send mail..."));
         finish();
         Log.i("Finished sending email...", "");
      } catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
      }
   }
}

res/layout/activity_main.xml檔案的內容將如下所示:

此處 abc 指示 tutorialspoint 徽標
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   
   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Sending Mail Example"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />
      
   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point "
      android:textColor="#ff87ff09"
      android:textSize="30dp"
      android:layout_above="@+id/imageButton"
      android:layout_alignRight="@+id/imageButton"
      android:layout_alignEnd="@+id/imageButton" />
      
   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageButton"
      android:src="@drawable/abc"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" />
      
   <Button 
      android:id="@+id/sendEmail"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/compose_email"/>
    
</LinearLayout>

res/values/strings.xml的內容將如下所示,以定義兩個新的常量:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">Tutorialspoint</string>
   <string name="compose_email">Compose Email</string>
</resources>

以下是AndroidManifest.xml的預設內容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.Tutorialspoint" >
   
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.tutorialspoint.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

讓我們嘗試執行您的tutorialspoint應用程式。我假設您已將實際的 Android 移動裝置連線到您的計算機。要從 Android Studio 執行應用程式,請開啟專案的活動檔案之一,然後單擊工具欄中的執行Eclipse Run Icon圖示。在啟動應用程式之前,Android Studio 安裝程式將顯示以下視窗,讓您選擇要在其中執行 Android 應用程式的選項。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示以下螢幕:

Android Mobile Device

現在使用撰寫郵件按鈕列出所有已安裝的電子郵件客戶端。您可以從列表中選擇一個電子郵件客戶端來發送您的電子郵件。我將使用 Gmail 客戶端傳送我的電子郵件,它將具有所有提供的預設欄位,如下所示。此處發件人:將是您為 Android 設備註冊的預設電子郵件 ID。

Android Mobile Gmail Screen

您可以修改任何給定的預設欄位,最後使用傳送電子郵件按鈕將您的電子郵件傳送給提到的收件人。

廣告