Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में फाइल अटैचमेंट के साथ ईमेल कैसे भेजें?

एंड्रॉइड में इंटेंट का उपयोग करके ईमेल भेजना

यह ट्यूटोरियल दर्शाता है कि कैसे एक ईमेल कंपोजर बनाएं जिसमें प्राप्तकर्ता, विषय, संदेश और फाइल अटैचमेंट शामिल हों, फिर डिवाइस पर इंस्टॉल ईमेल ऐप्स का उपयोग करके भेजें।

आवश्यक अनुमतियाँ

<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, "ईमेल ऐप चुनें"))
}
  1. एंड्रॉयड पर एन्क्रिप्टेड ईमेल कैसे भेजें एंड्रॉयड पर एन्क्रिप्टेड ईमेल कैसे भेजें

    अधिकांश भाग के लिए, एन्क्रिप्शन सरकारी एजेंसियों, आपराधिक अदालती मामलों और हैकर्स के बारे में फिल्मों का क्षेत्र लगता है जो बड़ी कंपनियों और बैंकों में घुसने की कोशिश कर रहे हैं। लेकिन एन्क्रिप्शन हमारे ऑनलाइन अनुभव के सभी क्षेत्रों में काम करता है - वेबसाइट फॉर्म से लेकर गूगल ड्राइव दस्तावेजों तक।

  1. ईमेल द्वारा एक्जीक्यूटेबल फ़ाइलें कैसे भेजें ईमेल द्वारा एक्जीक्यूटेबल फ़ाइलें कैसे भेजें

    आपने महसूस किया होगा कि आप केवल उन्हें अपने ईमेल में संलग्न करके एक्जीक्यूटेबल फ़ाइलें नहीं भेज सकते। कुछ पुरानी रणनीतियाँ जैसे फ़ाइल एक्सटेंशन को .jpg में बदलना, या फ़ाइल को ज़िप फ़ोल्डर में डालकर भेजना, या फ़ाइल में एक रैंडम और गैर-मौजूद फ़ाइल एक्सटेंशन जोड़ना, काम नहीं करेंगी। क्या काम करेगा? यह

  1. बड़े वीडियो फ़ाइलों को ईमेल के माध्यम से आसानी से साझा करें: चरण-दर-चरण गाइड बड़े वीडियो फ़ाइलों को ईमेल के माध्यम से आसानी से साझा करें: चरण-दर-चरण गाइड

    ईमेल के माध्यम से बड़ी फ़ाइलें भेजना काफी चुनौतीपूर्ण हो सकता है। ऐसा इसलिए है क्योंकि प्रत्येक ईमेल क्लाइंट की फ़ाइल साझाकरण के लिए एक आकार सीमा होती है।Gmail और Yahoo फ़ाइल आकार को 25MB तक सीमित करते हैं, जबकि Outlook और iCloud फ़ाइलों को 20MB तक सीमित करते हैं।तो, यदि आप उदाहरण के लिए, 25MB से बड