Java में SMTP सेवा क्लाइंट का उपयोग करके संदेश भेजना
SmtpClient से com.aspose.email.services.inetmail.client पैकेज SMTP सर्वर के माध्यम से संदेश भेजता है। इसे क्लासपाथ पर एक मेल प्रोटोकॉल लाइब्रेरी की आवश्यकता होती है। विवरण के लिए देखें मेल प्रोटोकॉल लाइब्रेरी सेटअप.
com.aspose.email पैकेज में समान नाम की क्लास है, जिसका वर्णन किया गया है SMTP क्लाइंट के साथ काम करना. आवश्यक क्लास चुनने के लिए स्पष्ट इम्पोर्ट का उपयोग करें।
पाठ संदेश भेजना
संदेश बनाएं InetMessage.builder(), और फिर इसे पास करें sendके पहुँचा जाता है। प्रेषक और कम से कम एक प्राप्तकर्ता सेट करें। बिना प्राप्तकर्ता वाला संदेश, send फ़ेंक्स MailClientException सर्वर से कनेक्ट होने से पहले। बिना प्रेषक वाला संदेश उसके प्राप्तकर्ताओं तक बिना From पता, और क्लाइंट लॉग में एक चेतावनी लिखता है।
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.SmtpClient;
import com.aspose.email.services.inetmail.model.InetMessage;
try (SmtpClient smtp = SmtpClient.builder()
.host("smtp.example.com")
.port(587)
.starttls()
.credentials(new PasswordCredentials("user@example.com", "password"))
.build()) {
InetMessage message = InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.build();
smtp.send(message);
}
प्रत्येक कॉल पर send सर्वर से अपना कनेक्शन खोलता है और संदेश भेजे जाने पर बंद कर देता है।
प्रमाणीकरण के बिना कनेक्ट करना
कुछ सर्वर, जैसे स्थानीय नेटवर्क में रिले, बिना प्रमाणीकरण के संदेश स्वीकार करते हैं। ऐसे सर्वर से कनेक्ट होने के लिए, कॉल न करें credentials:
import com.aspose.email.services.inetmail.client.SmtpClient;
import com.aspose.email.services.inetmail.model.InetMessage;
try (SmtpClient smtp = SmtpClient.builder()
.host("relay.example.com")
.port(25)
.noSecurity()
.build()) {
smtp.send(InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.build());
}
स्थानीय होस्ट नाम सेट करना
जब क्लाइंट कनेक्ट होता है, वह स्थानीय कंप्यूटर का नाम सर्वर को भेजता है। इस नाम को प्राप्त करने के लिए, मेल लाइब्रेरी DNS में स्थानीय पता खोजती है। यदि कंप्यूटर का रिवर्स DNS रिकॉर्ड नहीं है, तो लुकअप कई सेकंड ले सकता है, और यह प्रत्येक कॉल पर दोहराता है send. लुकअप को छोड़ने के लिए, नाम के साथ सेट करें localHost:
import com.aspose.email.services.inetmail.auth.PasswordCredentials;
import com.aspose.email.services.inetmail.client.SmtpClient;
SmtpClient smtp = SmtpClient.builder()
.host("smtp.example.com")
.credentials(new PasswordCredentials("user@example.com", "password"))
.localHost("client.example.com")
.build();
प्राप्तकर्ताओं और प्रेषक नाम को सेट करना
यह to, cc और bcc विधियों में एक या अधिक पते स्वीकार किए जाते हैं। प्रेषक के लिए डिस्प्ले नाम दिखाने के लिए, पहले नाम और फिर पता पास करें:
import com.aspose.email.services.inetmail.model.InetMessage;
InetMessage message = InetMessage.builder()
.from("Reports Team", "user@example.com")
.to("user@example.com", "user2@example.com")
.cc("user3@example.com")
.bcc("user4@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.build();
smtp.send(message);
HTML संदेश भेजना
उपयोग करें htmlBody संदेश का HTML संस्करण सेट करने के लिए। यदि आप भी सेट करते हैं textBody, संदेश दोनों संस्करणों को रखता है, और प्राप्तकर्ता का मेल क्लाइंट वह दिखाता है जो वह सपोर्ट करता है:
import com.aspose.email.services.inetmail.model.InetMessage;
InetMessage message = InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is ready.")
.htmlBody("<p>The report is <b>ready</b>.</p>")
.build();
smtp.send(message);
अटैचमेंट जोड़ना
अटैचमेंट जोड़ने के लिए, एक बनाएं InetAttachment एक फ़ाइल नाम, सामग्री प्रकार और डेटा वाला ऑब्जेक्ट, और फिर इसे पास करें attach:
import com.aspose.email.services.inetmail.model.InetAttachment;
import com.aspose.email.services.inetmail.model.InetMessage;
import java.nio.charset.StandardCharsets;
byte[] data = "Total: 42".getBytes(StandardCharsets.UTF_8);
InetMessage message = InetMessage.builder()
.from("user@example.com")
.to("user@example.com")
.subject("Weekly report")
.textBody("The report is attached.")
.attach(InetAttachment.of("report.txt", "text/plain", data))
.build();
smtp.send(message);
त्रुटियों को संभालना
जब संदेश नहीं भेजा जा सकता, जैसे कि सर्वर प्रमाण-पत्र अस्वीकार करता है, send फ़ेंक्स MailClientException, जो विस्तारित करता है java.io.IOException:
import com.aspose.email.services.inetmail.MailClientException;
import com.aspose.email.services.inetmail.model.InetMessage;
try {
smtp.send(InetMessage.builder()
.from("user@example.com")
.to("not a valid address")
.subject("Weekly report")
.textBody("The report is ready.")
.build());
} catch (MailClientException e) {
System.out.println(e.getMessage() + ": " + e.getCause().getMessage());
}