This tutorial shows how do we send bulk emails at once. This tutorial uses one method sendEmail() which takes three parameters such as ‘subject’ – Email Subject of String type, ’emailToAddresses’ – List of email addresses where email should be sent and ’emailBodyText’ – the message which has to be sent.

For testing purpose you can also send the email to yourself and after sending the mail, you can open your mailbox, you will find an email has arrived.

Prerequisites

JDK
mail API jar

Now we will create a main class which will send bulk emails to intended recipients

Step 1. Create a java project in any Java based IDE

Step 2. Configure the project to use the mail API jar file

Step 3. Create a main class which will send mail to multiple recipients

package in.webtuts.email;

import java.util.List;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

/**
 *
 * @author https://www.jeejava.com
 */
public class SendEmail {

    public static void main(String[] args) {
    
        //create a list of emails
        List<String> emails = new ArrayList<String>();
        emails.add("email1@email.com");
        emails.add("email2@email.com");
        emails.add("email3@email.com");
        emails.add("email4@email.com");
        emails.add("email5@email.com");
        
        //email subject
        String subject = "Test Email";
        
        //message which is to be sent
        String message = "Test Email Message";
        
        //send the email to multiple recipients
        sendEmail(subject, emails, message);
        
    }
    
    public static void sendEmail(final String subject, final List<String> emailToAddresses,
                final String emailBodyText) {
        
        // from email address
        final String username = "your email address";
        // make sure you put your correct password
        final String password = "your email password";
        // smtp email server
        final String smtpHost = "your mail smtp server host";

        // We will put some properties for smtp configurations
        Properties props = new Properties();

        // do not change - start
        props.put("mail.smtp.user", "username");
        props.put("mail.smtp.host", smtpHost);
        // props.put("mail.debug", "true");
        props.put("mail.smtp.auth", "true");
        // do not change - end

        // we authentcate using your email and password and on successful
        // we create the session
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        String emails = null;
        try {
            // we create new message
            Message message = new MimeMessage(session);
            // set the from 'email address'
            message.setFrom(new InternetAddress(username));
            // set email subject
            message.setSubject(subject);
            // set email message
            // this will send html mail to the intended recipients
            // if you do not want to send html mail then you do not need to wrap the message inside html tags
            String content = "<html>\n<body>\n";
            content += emailBodyText + "\n";
            content += "\n";
            content += "</body>\n</html>";
            message.setContent(content, "text/html");

            // form all emails in a comma separated string
            StringBuilder sb = new StringBuilder();
            int i = 0;
            for (String email : emailToAddresses) {
                sb.append(email);
                i++;
                if (emailToAddresses.size() > i) {
                    sb.append(", ");
                }
            }

            emails = sb.toString();

            // set 'to email address'
            // you can set also CC or TO for recipient type
            message.setRecipients(Message.RecipientType.BCC,
                    InternetAddress.parse(sb.toString()));

            System.out.println("Sending Email to " + emails + " from "
                    + username + " with Subject - " + subject);

            // send the email
            Transport.send(message);

            System.out.println("Email successfully sent to " + emails);

        } catch (MessagingException e) {
            System.out.println("Email sending failed to " + emails);
            System.out.println(e);
        }

    }

}

 

Step 4. Run the application.

That’s all. Thanks for your reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *