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

Cách gửi email HTML trong ứng dụng Android

Giới thiệu

Hướng dẫn này trình bày cách gửi email có định dạng HTML (bold, link, màu sắc, v.v.) từ ứng dụng Android sử dụng Intent.ACTION_SENDTO.

Các bước thực hiệ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 (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="Gửi Email HTML"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

Bước 3: Viết logic xử lý (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";
        
        // Sử dụng ACTION_SENDTO với mailto: để chỉ mở app email
        Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", mailId, null));
        
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Tiêu đề email tại đây");
        
        // Nội dung HTML
        String htmlContent = "<p><b>Nội dung in đậm</b></p>" 
            + "https://www.google.com" 
            + "<small><p>Nội dung nhỏ hơn</p></small>";
        
        emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(htmlContent));
        
        // Hiển thị chooser chọn app email
        startActivity(Intent.createChooser(emailIntent, "Gửi email qua..."));
    }
}

Bước 4: Cấu hình Manifest (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>

Các thẻ HTML được hỗ trợ trong Android

Android chỉ hỗ trợ một tập con HTML cơ bản qua Html.fromHtml():

  • <b>, <strong> - In đậm
  • <i>, <em>, <cite>, <dfn> - In nghiêng
  • <u> - Gạch chân
  • <sub>, <sup> - Chỉ số dưới/trên
  • <big>, <small> - Kích thước lớn/nhỏ
  • <font color="red"> - Màu chữ (chỉ tên màu hoặc hex)
  • <a href="..."> - Link (có thể không hoạt động trong mọi app email)
  • <br> - Xuống dòng
  • <p> - Đoạn văn
  • <blockquote> - Trích dẫn
  • <ul>, <ol>, <li> - Danh sách

Lưu ý quan trọng

  • Sử dụng ACTION_SENDTO với mailto: để chỉ hiển thị ứng dụng email, tránh các app chia sẻ khác
  • Người dùng phải có ứng dụng email (Gmail, Outlook, v.v.) cài đặt sẵn
  • HTML hỗ trợ hạn chế, không hỗ trợ CSS, JavaScript, table phức tạp
  • Cần escape ký tự đặc biệt trong string Java: < → &lt;, > → &gt;

Chạy ứng dụng

Kết nối thiết bị Android, nhấn Run trong Android Studio. Nhấn nút "Gửi Email HTML" để mở chooser chọn app email.

Cách gửi email HTML trong ứng dụng Android