We are explaining here an example of replying emails using JavaMail API.
Steps of replying email using JavaMail API:
-
1. Get a session instance from getDefaultInstance() or getInstance() method of Session class.
2. Create the store object (POP3) and connect to the pop store.
3. Create the folder object by calling getFolder() method on store object and open it in mailbox.
4. Get the messages from the folder object.
5. Iterate the emails and get all information like to, from, subject, content etc.
6. Retrieve the email message to be replied.
7. Create the reply message object using Message.reply() method.
8. Use send() method of javax.mail.Transport class to send the reply message.
9. Close the Transport, folder and store objects.
Example:
ReplyEmail.java
import java.io.*; import java.util.*; import javax.mail.*; import javax.mail.internet.*; /** * This class is used to reply email. * @author w3schools */ public class ReplyEmail { final String username = "[email protected]"; final String password = "****"; final String emailSMTPserver = "smtp.gmail.com"; final String emailSMTPPort = "587"; final String mailStoreType = "pop3s"; public ReplyEmail(String repliedText){ Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", emailSMTPserver); props.put("mail.smtp.port", emailSMTPPort); try { Authenticator auth = new SMTPAuthenticator(); Session session = Session.getInstance(props, auth); Store mailStore = session.getStore(mailStoreType); mailStore.connect(emailSMTPserver, username, password); Folder folder = mailStore.getFolder("INBOX"); folder.open(Folder.READ_ONLY); Message[] messages = folder.getMessages(); System.out.println("Total Message - " + messages.length); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); for (int i = 0; i < messages.length; i++) { Message emailMessage = messages[i]; System.out.println(); System.out.println("Email " + (i+1) + " -"); System.out.println("Subject - " + emailMessage.getSubject()); System.out.println("From - " + emailMessage.getFrom()[0]); } System.out.print("Enter email number to " + "which you want to reply: "); String emailNo = reader.readLine(); Message emailMessage = folder.getMessage(Integer.parseInt(emailNo) - 1); Message mimeMessage = new MimeMessage(session); mimeMessage = (MimeMessage) emailMessage.reply(false); mimeMessage.setFrom(new InternetAddress(username)); mimeMessage.setText(repliedText); mimeMessage.setSubject("RE: " + mimeMessage.getSubject()); mimeMessage.addRecipient(Message.RecipientType.TO, emailMessage.getFrom()[0]); Transport.send(mimeMessage); System.out.println("Email message " + "replied successfully."); folder.close(false); mailStore.close(); } catch (Exception e) { e.printStackTrace(); System.err.println("Error in replying email."); } } private class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } } public static void main(String[] args) { new ReplyEmail("Replied Email Text."); } } |
Output:
DEBUG POP3: server doesn't support TOP, disabling it Total Message - 260 Email 1 - Subject - Welcome to Twitter, w3schools From - Twitter <[email protected]> Email 2 - Subject - Hindustan Times retweeted a tweet from Twirus India on Twitter! From - Twitter <[email protected]> Enter email number to which you want to reply: 2 Email message replied successfully. |
Download this example.
Next Topic: Forwarding emails with example.
Previous Topic: Receiving emails with attachments with example.