Android-sending-email

提供:Dev Guides
移動先:案内検索

Android-メールの送信

'_*電子メール*は、ネットワークを介して1人のシステムユーザーから1人以上の受信者に電子的に配信されるメッセージです。_

電子メールアクティビティを開始する前に、意図的に電子メール機能を知っておく必要があります。Intentは、アプリケーション内またはアプリケーション外で1つのコンポーネントから別のコンポーネントにデータを伝送します。

アプリケーションからメールを送信するには、最初からメールクライアントを実装する必要はありませんが、Android、Gmail、Outlook、K-9 Mailなどから提供されるデフォルトのメールアプリのような既存のものを使用できます。 この目的のために、適切なアクションとデータで暗黙的なインテントを使用して、電子メールクライアントを起動するアクティビティを作成する必要があります。 この例では、既存のメールクライアントを起動するIntentオブジェクトを使用して、アプリからメールを送信します。

次のセクションでは、電子メールの送信に必要なIntentオブジェクトのさまざまな部分について説明します。

インテントオブジェクト-電子メールを送信するアクション

*ACTION_SEND* アクションを使用して、Androidデバイスにインストールされているメールクライアントを起動します。 以下は、ACTION_SENDアクションでインテントを作成する簡単な構文です。
Intent emailIntent = new Intent(Intent.ACTION_SEND);

インテントオブジェクト-電子メールを送信するデータ/タイプ

メールを送信するには、setData()メソッドを使用してURIとして* mailto:を指定する必要があり、データタイプは次のようにsetType()メソッドを使用して *text/plain になります-

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

インテントオブジェクト-電子メールを送信するための追加

Androidには、TO、SUBJECT、CC、TEXTなどを追加する組み込みサポートがあります。 インテントをターゲットの電子メールクライアントに送信する前にインテントに添付できるフィールド。 メールで次の追加フィールドを使用できます-

Sr.No. Extra Data & Description
1

EXTRA_BCC

ブラインドカーボンコピーされる電子メールアドレスを保持するString []。

2

EXTRA_CC

カーボンコピーする必要がある電子メールアドレスを保持するString []。

3

EXTRA_EMAIL

配信する必要がある電子メールアドレスを保持するString []。

4

EXTRA_HTML_TEXT

HTML形式のテキストとしてEXTRA_TEXTの代替を提供するためにACTION_SENDとともに使用される、インテントに関連付けられた定数文字列。

5

EXTRA_SUBJECT

メッセージの目的の件名行を保持する定数文字列。

6

EXTRA_TEXT

Intentに関連付けられた定数CharSequence。ACTION_SENDとともに使用して、送信するリテラルデータを提供します。

7

EXTRA_TITLE

ACTION_CHOOSERと共に使用されたときにユーザーに提供するCharSequenceダイアログのタイトル。

ここにあなたの意図に追加のデータを割り当てる方法を示す例があります-

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

上記のコードの出力は以下の画像のようになります

メール

メールの例

次の例は、Intentオブジェクトを使用して電子メールクライアントを起動し、指定された受信者に電子メールを送信する実際的な方法を示しています。

'_この例を使用して実験をメールで送信するには、最新のAndroid OSを搭載した実際のモバイルデバイスが必要です。そうしないと、エミュレーターで苦労して正しく動作しない可能性があります。 次に、GMail(デフォルトでは、Gmailクライアントアプリを持つすべてのAndroidバージョン)またはK9mailなどのメールクライアントをデバイスにインストールする必要があります。_

Step Description
1 You will use Android studio to create an Android application and name it as finddevguides under a package com.example.finddevguides.
2 Modify src/MainActivity.java file and add required code to take care of sending email.
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I’m adding a simple button to launch Email Client.
4 Modify res/values/strings.xml to define required constant values
5 Modify AndroidManifest.xml as shown below
6 Run the application to launch Android emulator and verify the result of the changes done in the application.

次に、変更されたメインアクティビティファイル src/com.example.finddevguides/MainActivity.java の内容を示します。

package com.example.finddevguides;

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はfinddevguidesロゴについて示しています_

<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>

以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">finddevguides</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.finddevguides" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >

      <activity
         android:name="com.example.finddevguides.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>
*finddevguides* アプリケーションを実行してみましょう。 実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオのインストーラーは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。オプションとしてモバイルデバイスを選択し、次の画面を表示するモバイルデバイスを確認します-

Androidモバイルデバイス

[メールの作成]ボタンを使用して、インストールされているすべてのメールクライアントを一覧表示します。 リストから、メールを送信するメールクライアントの1つを選択できます。 以下に示すように、Gmailクライアントを使用して、提供されているすべてのデフォルトフィールドを使用できるメールを送信します。 * From:*は、Androidデバイスに登録したデフォルトのメールIDです。

AndroidモバイルGmail画面

指定されたデフォルトフィールドのいずれかを変更し、最後にメール送信ボタンを使用して、指定された受信者にメールを送信できます。