title: Delivery Confirmation For Sent Mail
body: <p>My requirement is to send E-Mail Alerts to various customers for our client for which, we're planning to use JavaMail API. However, once mail is sent, we need to update status in DB as Sent/Delivered/Failure depending on the status of the mail. Please tell me how do we get Delivery Notification for the <strong>mail reaching the Mail Server of the Receiver</strong>. It's not necessary to ensure whether the person has read the mail or not, however, it will be great if we'll be able to know that. The mandatory thing to check is for delivery. How can we get the status. What I read was that using 'SMTPMessage' we can get the status, however, I couldn't find a code sample for how to read the Notification. I am putting my code which is very sample of what I have done till now. Please let me know how I can achieve the thing which we are trying to fulfill.</p>
  
  <pre><code>public class MailSender {
      private int port = 25;
      private String host = "testmailsrvr";
      private String from = "test@test.com";
      private boolean auth = true;
      private String username = "test";
      private String password = "test@123";
      private Protocol protocol = Protocol.SMTP;
      private boolean debug = true;
  
  
      public void sendEmail(String strMailID, String strSubject, String strBody) throws MessagingException{
          Properties props = new Properties();
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", port);
          switch (protocol) {
          case SMTPS:
              props.put("mail.smtp.ssl.enable", true);
              break;
          case TLS:
              props.put("mail.smtp.starttls.enable", true);
              break;
          case SMTP:
              props.put("mail.smtp.ssl.enable", false);
              break;
          }
  
          Authenticator authenticator = null;
          if (auth) {
              props.put("mail.smtp.auth", true);
              authenticator = new Authenticator() {
                  private PasswordAuthentication pa = new PasswordAuthentication(username, password);
                  @Override
                  public PasswordAuthentication getPasswordAuthentication() {
                      return pa;
                  }
              };
          }
  
          Session session = Session.getInstance(props, authenticator);
          session.setDebug(debug);
  
          MimeMessage message = new MimeMessage(session);
          message.setFrom(new InternetAddress(from));
  
          InternetAddress[] toAddress = {new InternetAddress(strMailID)};
          message.setRecipients(Message.RecipientType.TO, toAddress);
  
          message.setSubject(strSubject);
          message.setSentDate(new Date());
  
          message.setText(strBody);
          Transport.send(message);
      }
  }
  </code></pre>