Earlier, we sent an email in Java using SMTP using the app password generated for a Gmail account, and this post will discuss how we can send a reply over an email in Java using SMTP.
We used the below code to send an email using SMTP –
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, we will add a new function in this class to reply to an email.
Please remember that you don’t need to add Message-Id or In-Reply-To or anything else explicitly. It will be taken care of automatically in our code.
The new function will only require four parameters –
- To Email
- From Email
- App password
- And a body text
It won’t require a subject because it will automatically be picked from our first email.
Add dependencies
If you have read our previous article, you already have the necessary dependencies or jars, but if you haven’t, please add the dependencies below in your project’s pom.xml file.
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
or you can download the jars and add them to your project.
Let’s write the code
We will already have a Message object from our previous email. Now, we will use it to create a replyMessage to reply to that email.
// "message" object was created when we sent out first email.
// It is used to create a "replyMessage" object
Message replyMessage = new MimeMessage(session);
replyMessage = (MimeMessage) message.reply(false);
replyMessage.setFrom(new InternetAddress(toEmailAddress));
replyMessage.setText(bodyText);
replyMessage.setReplyTo(message.getReplyTo());
replyMessage.setFrom(new InternetAddress(fromEmailAddress));
replyMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmailAddress));
The below function using only four parameters, will send a reply using SMTP.
public void sendReply(String toEmailAddress, String fromEmailAddress, String appPassword, 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");
try {
Message replyMessage = new MimeMessage(session);
replyMessage = (MimeMessage) message.reply(false);
replyMessage.setFrom(new InternetAddress(toEmailAddress));
replyMessage.setText(bodyText);
replyMessage.setReplyTo(message.getReplyTo());
replyMessage.setFrom(new InternetAddress(fromEmailAddress));
replyMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmailAddress));
System.out.println("Sending reply...");
Transport t = session.getTransport("smtp");
t.connect(fromEmailAddress, appPassword);
t.sendMessage(replyMessage, replyMessage.getAllRecipients());
t.close();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("Reply sent successfully...");
}
The whole code will be –
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 {
Session session;
MimeMessage message;
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.getDefaultInstance(prop);
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 void sendReply(String toEmailAddress, String fromEmailAddress, String appPassword, 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");
try {
Message replyMessage = new MimeMessage(session);
replyMessage = (MimeMessage) message.reply(false);
replyMessage.setFrom(new InternetAddress(toEmailAddress));
replyMessage.setText(bodyText);
replyMessage.setReplyTo(message.getReplyTo());
replyMessage.setFrom(new InternetAddress(fromEmailAddress));
replyMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmailAddress));
System.out.println("Sending reply...");
Transport t = session.getTransport("smtp");
t.connect(fromEmailAddress, appPassword);
t.sendMessage(replyMessage, replyMessage.getAllRecipients());
t.close();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("Reply sent successfully...");
}
public static void main(String[] args) {
Codekru codekru = new Codekru();
String toEmailAddress = "admin@codekru.com";
String fromEmailAddress = "testemailforcodekru@gmail.com"; // use your own email here
String appPassword = "******"; // use your app password here
String subject = "Test Subject";
String body = "Test Body";
String replyText = "This is a reply text";
codekru.sendEmail(toEmailAddress, fromEmailAddress, appPassword, subject, body);
codekru.sendReply(toEmailAddress, fromEmailAddress, appPassword, replyText);
}
}
Started
message sent successfully...
Sending reply
Reply sent successfully...
The above code will successfully send a reply to an email in Java using SMTP.
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.