Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีส่งอีเมลผ่าน Gmail จากแอป Android

บทความนี้สาธิตวิธีส่งอีเมลโดยใช้ Intent.ACTION_SEND ซึ่งจะเปิดแอปอีเมลที่ติดตั้งอยู่ (Gmail, Outlook, ฯลฯ) ให้ผู้ใช้เลือก

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout (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:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <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"
        android:inputType="textEmailAddress" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Subject:" />

    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Message:" />

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="4"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Send" />

</LinearLayout>

ขั้นตอนที่ 3: เขียนโค้ด 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;

    @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) {
                sendEmail();
            }
        });
    }

    private void sendEmail() {
        String email = editTextMail.getText().toString().trim();
        String subject = editTextSubject.getText().toString();
        String message = editTextMessage.getText().toString();

        if (email.isEmpty()) {
            editTextMail.setError("Please enter recipient email");
            return;
        }

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{email}); // ต้องเป็น Array
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, message);
        intent.setType("message/rfc822"); // MIME type สำหรับอีเมล

        // Chooser ให้ผู้ใช้เลือกแอปอีเมล
        startActivity(Intent.createChooser(intent, "Select email app"));
    }
}

⚠️ จุดสำคัญที่ต้องแก้จากโค้ดต้นฉบับ

โค้ดต้นฉบับมีบั๊ก: new String(email) → ควรเป็น new String[]{email} (Array)

การระบุเฉพาะ Gmail (Optional)

หากต้องการบังคับใช้ Gmail เท่านั้น:

intent.setPackage("com.google.android.gm"); // package name ของ Gmail
startActivity(intent); // ไม่ต้องใช้ createChooser

ส่งอีเมลแบบมีไฟล์แนบ (Attachment)

Uri fileUri = FileProvider.getUriForFile(this, "com.example.fileprovider", file);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

สิทธิ์ใน Manifest

ไม่ต้องการสิทธิ์พิเศษสำหรับ ACTION_SEND (แต่ถ้ามี attachment ต้องตั้งค่า FileProvider)