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

วิธีส่งอีเมล HTML ด้วยแอป Android

การส่งอีเมลแบบ HTML ผ่านแอป Android

ตัวอย่างนี้สาธิตวิธีสร้าง Intent สำหรับส่งอีเมลพร้อมเนื้อหา HTML ใช้ Html.fromHtml() จัดรูปแบบข้อความ และ ACTION_SENDTO เพื่อเปิดแอปอีเมล

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

เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp">
    <Button
        android:onClick="sendHtmlEmail"
        android:text="ส่งอีเมล HTML"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)

package app.com.sample;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void sendHtmlEmail(View view) {
        String mailId = "yourmail@gmail.com";
        
        // ใช้ ACTION_SENDTO กับ mailto: เพื่อเปิดเฉพาะแอปอีเมล
        Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", mailId, null));
        
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "หัวข้ออีเมล");
        
        // เนื้อหา HTML
        String htmlContent = "

เนื้อหาทำตัวหนา

" + "https://www.google.com" + "

ข้อความเพิ่มเติมขนาดเล็ก

"; emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(htmlContent, Html.FROM_HTML_MODE_LEGACY)); // แสดง Dialog เลือกแอปอีเมล startActivity(Intent.createChooser(emailIntent, "ส่งอีเมลผ่าน...")); } }

ขั้นตอนที่ 4: กำหนดค่าใน 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>

การทดสอบ

รันแอปพลิเคชัน กดปุ่ม "ส่งอีเมล HTML" จะเปิด Dialog เลือกแอปอีเมล (Gmail, Outlook, ฯลฯ) พร้อมเนื้อหาที่จัดรูปแบบแล้ว

วิธีส่งอีเมล HTML ด้วยแอป Android

แท็ก HTML ที่รองรับใน Android

Html.fromHtml() รองรับแท็ก HTML พื้นฐาน:

  • ข้อความ: <b>, <strong>, <i>, <em>, <u>, <s>, <strike>, <sup>, <sub>, <big>, <small>
  • โครงสร้าง: <p>, <br>, <div>, <blockquote>
  • ลิงก์: <a href="..."> (ต้องตั้งค่า MovementMethod เพื่อคลิกได้)
  • รายการ: <ul>, <ol>, <li>
  • ฟอนต์: <font color="..." face="...">, <tt> (monospace)

สำหรับ Android 7.0+ (API 24+)

// ใช้ flag FROM_HTML_MODE_LEGACY หรือ FROM_HTML_MODE_COMPACT
Html.fromHtml(htmlString, Html.FROM_HTML_MODE_COMPACT);

// หรือใช้ HtmlCompat จาก androidx.core.text
HtmlCompat.fromHtml(htmlString, HtmlCompat.FROM_HTML_MODE_COMPACT);

เพิ่มไฟล์แนบ (Attachment)

// ต้องใช้ ACTION_SEND แทน ACTION_SENDTO
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient@example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(htmlContent));
intent.putExtra(Intent.EXTRA_STREAM, uriOfAttachment); // Uri ของไฟล์
startActivity(Intent.createChooser(intent, "Send Email"));