Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Programming

    Уроки React . Урок 10.

    JavaScript

    An Introduction to Finite State Machines: Simplifying React State Management with State Machines

    Java

    Benchmark Java Applications using JMH

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Java / Sending Emails in Java Applications: A Comprehensive Guide to JavaMail and SMTP Configuration
    Java

    Sending Emails in Java Applications: A Comprehensive Guide to JavaMail and SMTP Configuration

    MohanBy MohanJanuary 9, 2024No Comments14 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Sending Emails in Java Applications: A Comprehensive Guide to JavaMail and SMTP Configuration
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Sending Emails in Java Applications: A Comprehensive Guide to JavaMail and SMTP Configuration
    Sending Emails in Java Applications: A Comprehensive Guide to JavaMail and SMTP Configuration

    Table of Contents

    1. Introduction
    2. Setting Up Your JavaMail Environment
    3. Understanding JavaMail Components
    4. Configuring SMTP for JavaMail
    5. Sending Simple Emails
    6. Advanced Email Features
    7. Handling Email Responses
    8. Best Practices for Email Sending in Java
    9. Troubleshooting Common Issues
    10. Conclusion

    1. Introduction

    1.1 Why Email Communication in Java Applications?

    Email communication is a vital aspect of modern applications, facilitating various functionalities like user notifications, password resets, and transactional emails. Integrating email capabilities into Java applications enhances user experience and ensures effective communication between the application and its users.

    1.2 JavaMail API Overview

    The JavaMail API, part of the Java EE (Enterprise Edition) platform, simplifies the process of sending and receiving emails in Java applications. It provides a set of classes and interfaces for working with email protocols, making it a go-to solution for developers looking to implement robust email functionality.

    2. Setting Up Your JavaMail Environment

    2.1 Downloading and Including JavaMail Library

    Before getting started, download the JavaMail library from the official Oracle website or your preferred repository. Once downloaded, include the JAR files in your Java project. For example, if you’re using Maven, add the following dependency to your pom.xml:

    <dependencies>
            <!-- Add JavaMail API dependency -->
            <dependency>
                <groupId>com.sun.mail</groupId>
                <artifactId>javax.mail</artifactId>
                <version>1.6.2</version>
            </dependency>
            <dependency>
                <groupId>com.sun.mail</groupId>
                <artifactId>jakarta.mail</artifactId>
                <version>1.6.7</version>
            </dependency>
    </dependencies>

    2.2 Configuring Your Java Project

    Configure your Java project to use the JavaMail API. If you’re using an Integrated Development Environment (IDE) like Eclipse or IntelliJ, make sure to add the JavaMail library to your project’s build path.

    3. Understanding JavaMail Components

    3.1 Session

    The Session class in JavaMail represents a mail session, which encapsulates the properties and settings for connecting to a mail server. To create a Session instance, you need to use the Session.getInstance() method:

    import javax.mail.Session;
    import java.util.Properties;
    
    public class EmailSender {
        public static void main(String[] args) {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "your-smtp-host.com");
            properties.put("mail.smtp.port", "587");
            properties.put("mail.smtp.auth", "true");
    
            Session session = Session.getInstance(properties);
        }
    }
    

    3.2 Message

    The Message class represents an email message. To create a simple text message, use the MimeMessage class:

    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    public class EmailSender {
        public static void main(String[] args) throws MessagingException {
            // Assuming 'session' is a configured Session instance
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("sender@example.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com"));
            message.setSubject("Hello, JavaMail!");
            message.setText("This is a test email from JavaMail.");
    
            // Now 'message' is ready to be sent
        }
    }
    

    3.3 Transport

    The Transport class handles the actual delivery of email messages. To send a message, use the Transport.send() method:

    import javax.mail.Transport;
    
    public class EmailSender {
        public static void main(String[] args) throws Exception {
            // Assuming 'session' and 'message' are configured instances
            Transport.send(message);
        }
    }
    

    3.4 Store

    The Store class in JavaMail is a fundamental component that facilitates the retrieval of email messages from a mail server. While our focus in this guide has primarily been on sending emails, understanding how to retrieve and manage incoming messages is equally important for comprehensive email functionality in Java applications.

    IMAP: IMAP is a widely used protocol for retrieving emails from a server. It allows users to view and manipulate messages without downloading them to the local machine. IMAP maintains the messages on the server, providing a synchronized view across multiple devices.
    POP3: POP3, on the other hand, is another protocol for email retrieval. Unlike IMAP, POP3 downloads emails to the local machine, removing them from the server. This makes it suitable for scenarios where emails are primarily accessed from a single device.

    3.4.1 Connecting to a Mail Server

    To use the Store class, you first need to obtain a Session object (similar to what we discussed in previous sections). The Session object encapsulates the properties required for establishing a connection to the mail server.

    Here’s a basic example of connecting to an IMAP server:

    import javax.mail.*;
    
    // Assuming 'session' is a valid Session object
    Store store = session.getStore("imaps");  // Use "imap" for non-secure connections
    store.connect("imap.example.com", "your-username", "your-password");
    

    3.4.2 Accessing the Mailbox

    Once the connection is established, you can access the user’s mailbox and retrieve email messages. The Folder class represents a mailbox, and you can use it in conjunction with the Store class to manage messages.

    Folder inbox = store.getFolder("INBOX");
    inbox.open(Folder.READ_ONLY);  // Open the folder in read-only mode
    
    // Retrieve messages from the inbox
    Message[] messages = inbox.getMessages();
    

    3.4.3 Message Retrieval and Handling

    Once you have retrieved the messages, you can iterate through them and perform various actions, such as reading the content, extracting attachments, and managing flags.

    for (Message message : messages) {
        // Retrieve and display message details
        String subject = message.getSubject();
        Address[] from = message.getFrom();
        // Additional details can be retrieved as needed
    
        // Perform actions based on message content
        // ...
    
        // Mark the message as read, if necessary
        message.setFlag(Flags.Flag.SEEN, true);
    }
    

    3.4.4 Choosing between IMAP and POP3

    The choice between IMAP and POP3 depends on the specific requirements of your application:

    Use IMAP if you need to access emails from multiple devices, keep emails synchronized, and perform actions on the server without downloading the entire message.
    Use POP3 if your application is designed for a single device, and you prefer downloading emails to the local machine, removing them from the server.

    Let us understand this concept with a code example in the later part of this article.

    Read More:  Spring Cloud Config Refresh Strategies

    4. Configuring SMTP for JavaMail

    4.1 SMTP Servers and Providers

    Selecting the right SMTP server is crucial for reliable email delivery. Popular providers include Gmail, Outlook, and your organization’s SMTP server. Ensure that you have the correct server address, port, and authentication details. In this article, we will be using the Gmail SMTP server.

    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    

    4.2 Authentication

    SMTP servers often require authentication to ensure that only authorized users can send emails. Provide your email address and password for authentication:

    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.user", "your-email@gmail.com");
    properties.put("mail.smtp.password", "your-email-password");
    

    Note:

    When using Gmail SMTP, you cannot authenticate with your regular Gmail password. Instead, you need to generate a 16-digit app password. Detailed information about this process is available at https://support.google.com/mail/answer/185833?sjid=1520987343100173206-AP.

    4.3 Secure Connections (SSL/TLS)

    When you’re sending emails, especially over the internet, it’s essential to encrypt the communication to safeguard the content of the messages and sensitive information such as login credentials. SSL and TLS are cryptographic protocols that provide secure communication channels, and JavaMail makes it straightforward to enable these protocols.

    // Enable TLS
    properties.put("mail.smtp.starttls.enable", "true");
    
    // Enable SSL
    properties.put("mail.smtp.ssl.enable", "true");
    

    But in this article, we will use TLS.

    5. Sending Simple Emails

    Creating a message involves specifying the sender, recipient, subject, and content. Additionally, you can customize headers and other properties.

    Sending the message using the Transport class remains the same as shown in the previous section.

    package org.example;
    
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.Properties;
    
    public class Main {
        public static void main(String[] args) throws MessagingException, IOException {
            
            // Creating session
            Session session = createSession();
    
            // Create and configure a simple text message
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("fromAddress@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("toAddress@gmail.com"));
    		
    		message.setSubject("Hello, JavaMail!");
            message.setText("This is a test email from JavaMail.");
    		
            // Now 'message' is ready to be sent
            Transport.send(message);
            System.out.println("Email sent successfully!");
        }
    	
    	private static Session createSession() {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.port", "587");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.starttls.enable", "true");
    
            return Session.getInstance(properties, new javax.mail.Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication("fromAddress@gmail@gmail.com", "password");
                }
            });
        }
        
    }
    

    Output:

    Simple Mail from Java
    Simple Mail from Java

    6. Advanced Email Features

    6.1 Adding Attachments
    Including attachments in your emails is a common requirement. The following code snippet demonstrates how to attach a file to your email:

    1. Create a Session object to encapsulate email configuration settings. The createSession() method is presumed to provide a configured session.
    2. Instantiate a MimeMessage object to represent the email message.
    3. Set sender, recipient, and subject attributes of the email message.
    4. Create a MimeBodyPart object to represent the text body of the email. Set the text content of the body part using setText().
    5. Create another MimeBodyPart to represent the attachment.Use attachFile() to attach the specified file to the body part.
    6. Create a MimeMultipart object to hold both the text body and the attachment.Add the created MimeBodyPart instances to the multipart container.
    7. Set the Multipart object as the content of the email message. This indicates that the email contains both text and an attachment.
    8. Use Transport.send() to send the email with the configured message.
    package org.example;
    
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.Properties;
    
    public class Main {
        public static void main(String[] args) throws MessagingException, IOException {
    
            // Creating session
            Session session = createSession();
    
            // Create and configure a simple text message
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("From-Address@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("To-Address@gmail.com"));
    
            mailWithAttachments(session, message);
    
            // Now 'message' is ready to be sent
            Transport.send(message);
            System.out.println("Email sent successfully!");
        }
    	
    	private static Session createSession() {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.port", "587");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.starttls.enable", "true");
    
            return Session.getInstance(properties, new javax.mail.Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication("fromAddress@gmail@gmail.com", "password");
                }
            });
        }
    	
    	public static void mailWithAttachments(Session session, Message message) throws MessagingException, IOException {
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText("This email contains an attachment.");
    
            // Attach the file
            MimeBodyPart attachmentBodyPart = new MimeBodyPart();
            attachmentBodyPart.attachFile("C:/Users/sai.p/Downloads/test.csv");
    
            // Create a multipart message
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            multipart.addBodyPart(attachmentBodyPart);
    
            // Set the multipart as the message's content
            message.setContent(multipart);
        }
        
    }
    

    Output:

    Mail with attachment (CSV file in this case)
    Mail with attachment (CSV file in this case)

    6.2 HTML Emails

    Send HTML-formatted emails for more visually appealing content.

    6.2.1 Setting HTML content from a string.

    package org.example;
    
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.Properties;
    
    public class Main {
        public static void main(String[] args) throws MessagingException, IOException {
    
            // Creating session
            Session session = createSession();
    
            // Create and configure a simple text message
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("From-Address@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("To-Address@gmail.com"));
    
            simpleHtmlMail(message);
    
            // Now 'message' is ready to be sent
            Transport.send(message);
            System.out.println("Email sent successfully!");
        }
    	
    	private static Session createSession() {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.port", "587");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.starttls.enable", "true");
    
            return Session.getInstance(properties, new javax.mail.Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication("fromAddress@gmail.com", "password");
                }
            });
        }
    	
    	public static void simpleHtmlMail(Message message) throws MessagingException, IOException {
            String htmlContent = "This is a simple html <b>content</b> <div style='color:red;padding:8px'>Hello Everyone!</div> ";
            message.setContent(htmlContent,"text/html");
        }
        
    }
    

    Output:

    Mail with HTML Content
    Mail with HTML Content

    6.2.1 Setting HTML content from an HTML file.

    • Read the contents of an HTML.
    • file convert it into a string and pass it to the setContent() method.
    package org.example;
    
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.Properties;
    
    public class Main {
        public static void main(String[] args) throws MessagingException, IOException {
    
            // Creating session
            Session session = createSession();
    
            // Create and configure a simple text message
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("From-Address@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("To-Address@gmail.com"));
    
            htmlMail(message);
    
            // Now 'message' is ready to be sent
            Transport.send(message);
            System.out.println("Email sent successfully!");
        }
    	
    	private static Session createSession() {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.port", "587");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.starttls.enable", "true");
    
            return Session.getInstance(properties, new javax.mail.Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication("fromAddress@gmail.com", "password");
                }
            });
        }
    	
    	public static void htmlMail(Message message) throws MessagingException, IOException {
            String htmlContent = new String(Files.readAllBytes(Paths.get("C:/Users/sai.p/Downloads/greetings.html")));
            message.setContent(htmlContent,"text/html");
        }
        
    }
    
    

    Output:

    Read More:  Centralize The Configuration of Services With Spring Cloud Config
    Mails sent by rendering HTML file
    Mails sent by rendering HTML file

    6.3 Retrieving mail from Mail Inbox.

    1. The readMails method connects to a Gmail account using IMAP and retrieves the last 10 emails from the INBOX folder.
    2. It connects to the Gmail IMAP server using the provided credentials.
    3. Accesses the INBOX folder and opens it in read-only mode.
    4. Retrieves the last 10 messages (or fewer if there are fewer than 10) using inbox.getMessages(inbox.getMessageCount()-10, inbox.getMessageCount()-1).
    5. Iterates through the retrieved messages and prints their subjects and senders.
    package org.example;
    
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.Properties;
    
    public class Main {
            public static void main(String[] args) throws MessagingException, IOException {
    
            // Creating session
            Session session = createSession();
    
            readMails(session);
    
            System.out.println("Email sent successfully!");
        }
    
        public static void readMails(Session session) throws MessagingException {
            Store store = session.getStore("imaps");
            store.connect("imap.gmail.com", "username@gmail.com", "password");
    
            // Access the INBOX folder
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
    
            // Retrieve and display messages
            Message[] messages = inbox.getMessages(inbox.getMessageCount()-10, inbox.getMessageCount()-1);
            for (Message message : messages) {
                System.out.print("Subject: " + message.getSubject());
                System.out.println(", From: " + Arrays.toString(message.getFrom()));
                // Additional details can be displayed
            }
    
            // Close the connection
            inbox.close(false);
            store.close();
        }
    	
    	private static Session createSession() {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.port", "587");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.starttls.enable", "true");
    
            return Session.getInstance(properties, new javax.mail.Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication("username@gmail", "password");
                }
            });
        }
    	
        
    }
    
    

    7. Handling Email Responses

    7.1 Setting Reply-To Address

    Setting a “Reply-To” address in an email message is a useful feature that allows the sender to specify an alternative email address to which recipients should direct their replies. This is particularly beneficial in scenarios where the person initiating the email communication may not be the primary point of contact or where specific departments or individuals handle responses.

    The setReplyTo method is used to configure the “Reply-To” address in the email message.

    message.setReplyTo(new Address[]{new InternetAddress("reply-to@example.com")});
    

    7.2 Handling Bounced Emails

    Handling bounced emails involves monitoring and processing delivery status notifications. While this often requires additional configuration on the server side, you can set the Return-Receipt-To header to request a notification.

    message.setHeader("Return-Receipt-To", "your-email@example.com");

    By configuring the return receipt, the configured mail will receive the return receipt notification.

    8. Best Practices for Email Sending in Java

    8.1 Error Handling and Logging

    Implementing error handling ensures that your application gracefully manages exceptions. Logging mechanisms, such as Java’s built-in logging or third-party libraries like Log4j, can help track issues.

    try {
        // Sending email code here
    } catch (MessagingException e) {
        // Handle messaging exception
        e.printStackTrace(); // Log the exception
    }
    

    8.2 Performance Optimization

    Optimizing email-sending performance involves minimizing network latency and resource consumption. Consider using asynchronous methods or threading for parallel email sending.

    • A loop is used to send five emails concurrently, each in a separate thread.
    • Inside the loop, a lambda expression creates a MimeMessage for each email, sets content, and sends it using a thread from the fixed-size thread pool.
    • An ExecutorService with a fixed thread pool is employed for the concurrent execution of email-sending tasks.
    package org.example;
    
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.Arrays;
    import java.util.Properties;
    
    public class Main {
        public static void main(String[] args) throws MessagingException, IOException {
            // Creating session
            Session session = createSession();
    
            ExecutorService executorService = Executors.newFixedThreadPool(5);
    
            for (int i = 0; i < 5; i++) {
                int finalI = i;
                executorService.submit(() -> {
                    try {
                        // Create and configure a new message for each iteration
                        Message message = new MimeMessage(session);
                        message.setFrom(new InternetAddress("from-address@gmail.com"));
                        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("To-address@gmail.com"));
    
                        // Set the content for this specific message
                        message.setText("Mail number = " + finalI);
    
                        // Send the message
                        Transport.send(message);
                        System.out.println("Email sent successfully for mail number = " + finalI);
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                });
            }
    
            executorService.shutdown();
        }
    
    	private static Session createSession() {
            Properties properties = new Properties();
            properties.put("mail.smtp.host", "smtp.gmail.com");
            properties.put("mail.smtp.port", "587");
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.starttls.enable", "true");
    
            return Session.getInstance(properties, new javax.mail.Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication("username@gmail.com", "password");
                }
            });
        }
    	
        
    }
    
    

    Output:

    Mails sent using Multi-Threading
    Mails sent using Multi-Threading

    9. Troubleshooting Common Issues

    9.1 Connection Problems

    Issue: Unable to connect to the SMTP server.

    Ensure that the SMTP server address, port, and connection properties are correctly configured. Additionally, check your network settings and firewall rules.

    9.2 Authentication Failures

    Issue: Authentication credentials are not accepted by the SMTP server.

    Double-check the correctness of your email address and password. Some SMTP providers might require an application-specific password, so verify the authentication requirements.

    10. Conclusion

    In this comprehensive guide, we’ve covered the fundamentals of sending emails in Java applications using the JavaMail API. From setting up your environment, understanding JavaMail components, and configuring SMTP for email delivery, to implementing advanced features and handling common issues, you now have a solid foundation for incorporating robust email functionality into your Java projects.

    By following best practices, testing thoroughly, and leveraging real-world examples, you can enhance user experience and streamline communication in your applications. Remember to troubleshoot any issues effectively, ensuring that your email-sending functionality is reliable and error-tolerant.

    References:

    1. https://javaee.github.io/javamail/
    2. https://source.unsplash.com/ (For Images)
    3. https://openai.com/dall-e-2 (Thumbnail Image)
    4. https://deepai.org/ (Cover Image)
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Mohan

      Related Posts

      10 Practices You Should Avoid to Become a Good Java Developer

      May 15, 2023

      Spring Security Basics

      December 8, 2020

      Java Stream API

      October 30, 2020
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Entrepreneurship December 5, 2024

      Strategic Approaches to Navigating Entrepreneurship Competition

      In today’s volatile market, entrepreneurs must adopt strategic approaches to effectively navigate competition. This involves thorough market analysis, differentiating product offerings, leveraging technology, and cultivating strong customer relationships to establish a sustainable advantage.

      Tim Burgess: The Future of Work Will Revolve Heavily Around Flexibility

      August 3, 2019

      12. Уроки Node.js . Документация Http Модуля.

      September 21, 2016

      Learn JavaScript and React with the TOP JavaScript YouTube Channels

      May 16, 2019

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      How to implement WPF Canvas? Explain with an example

      Beginners November 4, 2020

      Python map() Function Explained & Visualized

      Programming October 7, 2019

      Mastering JavaScript Proxies: Practical Use Cases and Real-World Applications

      Express.js May 7, 2023

      Understanding The GIT Workflow

      Git January 27, 2020

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      JavaScript

      Navigating Remote Project Management Challenges: Best Practices

      JavaScript

      Building Mobile Apps With Vue3 and Ionic

      JavaScript

      Project Manager Role

      Most Popular

      19. Уроки Node.js. Безопасный Путь к Файлу в fs и path.

      Programming

      Effective Strategies for B2B Lead Generation with Paid Ads

      B2B Leads

      Understanding Python Decorators and How to Use Them Effectively

      Python
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.