खोज…


टिप्पणियों

ओरेकल वेबसाइट पर JavaMail पेज इस प्रकार वर्णन करता है

जावामेल एपीआई मेल और मैसेजिंग एप्लिकेशन के निर्माण के लिए एक प्लेटफॉर्म-स्वतंत्र और प्रोटोकॉल-स्वतंत्र ढांचा प्रदान करता है। JavaMail API, Java SE प्लेटफ़ॉर्म के साथ उपयोग के लिए एक वैकल्पिक पैकेज के रूप में उपलब्ध है और जावा EE प्लेटफ़ॉर्म में भी शामिल है।

JavaMail प्रोजेक्ट की प्राथमिक साइट अब java.net पर है । वहाँ से आप एपीआई के कई संस्करणों के लिए javadocs पा सकते हैं, स्रोत कोड रिपॉजिटरी के लिंक, डाउनलोड के लिए लिंक, उदाहरण और कुछ लोकप्रिय ईमेल सेवा प्रदाताओं के साथ जावामेल का उपयोग करने के लिए संकेत।

POP3 ईमेल सर्वर पर ईमेल भेजना

यह उदाहरण दिखाता है कि SSL-सक्षम POP3 ईमेल सर्वर से कनेक्शन कैसे स्थापित किया जाए और एक साधारण (केवल पाठ) ईमेल भेजा जाए।

    // Configure mail provider
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.mymailprovider.com");
    props.put("mail.pop3.host", "pop3.mymailprovider.com");
    // Enable SSL
    props.put("mail.pop3.ssl.enable", "true");
    props.put("mail.smtp.starttls.enable", "true");

    // Enable SMTP Authentication
    props.put("mail.smtp.auth","true");

    Authenticator auth = new PasswordAuthentication("user", "password");
    Session session = Session.getDefaultInstance(props, auth);

    // Get the store for authentication
    final Store store;
    try {
        store = session.getStore("pop3");
    } catch (NoSuchProviderException e) {
        throw new IllegalStateException(e);
    }

    try {
        store.connect();
    } catch (AuthenticationFailedException | MessagingException e) {
        throw new IllegalStateException(e);
    }
    
    try {
      // Setting up the mail
      InternetAddress from = new InternetAddress("[email protected]");
      InternetAddress to = new InternetAddress("[email protected]");

      MimeMessage message = new MimeMessage(session);
      message.setFrom(from);
      message.addRecipient(Message.RecipientType.TO, to);

      message.setSubject("Test Subject");
      message.setText("Hi, I'm a Mail sent with Java Mail API.");

      // Send the mail
      Transport.send(message);
    } catch (AddressException | MessagingException e)
        throw new IllegalStateException(e);
    } 

चेतावनियां:

  • विभिन्न प्रयोजनों को चित्रण प्रयोजनों के लिए ऊपर दिए गए कोड में हार्डवेअर किया गया है।
  • अपवाद हैंडलिंग परीक्षात्मक नहीं है। IllegalStateException शुरुआत के लिए एक बुरा विकल्प है।
  • संसाधनों को सही ढंग से संभालने के लिए कोई प्रयास नहीं किया गया है।

सरल ईमेल भेजें

public class GoogleMailTest {

    GoogleMailTest() {

    }

    public static void Send(final String username, final String password, String recipientEmail, String title, String message) throws AddressException, MessagingException {
        GoogleMailTest.Send(username, password, recipientEmail, "", title, message);
    }

    public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        // Get a Properties object
        Properties props = System.getProperties();
        props.setProperty("mail.smtps.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.put("mail.debug", "true");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtps.auth", "true");
        props.put("mail.smtps.quitwait", "false");
        Session session = Session.getInstance(props, null);
        // -- Create a new message --
        final MimeMessage msg = new MimeMessage(session);
        // -- Set the FROM and TO fields --
        msg.setFrom(new InternetAddress(username));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));
        JOptionPane.showMessageDialog(null, msg.getSize());
        if (ccEmail.length() > 0) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
        }
        msg.setSubject(title);
        msg.setText(message);
        msg.setSentDate(new Date());
        SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
        t.connect("smtp.gmail.com", username, password);
        t.sendMessage(msg, msg.getAllRecipients());
        t.close();
    }
    //    And use this code in any class, I'm using it in the same class in main method
    public static void main(String[] args) {
        String senderMail = "[email protected]"; //sender mail id
        String password = "769inzimam-9771"; // sender mail password here
        String toMail = "[email protected]"; // recepient  mail id here
        String cc = ""; // cc mail id here
        String title = "Java mail test"; // Subject of the mail
        String msg = "Message here"; // message to be sent

        GoogleMailTest gmt = new GoogleMailTest();

        try {
            if (cc.isEmpty()) {
                GoogleMailTest.Send(senderMail, password, toMail, title, msg);
            } else {
                GoogleMailTest.Send(senderMail, password, toMail, cc, title, msg);
            }
        } catch (MessagingException ex) {
            Logger.getLogger(GoogleMailTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

HTML फॉर्मेटेड मेल भेजें

आप थोड़े से संशोधन के साथ Send Simple Mail के ऊपर उसी उदाहरण का उपयोग कर सकते हैं। msg.setContent() बजाय msg.setText() का उपयोग करें और text/html रूप में सामग्री प्रकार HTML का उपयोग करें।

इसे देखो

msg.setContent(message, "text/html; charset=utf-8");

के बजाय

msg.setText(message);


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow