title: java email insert parameter values into subject and message
body: <p>i need some help to a quite common issue i guess. I am using a .properties file for defining some static texts in my java application. In this .properties file i have define a subject and a message for an email that i would like to send in a specific case. So here is the relevant part of my application.properties file:</p>
  
  <pre class="lang-none prettyprint-override"><code>fail_msgToAddress = xxxx@yyyy.zzz
  fail_msgSubject = [FAIL TO PROCESS EXAM] Study: %s for Patient %s could not be processed due to invalid examination codes
  fail_msgText = &lt;p&gt;Case: %s, for patient: %s, failed to be processed since it seemed not to correspond to a valid examination type!&lt;/p&gt;&lt;p&gt; Please communicate with Npap lab for specific instructions&lt;/p&gt;
  </code></pre>
  
  <p>In my code i would like to pass two parameters: a study id and a patient name to email subject and message. So i have set some methods to fetch this info from my properties file and i am trying to use String.format method to insert some parameter values into the text:</p>
  
  <pre><code>....
  GlobalConfig cfg = new GlobalConfig();
  String emailAddr = cfg.failMsgToAddress();
  String emailSubj = cfg.failMsgSubject();
  String emailTxt = cfg.failMsgText();
  ...
  try {
      Notifier ntf = new Notifier();
      emailSubj = String.format("[FAIL TO PROCESS EXAM] Study: %s for Patient %s could not be processed due to invalid examination codes", d, dcSerMeta.getAnonymizationData(d).getPatientName());
      emailTxt = String.format(emailTxt, d, dcSerMeta.getAnonymizationData(d).getPatientName());
      ntf.sendMail(emailAddr, emailSubj, emailTxt);
      ....
  }
  </code></pre>
  
  <p>where Notifier is my mail wrapper class. Its sendMail method is a classical implementation for sending html email. </p>
  
  <p>I am using the common javax.mail library and Notifier.sendMail method is:</p>
  
  <pre><code>public boolean sendMail(String msgToAddress, String msgSubject, String msgText) throws GeneralSecurityException {
      boolean result = false;
  
      GlobalConfig cfg = new GlobalConfig();
  
      smtpServer = cfg.getSmtpServer();
      smtpPort = Integer.parseInt(cfg.getSmtpPort());
      smtpUser = cfg.getSmtpUsername();
      smtpPassword = cfg.getSmtpPassword();
      emailSender = cfg.getSmtpSender();
      smtpProtocol = cfg.getSmtpProtocol();
  
      smtpSecure = ("0".equals(cfg.getSmtpSecure())) ? "false" : "true";
  
      log.info("###SMTP server: " + smtpServer + ", SMTP port: " + smtpPort + ", SMTP username: " + smtpUser + ", SMTP password: " + smtpPassword + ", email Sender: " + emailSender + ", smtpSecure: " + smtpSecure + ", smtpProtocol: " + smtpProtocol);
      Properties props = new Properties();
  
      MailSSLSocketFactory socketFactory = new MailSSLSocketFactory(); socketFactory.setTrustedHosts(new String[] { smtpServer });
      socketFactory.setTrustAllHosts(true);
  
      if("true".equals(smtpSecure)) {
  
          props.put("mail.transport.protocol", smtpProtocol);
          props.put("mail.smtps.host", smtpServer);
          props.put("mail.smtps.port", smtpPort);
          props.put("mail.smtps.ssl.enable", "false");
          props.put("mail.smtps.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtps.socketFactory", socketFactory);
  
      } else {
  
          props.put("mail.transport.protocol", smtpProtocol);
          props.put("mail.smtp.host", smtpServer);
          props.put("mail.smtp.port", smtpPort);
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.ssl.enable", "false");            
          props.put("mail.smtps.socketFactory", socketFactory);
      }
  
      Authenticator auth = new SMTPAuthenticator();
  
      javax.mail.Session mailSession = javax.mail.Session.getInstance(props, auth);
  
      mailSession.setDebug(true);
      Transport transport;
          try {
              transport = mailSession.getTransport();
              MimeMessage message = new MimeMessage(mailSession);
                  try {
                      message.setSubject(msgSubject);
                      //message.setContent(msgText, "text/plain");
              message.setContent(msgText, "text/html" );
          message.setFrom(new InternetAddress(emailSender));
          message.addRecipient(Message.RecipientType.TO,
                          new InternetAddress(msgToAddress));
  
          transport.connect();
                  transport.sendMessage(message,
                      message.getRecipients(Message.RecipientType.TO));
                  transport.close();
                  result = true;
              } catch (MessagingException e) {
                  e.printStackTrace();
              }                 
          } catch (NoSuchProviderException e1) {
              e1.printStackTrace();
          }
          return result;
     }      
  </code></pre>
  
  <p>However the String.format method do not seem to work and when i am receiving the email i just get the '%s' instead of the actual parameter values! What am i missing?</p>