Earlier we send an email to a single recipient using SMTP in Java, and this post will discuss how to send an email to multiple recipients using SMTP.
The Below code will send an email to a single recipient –
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Codekru {
public void sendEmail(String toEmailAddress, String fromEmailAddress, String appPassword, String subject,
String bodyText) {
Properties prop = new Properties();
prop.put("mail.smtp.auth", true);
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.ssl.protocols", "TLSv1.2");
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(fromEmailAddress));
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmailAddress)); // setting "TO" email address
message.setSubject(subject); // setting subject
message.setText(bodyText); // setting body
System.out.println("Sending Email...");
Transport t = session.getTransport("smtp");
t.connect(fromEmailAddress, appPassword);
t.sendMessage(message, message.getAllRecipients());
t.close();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("Email sent successfully...");
}
public static void main(String[] args) {
Codekru codekru = new Codekru();
String toEmailAddress = "admin@codekru.com";
String fromEmailAddress = "testemailforcodekru@gmail.com";
String appPassword = "****"; // Enter your app password here
String subject = "Test Subject";
String body = "Test Body";
codekru.sendEmail(toEmailAddress, fromEmailAddress, appPassword, subject, body);
}
}
Now, how can we send emails to multiple recipients?
We can pass the comma-separated email addresses to send emails to multiple recipients.
String toEmailAddress = "admin@codekru.com,github@codekru.com";
The whole code will look like this –
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Codekru {
public void sendEmail(String toEmailAddress, String fromEmailAddress, String appPassword, String subject,
String bodyText) {
Properties prop = new Properties();
prop.put("mail.smtp.auth", true);
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.ssl.protocols", "TLSv1.2");
Session session = Session.getDefaultInstance(prop);
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(fromEmailAddress));
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmailAddress)); // setting "TO" email address
message.setSubject(subject); // setting subject
message.setText(bodyText); // setting body
System.out.println("Sending Email...");
Transport t = session.getTransport("smtp");
t.connect(fromEmailAddress, appPassword);
t.sendMessage(message, message.getAllRecipients());
t.close();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("Email sent successfully...");
}
public static void main(String[] args) {
Codekru codekru = new Codekru();
String toEmailAddress = "admin@codekru.com,github@codekru.com";
String fromEmailAddress = "testemailforcodekru@gmail.com";
String appPassword = "****"; // Enter your app password here
String subject = "Test Subject";
String body = "Test Body";
codekru.sendEmail(toEmailAddress, fromEmailAddress, appPassword, subject, body);
}
}
Output –
Sending Email...
Email sent successfully...
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.