हम इस ट्यूटोरियल में सीखने वाले है कि जावा सेंडिंग ईमेल (Java Sending Email in Hindi) और हम इसे कैसे इस्तमाल कर सकते हैं? अपने जावा एप्लिकेशन का उपयोग करके एक ई-मेल भेजने के लिए काफी आसान है लेकिन आपके साथ शुरू करने के लिए आपकी मशीन पर जावामेल एपीआई और जावा एक्टिवेशन फ्रेमवर्क (JAF) स्थापित होना चाहिए।
Table of Contents
- आप JavaMail का नवीनतम संस्करण (संस्करण 1.2) जावा की मानक वेबसाइट से डाउनलोड कर सकते हैं।
- आप जावा की मानक वेबसाइट से JAF (संस्करण 1.1.1) का नवीनतम संस्करण डाउनलोड कर सकते हैं।
इन फ़ाइलों को डाउनलोड और अनज़िप करें, नई बनाई गई शीर्ष स्तरीय निर्देशिकाओं में आपको दोनों अनुप्रयोगों के लिए कई जार फ़ाइलें मिलेंगी। आपको अपने CLASSPATH में mail.jar और सक्रियण.jar फ़ाइलों को जोड़ना होगा।
एक साधारण ई-मेल भेजें (Send a Simple E-mail)
आपकी मशीन से एक साधारण ई-मेल भेजने का एक उदाहरण यहां दिया गया है। यह माना जाता है कि आपका लोकलहोस्ट इंटरनेट से जुड़ा है और ई-मेल भेजने में Able है।
Example
// File Name SendEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail {
public static void main(String [] args) {
// Recipient’s email ID needs to be mentioned.
String to = “abcd@gmail.com”;
// Sender’s email ID needs to be mentioned
String from = “web@gmail.com”;
// Assuming you are sending email from localhost
String host = “localhost”;
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty(“mail.smtp.host”, host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject(“This is the Subject Line!”);
// Now set the actual message
message.setText(“This is actual message”);
// Send message
Transport.send(message);
System.out.println(“Sent message successfully….”);
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
एक साधारण ई-मेल भेजने के लिए इस प्रोग्राम को संकलित करें और चलाएं –
Output
$ java SendEmail
Sent message successfully….
यदि आप एक से अधिक प्राप्तकर्ताओं को एक ई-मेल भेजना चाहते हैं तो अनेक ई-मेल आईडी निर्दिष्ट करने के लिए निम्नलिखित विधियों का उपयोग किया जाएगा –
void addRecipients(Message.RecipientType type, Address[] addresses)
throws MessagingException
यहाँ parameters का वर्णन है –
Type − इसे TO, CC या BCC पर सेट किया जाएगा। यहाँ CC कार्बन कॉपी का प्रतिनिधित्व करता है और BCC ब्लैक कार्बन कॉपी का प्रतिनिधित्व करता है। उदाहरण: Message.RecipientType.TO
Addresses − यह ई-मेल आईडी की एक सरणी है। ईमेल आईडी निर्दिष्ट करते समय आपको InternetAddress() पद्धति का उपयोग करना होगा।
एक HTML ईमेल भेजें (Send an HTML E-mail in Hindi)
अपनी मशीन से HTML ई-मेल भेजने का एक उदाहरण यहां दिया गया है। यहां यह माना जाता है कि आपका लोकलहोस्ट इंटरनेट से जुड़ा है और ई-मेल भेजने में Able है।
यह उदाहरण पिछले वाले के समान है, Except इसके कि हम सामग्री सेट करने के लिए सेटकंटेंट () विधि का उपयोग कर रहे हैं जिसका दूसरा तर्क “टेक्स्ट/एचटीएमएल” है, यह निर्दिष्ट करने के लिए कि HTML सामग्री संदेश में शामिल है।
इस उदाहरण का उपयोग करके, आप अपनी पसंद की HTML सामग्री जितनी बड़ी भेज सकते हैं।
Example
// File Name SendHTMLEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendHTMLEmail {
public static void main(String [] args) {
// Recipient’s email ID needs to be mentioned.
String to = “abcd@gmail.com”;
// Sender’s email ID needs to be mentioned
String from = “web@gmail.com”;
// Assuming you are sending email from localhost
String host = “localhost”;
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty(“mail.smtp.host”, host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject(“This is the Subject Line!”);
// Send the actual HTML message, as big as you like
message.setContent(“<h1>This is actual message</h1>”, “text/html”);
// Send message
Transport.send(message);
System.out.println(“Sent message successfully….”);
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
HTML ई-मेल भेजने के लिए इस प्रोग्राम को कंपाइल करें और चलाएं –
Output
$ java SendHTMLEmail
Sent message successfully….
ई-मेल में अटैचमेंट भेजें (Send Attachment in E-mail)
अपनी मशीन से अटैचमेंट के साथ ई-मेल भेजने का एक उदाहरण यहां दिया गया है। यहां यह माना जाता है कि आपका लोकलहोस्ट इंटरनेट से जुड़ा है और ई-मेल भेजने में Able है।
Example
// File Name SendFileEmail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendFileEmail {
public static void main(String [] args) {
// Recipient’s email ID needs to be mentioned.
String to = “abcd@gmail.com”;
// Sender’s email ID needs to be mentioned
String from = “web@gmail.com”;
// Assuming you are sending email from localhost
String host = “localhost”;
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty(“mail.smtp.host”, host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
// Set Subject: header field
message.setSubject(“This is the Subject Line!”);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(“This is message body”);
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = “file.txt”;
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart );
// Send message
Transport.send(message);
System.out.println(“Sent message successfully….”);
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
HTML ई-मेल भेजने के लिए इस प्रोग्राम को कंपाइल करें और चलाएं –
Output
$ java SendFileEmail
Sent message successfully….
उपयोगकर्ता प्रमाणीकरण भाग (User Authentication Part)
यदि प्रमाणीकरण उद्देश्य के लिए ई-मेल सर्वर को यूजर आईडी और पासवर्ड प्रदान करना आवश्यक है, तो आप इन गुणों को निम्नानुसार सेट कर सकते हैं –
props.setProperty(“mail.user”, “myuser”);
props.setProperty(“mail.password”, “mypwd”);
बाकी ई-मेल भेजने का तंत्र ऊपर बताए अनुसार रहेगा।
हम उम्मीद करते है कि आपको “जावा सेंडिंग ईमेल (Java Sending Email in Hindi)” से सम्बंधित जानकारी हिंदी में समझ में आयी होंगी यदि आपको बताई गई जानकारी अच्छी लगी हो तो अपने दोस्तों में ऐसे शेयर करे जिससे उनकी भी हेल्प हो सके धन्यवाद!