Free Java API for Sending Simple & HTML-based Emails

Open Source Java Email Processing API to Create and Send Text and HTML-based Email Messages with Attachments, Embedded Images, SMTP, IMAP, POP3 Protocol Support abd Advanced Security support.

Τι είναι το JavaMail;

Στον κόσμο των επιχειρηματικών εφαρμογών και των αυτοματοποιημένων συστημάτων, η δυνατότητα αποστολής και λήψης email προγραμματιστικά δεν είναι απλώς μια ευκολία—είναι μια ανάγκη. Από την αποστολή επαναφοράς κωδικών και επιβεβαιώσεων παραγγελιών μέχρι την επεξεργασία εισερχόμενων αιτημάτων υποστήριξης, η ενσωμάτωση email αποτελεί βασική λειτουργία. Για τους προγραμματιστές Java, το θεμέλιο αυτής της λειτουργικότητας είναι το ισχυρό, ανοιχτού κώδικα JavaMail API. Αυτή η ολοκληρωμένη σελίδα προϊόντος θα εξερευνήσει το JavaMail API, τις ισχυρές δυνατότητές του και θα προσφέρει πρακτικά παραδείγματα κώδικα για να ξεκινήσετε με την ενσωμάτωση email στις εφαρμογές Java.

Το JavaMail API είναι ένα ώριμο, ανοιχτού κώδικα πλαίσιο που παρέχεται από την Oracle (παλαιότερα Sun Microsystems) και προσφέρει ένα πλατφόρμα‑ανεξάρτητο και πρωτόκολλο‑ανεξάρτητο περιβάλλον για την κατασκευή εφαρμογών αλληλογραφίας και μηνυμάτων. Είναι το πρότυπο API για τη διαχείριση email στο οικοσύστημα Java. Αποσπά τις πολυπλοκότητες των υποκείμενων πρωτοκόλλων email όπως SMTP, POP3 και IMAP, επιτρέποντας στους προγραμματιστές να εργάζονται με ένα καθαρό, αντικειμενοστραφές interface. Υπάρχουν πολλές σημαντικές δυνατότητες της βιβλιοθήκης, όπως η δημιουργία και αποστολή HTML email, η προσθήκη συνημμένων, η εισαγωγή ενσωματωμένων εικόνων, το πολυ‑τμηματικό περιεχόμενο, η ισχυρή αυθεντικοποίηση και η υποστήριξη ασφαλείας, η υποστήριξη email με βάση φακέλους, η βελτιωμένη αναζήτηση και φιλτράρισμα email, η ανάγνωση email μέσω SMTP, κ.ά.

Previous Next

Getting Started with JavaMail

First of all, you need to install JDK 1.6 or higher. You need to add the following maven dependency in pom.xml.

Maven Dependency

<dependency>
  <groupId>com.sun.mail</groupId>s;
  <artifactId>javax.mail</artifactId>
  <version>1.6.2</version>
</dependency>

You can download the library directly from GitHub page.

Email Message Sending via Java Library

The open source JavaMail library has included support for creating and sending simple as well as HTML email messages inside Java applications. Software developers can read existing emails, add file as well images as attachments, send email to multiple users, add custom headers, and many more. You need to define connection parameters like the host, port, and flags to enable authentication and TLS. The following example demonstrates how to send a basic plain-text email using an SMTP server (like Gmail's) inside Java applications.

How to Send a Basic Plain-Text Email using an SMTP Server via Java API?


import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class SimpleEmailSender {

    public static void main(String[] args) {
        // Sender's and recipient's email ID
        String from = "your.email@gmail.com";
        String to = "recipient.email@example.com";
        
        // SMTP server configuration (for Gmail)
        String host = "smtp.gmail.com";
        final String username = "your.email@gmail.com";
        final String password = "your-app-password"; // Use an App Password for Gmail

        // Setup mail server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true"); // Use TLS
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "587");

        // Get the Session object and pass username and password
        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            // Create a default MimeMessage object
            Message message = new MimeMessage(session);
            
            // Set From: header field
            message.setFrom(new InternetAddress(from));
            
            // Set To: header field
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            
            // Set Subject: header field
            message.setSubject("Hello from JavaMail API");
            
            // Set the actual message body
            message.setText("This is a test email sent programmatically using the JavaMail API.");

            // Send message
            Transport.send(message);
            System.out.println("Email sent successfully!");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

Email Searching & Filtering via java

The open source JavaMail library has included full support for searching email messages in a folder by various criteria (sender, subject, date, flags) inside java applications. This is especially useful when developing email clients or automations. You can also combine multiple search terms (AND, OR, NOT) via AndTerm, OrTerm, NotTerm. The following example demonstrates, how to search for messages from a specific sender using Java commands.

How to Search for Messages from a Specific Sender inside java Apps?


Store store = session.getStore("imap");
store.connect("imap.example.com", username, password);

Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);

// Search for messages from a specific sender
SearchTerm senderTerm = new FromStringTerm("alerts@example.com");
Message[] found = inbox.search(senderTerm);

for (Message m : found) {
    System.out.println("Subject: " + m.getSubject());
}

inbox.close(false);
store.close();

SMTP, IMAP, POP3 Protocol Support

The open source JavaMail by default supports the three most common email protocols, such as SMTP (Simple Mail Transfer Protocol) for sending messages, POP3 (Post Office Protocol 3) for simple email retrieval and IMAP (Internet Message Access Protocol) for more advanced email access (folders, partial fetch). Moreover, it also supports secure variants like SMTPS, POP3S, IMAPS, and can be extended to custom providers.

Advanced Authentication and Security via Java

The open source JavaMail supports secure communication with email servers, which is crucial in today's security-conscious environment. The library support TLS (Transport Layer Security) which encrypts the communication channel between your application and the mail server. It also supports SSL (Secure Sockets Layer) which is the predecessor to TLS, also supported for establishing a secure connection. The library also supports standard username/password authentication to connect to servers that require login.

 Ελληνικά