Androidでは、 `Intent.ACTION__SEND`を使って既存の電子メールクライアントを呼び出して電子メールを送信することができます。

次のコードスニペットを参照してください。

    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 :"));

__P.SこのプロジェクトはEclipse 3.7で開発され、Samsung Galaxy S2(Android 2.3.3)でテストされました。

1. Androidのレイアウト

ファイル:res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout__width="fill__parent"
    android:layout__height="fill__parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewPhoneNo"
        android:layout__width="wrap__content"
        android:layout__height="wrap__content"
        android:text="To : "
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <EditText
        android:id="@+id/editTextTo"
        android:layout__width="fill__parent"
        android:layout__height="wrap__content"
        android:inputType="textEmailAddress" >

        <requestFocus/>

    </EditText>

    <TextView
        android:id="@+id/textViewSubject"
        android:layout__width="wrap__content"
        android:layout__height="wrap__content"
        android:text="Subject : "
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <EditText
        android:id="@+id/editTextSubject"
        android:layout__width="fill__parent"
        android:layout__height="wrap__content"
         >
    </EditText>

    <TextView
        android:id="@+id/textViewMessage"
        android:layout__width="wrap__content"
        android:layout__height="wrap__content"
        android:text="Message : "
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <EditText
        android:id="@+id/editTextMessage"
        android:layout__width="fill__parent"
        android:layout__height="wrap__content"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="5"/>

    <Button
        android:id="@+id/buttonSend"
        android:layout__width="fill__parent"
        android:layout__height="wrap__content"
        android:text="Send"/>

</LinearLayout>

2.活動

電子メールを送信するフルアクティビティクラス。 `onClick()`メソッドを読んで、それは自明です。

package com.mkyong.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class SendEmailActivity extends Activity {

    Button buttonSend;
    EditText textTo;
    EditText textSubject;
    EditText textMessage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        buttonSend = (Button) findViewById(R.id.buttonSend);
        textTo = (EditText) findViewById(R.id.editTextTo);
        textSubject = (EditText) findViewById(R.id.editTextSubject);
        textMessage = (EditText) findViewById(R.id.editTextMessage);

        buttonSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

              String to = textTo.getText().toString();
              String subject = textSubject.getText().toString();
              String message = textMessage.getText().toString();

              Intent email = new Intent(Intent.ACTION__SEND);
              email.putExtra(Intent.EXTRA__EMAIL, new String[]{ to});
             //email.putExtra(Intent.EXTRA__CC, new String[]{ to});
             //email.putExtra(Intent.EXTRA__BCC, new String[]{to});
              email.putExtra(Intent.EXTRA__SUBJECT, subject);
              email.putExtra(Intent.EXTRA__TEXT, message);

             //need this to prompts email client only
              email.setType("message/rfc822");

              startActivity(Intent.createChooser(email, "Choose an Email client :"));

            }
        });
    }
}

デモ

デフォルトのスクリーを参照し、詳細を記入し、「送信」ボタンをクリックします。


アンドロイドでメールを送信すると、タイトル= "Android-Send-Email-Example-1"、width = 291、height = 480

既存の電子メールクライアントに選択を促すメッセージが表示されます。


アンドロイドでメールを送信すると、タイトル= "Android-Send-Email-Example-2"、width = 291、height = 480

この場合、私は

Gmail

を選択しました。詳細については、

Gmail

クライアントに自動的に入力されます。


アンドロイドでメールを送信すると、タイトル= "Android-Send-Email-Example-3"、width = 291、height = 480

  • 注意** AndroidはEメールを直接送信するためのAPIを提供していません。既存のEメールクライアントに電話をかけてEメールを送信する必要があります。

ソースコードをダウンロードする

ダウンロードする –

Android-Send-Email-Example.zip

(16 KB)