পূর্ব প্রস্তুতি
এই উদাহরণটি `Intent.ACTION_SENDTO` ব্যবহার করে HTML ফরম্যাটে ইমেইল পাঠানোর পদ্ধতি দেখায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project নির্বাচন করুন।
ধাপ ২: 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>
ধাপ ৩: 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")
emailIntent.putExtra(
Intent.EXTRA_TEXT,
Html.fromHtml(
"<p><b>Some Content</b></p>" +
"https://www.google.com" +
"<small><p>More content</p></small>",
Html.FROM_HTML_MODE_LEGACY
)
)
startActivity(Intent.createChooser(emailIntent, "Send email..."))
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.htmlemail">
<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>
পরীক্ষা করুন
অ্যাপ চালান। "Send HTML Email" বাটনে ক্লিক করলে ইমেইল ক্লায়েন্ট খুলবে (Gmail, Outlook ইত্যাদি) যেখানে HTML কন্টেন্ট রেন্ডার হয়ে দেখাবে।
গুরুত্বপূর্ণ নোট
- `ACTION_SENDTO` শুধু ইমেইল অ্যাপগুলো দেখাবে (সহজ শেয়ার মেনু নয়)।
- `Html.fromHtml()` ডিপ্রিকেটেড ভার্সন ব্যবহার করা হয়েছে; API 24+ এ `Html.FROM_HTML_MODE_COMPACT` বা `FROM_HTML_MODE_LEGACY` ব্যবহার করুন।
- সব ইমেইল ক্লায়েন্ট HTML ফুল সাপোর্ট করে না।