Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Gửi email qua Gmail từ ứng dụng Android

Gửi email qua Gmail từ ứng dụng Android

Hướng dẫn này minh họa cách gửi email sử dụng Intent ACTION_SEND để mở ứng dụng email (Gmail, Outlook, v.v.) với nội dung đã điền sẵn.

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Recipient email!" />
    <EditText
        android:id="@+id/editTextMail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Subject" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editTextSubject" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Message" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editTextMessage"
        android:lines="4" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/buttonSend"
        android:text="Send" />
</LinearLayout>

Bước 3: Code MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    EditText editTextMail, editTextSubject, editTextMessage;
    Button buttonSend;
    String email, subject, message;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        editTextMail = findViewById(R.id.editTextMail);
        editTextSubject = findViewById(R.id.editTextSubject);
        editTextMessage = findViewById(R.id.editTextMessage);
        buttonSend = findViewById(R.id.buttonSend);
        
        buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
                intent.putExtra(Intent.EXTRA_SUBJECT, subject);
                intent.putExtra(Intent.EXTRA_TEXT, message);
                intent.setType("message/rfc822");
                startActivity(Intent.createChooser(intent, "Select email app"));
            }
        });
    }
    
    private void getData() {
        email = editTextMail.getText().toString().trim();
        subject = editTextSubject.getText().toString().trim();
        message = editTextMessage.getText().toString().trim();
    }
}

Bước 4: Cấu hình AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị, chạy app. Điền email người nhận, chủ đề, nội dung và nhấn "Send". Hộp thoại chọn ứng dụng email sẽ hiện ra (Gmail, Outlook, v.v.). Chọn ứng dụng để gửi.

Giải thích quan trọng:
  • Intent.ACTION_SEND: Chia sẻ dữ liệu đến ứng dụng khác
  • message/rfc822: MIME type chuẩn cho email
  • Intent.createChooser(): Hiển thị hộp thoại chọn ứng dụng
  • EXTRA_EMAIL nhận mảng String (có thể gửi cho nhiều người)

Mở rộng: Gửi kèm file đính kèm

// Thêm file đính kèm
Uri fileUri = FileProvider.getUriForFile(this, "com.example.fileprovider", file);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);