Skip to main content

On This Page

How to Send an Email Using MS Exchange Server

2 min read
Share

These articles are AI-generated summaries. Please check the original sources for full details.

How to Send an Email Using MS Exchange Server

Sending emails is a common task in enterprise Java applications, used for transactional emails, notifications, and important updates. This article details configuring and sending emails programmatically using MS Exchange Server in Java, along with common issues and alternatives.

Why This Matters

Ideal models assume seamless email integration, but real-world implementations often face authentication, connection, and security challenges with SMTP servers. Incorrect configurations or network issues can lead to failed email delivery, impacting critical business processes and potentially causing significant operational disruptions and customer dissatisfaction.

Key Insights

  • Jakarta Mail API: Requires jakarta.mail-api and angus-mail dependencies for SMTP implementation.
  • STARTTLS: Enables an encrypted TLS channel for secure email transmission, protecting credentials and content.
  • Microsoft Graph API: Provides a more secure and feature-rich alternative to SMTP, especially for Exchange Online, utilizing OAuth-based authentication.

Working Example

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

public class ExchangeEmailSender {

    public static void main(String[] args) {
        String username = "your_username";
        String password = "your_password";
        String FROM_ADDRESS = "[email protected]";
        String recipient = "[email protected]";
        String subject = "Test Email";
        String body = "This is a test email sent using MS Exchange Server.";

        Properties props = new Properties();
        props.put("mail.smtp.host", "exchange.company.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth.mechanisms", "LOGIN");

        Session session = Session.getInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(FROM_ADDRESS));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
            message.setSubject(subject);
            message.setText(body);

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

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Practical Applications

  • Financial Institutions: Sending transaction confirmations and account statements via Exchange Server.
  • Pitfall: Hardcoding SMTP credentials directly in the application code; use environment variables or a secure configuration management system instead.

References:

Continue reading

Next article

Mastering @IterableMapping in MapStruct

Related Content