title: Sending two different emails at the same time using JavaMail API
body: <p>I'm using JavaMail API to notify the administrator when there's <a href="https://stackoverflow.com/questions/32687280/how-to-get-notified-by-mail-when-rows-are-inserted-into-table">data insertion in database</a> </p>
  
  <p>This works fine. The admin receives the notification of data entry.</p>
  
  <p>However, I also need to notify the client by sending him a confirmation at the same time, but it does not work. The customer does not receive any confirmation. I also want to paste the confirmation number in the subject. Could it be related to port conflict? </p>
  
  <pre><code>EmailAdmin.sendEmail(to, subject, body);
  EmailCustomer.sendEmail(EM, subjectsub, bodybod, CN);
  </code></pre>
  
  <p>EmailAdmin.java</p>
  
  <pre><code>    package session;
  
  import java.util.Date;
  import java.util.Properties;
  import javax.ejb.LocalBean;
  import javax.ejb.Stateless;
  import javax.mail.*;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  
  @Stateless
  @LocalBean
  public class EmailAdmin {
  
      private final int port = 465;
      private final String host = "smtp.server.com";
      private final String from = "admin@domain.com";
      private final boolean auth = true;
      private final String username = "admin@domain.com";
      private final String password = "password";
      private final Protocol protocol = Protocol.SMTPS;
      private final boolean debug = true;
  
      public void sendEmail(String to, String subject, String body) {
  
          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;
          }
  
          Authenticator authenticator = null;
          if (auth) {
              props.put("mail.smtp.auth", true);
              authenticator = new Authenticator() {
                  private final 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);
          try {
              message.setFrom(new InternetAddress(from));
              InternetAddress[] address = {new InternetAddress(to)};
              message.setRecipients(Message.RecipientType.TO, address);
              message.setSubject(subject);
              message.setSentDate(new Date());
              message.setText(body);
  
              Transport.send(message);
          } catch (MessagingException ex) {
          }
      }
  }
  </code></pre>
  
  <p>EmailCustomer.java</p>
  
  <pre><code>    package session;
  
  import java.util.Date;
  import java.util.Properties;
  import javax.ejb.LocalBean;
  import javax.ejb.Stateless;
  import javax.mail.*;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  
  @Stateless
  @LocalBean
  public class EmailCustomer {
  
      private final int port = 465;
      private final String host = "smtp.server.com";
      private final String from = "admin@domain.com";
      private final boolean auth = true;
      private final String username = "admin@domain.com";
      private final String password = "password";
      private final Protocol protocol = Protocol.SMTPS;
      private final boolean debug = true;
  
      public void sendEmail(String EM, String subjectsub, String bodybod, String CN) {
  
          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;
          }
  
          Authenticator authenticator = null;
          if (auth) {
              props.put("mail.smtp.auth", true);
              authenticator = new Authenticator() {
                  private final 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);
          try {
              message.setFrom(new InternetAddress(from));
              InternetAddress[] address = {new InternetAddress(EM)};
              message.setRecipients(Message.RecipientType.TO, address);
              message.setSubject(subject, CN);
              message.setSentDate(new Date());
              message.setText(body);
  
              Transport.send(message);
          } catch (MessagingException ex) {
          }
      }
  }
  </code></pre>