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

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

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการส่งอีเมลแบบ HTML ในแอป Android โดยใช้ Intent.ACTION_SENDTO และ Html.fromHtml() ด้วย Kotlin

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

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอหลัก (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
    <Button
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendHtmlEmail"
        android:text="Send HTML Email" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.kt

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

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
    }

    fun sendHtmlEmail(view: View?) {
        val mailId = "yourmail@gmail.com"
        val emailIntent = Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", mailId, null))
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject text here")
        
        // เนื้อหา HTML
        val htmlContent = "<p><b>Some Content</b></p>" +
            "https://www.google.com" +
            "<small><p>More content</p></small>"
        
        emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(htmlContent, Html.FROM_HTML_MODE_LEGACY))
        startActivity(Intent.createChooser(emailIntent, "Send email..."))
    }
}

ขั้นตอนที่ 4: ตั้งค่า AndroidManifest.xml

ใช้การตั้งค่ามาตรฐาน (ไม่ต้องเพิ่มสิทธิ์พิเศษสำหรับ ACTION_SENDTO)

ทดสอบการทำงาน

รันแอป กดปุ่ม "Send HTML Email" จะเปิดแอปอีเมล (Gmail, Outlook, etc.) พร้อมกรอก To, Subject, และ Body แบบ HTML

ข้อมูลเพิ่มเติม

  • ACTION_SENDTO vs ACTION_SEND: SENDTO ใช้ mailto: URI เปิดแอปอีเมลโดยตรง SEND ใช้ส่งข้อมูลทุกประเภท (อาจเปิดแอปแชร์อื่นๆ)
  • Html.fromHtml(): API 24+ ใช้ Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY) หรือ FROM_HTML_MODE_COMPACT
  • HTML Tags ที่รองรับ: <b>, <i>, <u>, <p>, <br>, <a href>, <img> (จำกัด), <font color>, <big>, <small>, <blockquote>
  • Attachment: ใช้ Intent.EXTRA_STREAM พร้อม Uri ไฟล์ (ต้องใช้ FileProvider)
  • Multiple Recipients: Intent.EXTRA_EMAIL (String Array) สำหรับ TO, EXTRA_CC, EXTRA_BCC