एंड्रॉइड में इंटेंट का उपयोग करके ईमेल भेजना
यह ट्यूटोरियल दर्शाता है कि कैसे एक ईमेल कंपोजर बनाएं जिसमें प्राप्तकर्ता, विषय, संदेश और फाइल अटैचमेंट शामिल हों, फिर डिवाइस पर इंस्टॉल ईमेल ऐप्स का उपयोग करके भेजें।
आवश्यक अनुमतियाँ
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
लेआउट (activity_main.xml)
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/etTo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="प्राप्तकर्ता का ईमेल पता"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="विषय दर्ज करें" />
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="top|start"
android:hint="ईमेल लिखें"
android:inputType="textMultiLine" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="8dp">
<Button
android:id="@+id/btAttachment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="अटैचमेंट" />
<Button
android:id="@+id/btSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="भेजें" />
</LinearLayout>
<TextView
android:id="@+id/tvAttachment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:drawableStart="@drawable/ic_attach"
android:padding="8dp" />
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText etEmail, etSubject, etMessage;
private Button btnSend, btnAttachment;
private TextView tvAttachment;
private Uri attachmentUri = null;
private static final int PICK_FILE_REQUEST = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etEmail = findViewById(R.id.etTo);
etSubject = findViewById(R.id.etSubject);
etMessage = findViewById(R.id.etMessage);
btnSend = findViewById(R.id.btSend);
btnAttachment = findViewById(R.id.btAttachment);
tvAttachment = findViewById(R.id.tvAttachment);
btnAttachment.setOnClickListener(v -> openFilePicker());
btnSend.setOnClickListener(v -> sendEmail());
}
private void openFilePicker() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*"); // सभी फाइल टाइप्स
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "फाइल चुनें"), PICK_FILE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FILE_REQUEST && resultCode == RESULT_OK && data != null) {
attachmentUri = data.getData();
tvAttachment.setText("अटैच्ड: " + getFileName(attachmentUri));
tvAttachment.setVisibility(View.VISIBLE);
}
}
private String getFileName(Uri uri) {
String result = null;
if (uri.getScheme().equals("content")) {
try (android.database.Cursor cursor = getContentResolver().query(uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(android.provider.OpenableColumns.DISPLAY_NAME);
if (nameIndex >= 0) result = cursor.getString(nameIndex);
}
}
}
return result != null ? result : uri.getLastPathSegment();
}
private void sendEmail() {
String email = etEmail.getText().toString().trim();
String subject = etSubject.getText().toString().trim();
String message = etMessage.getText().toString().trim();
if (email.isEmpty()) {
Toast.makeText(this, "कृपया प्राप्तकर्ता ईमेल दर्ज करें", Toast.LENGTH_SHORT).show();
return;
}
try {
Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
if (attachmentUri != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, attachmentUri);
}
startActivity(Intent.createChooser(emailIntent, "ईमेल भेजने के लिए ऐप चुनें"));
} catch (Exception e) {
Toast.makeText(this, "ईमेल भेजने में विफल: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
महत्वपूर्ण बिंदु
ACTION_SENDसिंगल अटैचमेंट के लिए,ACTION_SEND_MULTIPLEमल्टीपल के लिए- MIME टाइप
message/rfc822केवल ईमेल ऐप्स को फ़िल्टर करता है - FileProvider का उपयोग करें यदि
file://URI शेयर कर रहे हैं (एंड्रॉइड 7.0+) - बड़े अटैचमेंट्स के लिए परमिशन और स्टोरेज एक्सेस की आवश्यकता हो सकती है
Kotlin संस्करण (संक्षिप्त)
private fun sendEmail() {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "message/rfc822"
putExtra(Intent.EXTRA_EMAIL, arrayOf(etEmail.text.toString()))
putExtra(Intent.EXTRA_SUBJECT, etSubject.text.toString())
putExtra(Intent.EXTRA_TEXT, etMessage.text.toString())
attachmentUri?.let { putExtra(Intent.EXTRA_STREAM, it) }
}
startActivity(Intent.createChooser(intent, "ईमेल ऐप चुनें"))
}