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

कोटलिन का उपयोग करके एंड्रॉइड एप्लिकेशन से Gmail का उपयोग करके ईमेल कैसे भेजें?

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

यह उदाहरण दर्शाता है कि `ACTION_SEND` इंटेंट का उपयोग करके उपयोगकर्ता के पसंदीदा ईमेल क्लाइंट (Gmail, Outlook, आदि) के माध्यम से ईमेल कैसे भेजा जाए।

चरण 1: नया प्रोजेक्ट बनाएं

एंड्रॉइड स्टूडियो में File → New Project चुनें।

चरण 2: लेआउट (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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"
    android:orientation="vertical"
    android:padding="24dp"
    tools:context=".MainActivity"> 
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="प्राप्तकर्ता ईमेल"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_marginBottom="8dp" /> 
    
    <EditText
        android:id="@+id/editTextMail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="recipient@example.com"
        android:inputType="textEmailAddress"
        android:layout_marginBottom="16dp" /> 
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="विषय"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_marginBottom="8dp" /> 
    
    <EditText
        android:id="@+id/editTextSubject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="ईमेल विषय"
        android:layout_marginBottom="16dp" /> 
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="संदेश"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_marginBottom="8dp" /> 
    
    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:hint="अपना संदेश यहां लिखें..."
        android:gravity="top|start"
        android:inputType="textMultiLine"
        android:layout_marginBottom="24dp" /> 
    
    <Button
        android:id="@+id/buttonSend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ईमेल भेजें"
        android:textSize="18sp"
        android:padding="16dp" /> 
        
</LinearLayout>

चरण 3: MainActivity.kt

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var editTextMail: EditText
    private lateinit var editTextSubject: EditText
    private lateinit var editTextMessage: EditText
    private lateinit var buttonSend: Button
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
        
        editTextMail = findViewById(R.id.editTextMail)
        editTextSubject = findViewById(R.id.editTextSubject)
        editTextMessage = findViewById(R.id.editTextMessage)
        buttonSend = findViewById(R.id.buttonSend)
        
        buttonSend.setOnClickListener { sendEmail() }
    }
    
    private fun sendEmail() {
        val email = editTextMail.text.toString().trim()
        val subject = editTextSubject.text.toString().trim()
        val message = editTextMessage.text.toString().trim()
        
        // सत्यापन
        if (email.isEmpty()) {
            editTextMail.error = "कृपया प्राप्तकर्ता ईमेल दर्ज करें"
            editTextMail.requestFocus()
            return
        }
        
        if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            editTextMail.error = "अमान्य ईमेल प्रारूप"
            editTextMail.requestFocus()
            return
        }
        
        if (subject.isEmpty()) {
            editTextSubject.error = "कृपया विषय दर्ज करें"
            editTextSubject.requestFocus()
            return
        }
        
        if (message.isEmpty()) {
            editTextMessage.error = "कृपया संदेश दर्ज करें"
            editTextMessage.requestFocus()
            return
        }
        
        // ईमेल इंटेंट बनाएं
        val intent = Intent(Intent.ACTION_SENDTO).apply {
            // mailto: स्कीम केवल ईमेल ऐप्स खोलता है
            data = Uri.parse("mailto:$email")
            putExtra(Intent.EXTRA_SUBJECT, subject)
            putExtra(Intent.EXTRA_TEXT, message)
            // एकाधिक प्राप्तकर्ता (वैकल्पिक)
            // putExtra(Intent.EXTRA_CC, arrayOf("cc@example.com"))
            // putExtra(Intent.EXTRA_BCC, arrayOf("bcc@example.com"))
        }
        
        // इंटेंट हल करने योग्य है या नहीं जांचें
        if (intent.resolveActivity(packageManager) != null) {
            startActivity(Intent.createChooser(intent, "ईमेल ऐप चुनें"))
        } else {
            Toast.makeText(this, "कोई ईमेल ऐप नहीं मिला", Toast.LENGTH_LONG).show()
        }
    }
}

चरण 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="https://schemas.android.com/apk/res/android" 
    package="com.example.sendemail"> 
    <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>

उन्नत: HTML ईमेल और अटैचमेंट

// HTML ईमेल के लिए
val htmlMessage = """
    <h2>नमस्ते</h2>
    <p>यह एक <b>HTML</b> ईमेल है।</p>
    <ul>
        <li>आइटम 1</li>
        <li>आइटम 2</li>
    </ul>
""".trimIndent()

val intent = Intent(Intent.ACTION_SEND).apply {
    type = "text/html"
    putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
    putExtra(Intent.EXTRA_SUBJECT, subject)
    putExtra(Intent.EXTRA_HTML_TEXT, htmlMessage)
    // प्लेन टेक्स्ट फॉलबैक
    putExtra(Intent.EXTRA_TEXT, "HTML ईमेल देखने के लिए HTML-सक्षम क्लाइंट का उपयोग करें")
}

// अटैचमेंट के साथ (FILE_PROVIDER आवश्यक)
val attachmentUri = FileProvider.getUriForFile(
    this, 
    "${packageName}.fileprovider", 
    file
)

intent.putExtra(Intent.EXTRA_STREAM, attachmentUri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.type = "*/*" // या विशिष्ट MIME प्रकार

ईमेल इंटेंट प्रकार:

इंटेंट एक्शनMIME प्रकारउपयोग
`ACTION_SENDTO``mailto:`अनुशंसित - केवल ईमेल ऐप्स खोलता है
`ACTION_SEND``text/plain`प्लेन टेक्स्ट ईमेल + अन्य शेयर विकल्प
`ACTION_SEND``text/html`HTML ईमेल
`ACTION_SEND_MULTIPLE``*/*`एकाधिक अटैचमेंट

Gmail-विशिष्ट सुविधाएं (केवल Gmail ऐप में काम करती हैं):

// Gmail के लिए विशेष एक्स्ट्रा (अन्य ऐप्स इन्हें अनदेखा कर सकते हैं)
intent.putExtra("android.content.extra.EMAIL_PACKAGE_NAME", "com.google.android.gm")

// या सीधे Gmail लॉन्च करें (यदि इंस्टॉल है)
val gmailIntent = packageManager.getLaunchIntentForPackage("com.google.android.gm")?.apply {
    putExtra(Intent.EXTRA_EMAIL, arrayOf(email))
    putExtra(Intent.EXTRA_SUBJECT, subject)
    putExtra(Intent.EXTRA_TEXT, message)
}
gmailIntent?.let { startActivity(it) } ?: run {
    // फॉलबैक सामान्य ईमेल इंटेंट पर
}

पृष्ठभूमि में ईमेल भेजना (उपयोगकर्ता इंटरैक्शन के बिना):

उपयोगकर्ता इंटरैक्शन के बिना ईमेल भेजने के लिए, आपको आवश्यकता होगी:

  • JavaMail API: SMTP के माध्यम से सीधे भेजें (पासवर्ड/टोकन आवश्यक)
  • बैकएंड सेवा: अपने सर्वर के माध्यम से भेजें (अनुशंसित सुरक्षा के लिए)
  • Firebase Extensions: "Send Email" एक्सटेंशन का उपयोग करें
// JavaMail उदाहरण (निर्भरता आवश्यक: com.sun.mail:android-mail, android-activation)
Thread {
    val props = Properties().apply {
        put("mail.smtp.host", "smtp.gmail.com")
        put("mail.smtp.port", "587")
        put("mail.smtp.auth", "true")
        put("mail.smtp.starttls.enable", "true")
    }
    
    val session = Session.getInstance(props) { _, _ ->
        PasswordAuthentication("your_email@gmail.com", "app_password")
    }
    
    val message = MimeMessage(session).apply {
        setFrom(InternetAddress("your_email@gmail.com"))
        addRecipient(Message.RecipientType.TO, InternetAddress(email))
        setSubject(subject)
        setText(message)
    }
    
    Transport.send(message)
}.start()

⚠️ सुरक्षा नोट:

ऐप में ईमेल पासवर्ड कभी भी हार्डकोड न करें। ऐप पासवर्ड, OAuth 2.0, या बैकएंड सेवा का उपयोग करें।

  1. - Android में Bundle का उपयोग करके Activity के बीच डेटा कैसे पास करें?

    Bundle Android में डेटा का एक Key-Value कंटेनर है जो Intent के साथ मिलकर Activities के बीच संरचित डेटा स्थानांतरित करने का एक मजबूत तरीका प्रदान करता है। यह दृष्टिकोण तब विशेष रूप से उपयोगी होता है जब आपको कई मान या जटिल डेटा प्रकार पास करने होते हैं। पूर्वापेक्षाएँ Android Studio सेटअप Intent और

  1. - Android में Intent.putExtra() का उपयोग करके Activity के बीच डेटा कैसे भेजें?

    Android में Activities के बीच डेटा पास करने का सबसे आम और सीधा तरीका Intent.putExtra() विधि का उपयोग करना है। यह दृष्टिकोण सरल Key-Value जोड़े के लिए आदर्श है और अधिकांश बुनियादी उपयोग मामलों के लिए पर्याप्त है। पूर्वापेक्षाएँ Android Studio इंस्टॉल Java और Android Fundamentals का बुनियादी ज्ञान

  1. Android ऐप से Gmail का उपयोग करके ईमेल कैसे भेजें? Android ऐप से Gmail का उपयोग करके ईमेल कैसे भेजें?

    Android ऐप से इंटेंट के जरिए ईमेल भेजना एडिटटेक्स्ट से प्राप्तकर्ता, विषय और संदेश लेकर Gmail/ईमेल क्लाइंट चॉइसर खोलने का तरीका। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project। चरण 2: लेआउट में फॉर्म फील्ड्स res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-8?> &