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.
Hvad er JavaMail?
I verden af virksomhedsapplikationer og automatiserede systemer er evnen til at sende og modtage e-mails programmatisk ikke blot en bekvemmelighed—det er en nødvendighed. Fra at sende nulstilling af adgangskoder og ordrebekræftelser til at behandle indkommende support‑tickets, er e‑mail‑integration en kernefunktion. For Java‑udviklere har hjørnestenen i denne funktionalitet været det robuste, open‑source JavaMail‑API. Denne omfattende produktside vil udforske JavaMail‑API’et, dets kraftfulde funktioner og give praktiske kodeeksempler, så du kan komme i gang med e‑mail‑integration i dine Java‑applikationer.
JavaMail‑API’et er et modent, open‑source‑framework leveret af Oracle (tidligere Sun Microsystems), som giver en platform‑uafhængig og protokol‑uafhængig ramme til at bygge mail‑ og besked‑applikationer. Det er standard‑API’et til håndtering af e‑mail i Java‑økosystemet. Det abstraherer kompleksiteten i de underliggende e‑mail‑protokoller som SMTP, POP3 og IMAP, så udviklere kan arbejde med et rent, objekt‑orienteret interface. Der er flere vigtige funktioner i biblioteket, såsom at oprette og sende HTML‑e‑mails, tilføje vedhæftninger, indsætte inline‑billeder, multipart‑indhold, stærk godkendelse og sikkerhedsunderstøttelse, mappe‑baseret e‑mail‑support, bedre søge‑ og filtreringsfunktioner, læsning af e‑mails via SMTP med mere.
Kom i gang med JavaMail
Først og fremmest skal du installere JDK 1.6 eller højere. Du skal tilføje følgende Maven-afhængighed i 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.
Afsendelse af e-mailbesked via Java-bibliotek
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);
}
}
}
E-mail-søgning & filtrering 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 protokolunderstøttelse
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.
Avanceret godkendelse og sikkerhed 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.