E-mail in Android in Hindi
RGPV University / DIPLOMA_CSE / MOBILE COMPUTING
Sending E-mails Programmatically in Android in Hindi
SEO Optimised Table of Contents for Sending E-mails in Android in Hindi
E-mail in Android in Hindi
Introduction to E-mail Functionality in Android
- Android में E-mail भेजना एक बहुत ही महत्वपूर्ण functionality है, जिसका उपयोग users को notifications, feedback, reports या किसी भी प्रकार की जानकारी भेजने के लिए किया जाता है।
- Android applications में E-mail भेजने के लिए दो मुख्य तरीके होते हैं:
- Intent का उपयोग करके
- Programmatically SMTP (Simple Mail Transfer Protocol) के माध्यम से
- Intent method सरल होता है और सीधे Gmail या अन्य mail apps का उपयोग करता है।
- SMTP method तब उपयोग किया जाता है जब आपको बिना user interaction के background से E-mail भेजना हो।
Send E-mail using Intent in Android
- Intent एक Android component होता है जिससे आप किसी दूसरे app (जैसे Gmail) को invoke कर सकते हैं।
- यह तरीका user interaction पर आधारित होता है, यानी user को manually E-mail भेजने के लिए confirm करना पड़ता है।
Intent emailIntent = new Intent(Intent.ACTION_SEND);/n
emailIntent.setType("message/rfc822");/n
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"receiver@example.com"});/n
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject of email");/n
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body of the email");/n
try {/n
startActivity(Intent.createChooser(emailIntent, "Send email using:"));/n
} catch (android.content.ActivityNotFoundException ex) {/n
Toast.makeText(context, "No email clients installed.", Toast.LENGTH_SHORT).show();/n
}
- ऊपर के कोड में, आप Intent की मदद से किसी भी installed mail client के जरिए E-mail भेज सकते हैं।
Sending E-mails Programmatically in Android in Hindi
Sending E-mails without User Interaction using JavaMail API
- जब आपको बिना user की जानकारी के app के अंदर से ही programmatically E-mail भेजना हो, तो JavaMail API का उपयोग किया जाता है।
- इसके लिए आपको external dependency जोड़नी होती है, जैसे कि JavaMail या Apache Commons Email।
JavaMail API को Gradle में जोड़ना
dependencies {/n
implementation 'com.sun.mail:android-mail:1.6.7'/n
implementation 'com.sun.mail:android-activation:1.6.7'/n
}
Internet Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
JavaMailSender क्लास बनाना
public class JavaMailAPI extends AsyncTask<Void, Void, Void> {/n
private Context context;/n
private Session session;/n/n
private String email;/n
private String subject;/n
private String message;/n/n
public JavaMailAPI(Context context, String email, String subject, String message) {/n
this.context = context;/n
this.email = email;/n
this.subject = subject;/n
this.message = message;/n
}/n/n
@Override/n
protected Void doInBackground(Void... voids) {/n
Properties props = new Properties();/n
props.put("mail.smtp.host", "smtp.gmail.com");/n
props.put("mail.smtp.port", "587");/n
props.put("mail.smtp.auth", "true");/n
props.put("mail.smtp.starttls.enable", "true");/n/n
session = Session.getInstance(props, new Authenticator() {/n
@Override/n
protected PasswordAuthentication getPasswordAuthentication() {/n
return new PasswordAuthentication("your_email@gmail.com", "your_app_password");/n
}/n
});/n/n
try {/n
Message mimeMessage = new MimeMessage(session);/n
mimeMessage.setFrom(new InternetAddress("your_email@gmail.com"));/n
mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));/n
mimeMessage.setSubject(subject);/n
mimeMessage.setText(message);/n/n
Transport.send(mimeMessage);/n
} catch (MessagingException e) {/n
e.printStackTrace();/n
}/n
return null;/n
}/n
}
- यह class AsyncTask के अंदर email भेजने का काम करती है ताकि यह background में execute हो और UI block न हो।
- यहाँ पर आपको अपना Gmail का email और app-specific password डालना होता है (normal password काम नहीं करेगा)।
Gmail App Password कैसे प्राप्त करें?
- अपने Gmail अकाउंट में login करें।
- Google Account settings में जाएं और "Security" टैब पर क्लिक करें।
- "2-Step Verification" enable करें।
- इसके बाद "App Passwords" में जाकर Android App के लिए एक नया password generate करें।
- इसी password का उपयोग ऊपर दिए गए JavaMail API कोड में करें।
JavaMailSender को Call करना
JavaMailAPI javaMailAPI = new JavaMailAPI(this, "to@example.com", "Test Subject", "This is a test message");/n
javaMailAPI.execute();
- उपरोक्त कोड से आप programmatically किसी भी user को email भेज सकते हैं बिना उसके confirmation के।
Comparison Table: Intent vs Programmatic Email
Feature | Using Intent | Using JavaMail API |
---|---|---|
User Interaction | Required | Not Required |
Setup Difficulty | Easy | Moderate to Hard |
Security | High (User controlled) | Depends on Implementation |
Customization | Limited | Full control |
FAQs
Intent
का उपयोग कर सकते हैं। यह user को पहले से installed email apps के माध्यम से email भेजने की सुविधा देता है। इसमें user को email manually भेजने की अनुमति देनी होती है। उदाहरण के लिए: Intent.ACTION_SEND
का प्रयोग करके Gmail या अन्य apps से ईमेल भेजा जाता है।
<uses-permission android:name="android.permission.INTERNET"/>
की आवश्यकता होगी, क्योंकि email भेजने के लिए नेटवर्क access जरूरी है।