title: Java 'Email Sending Code' > Known Exception (Index: 14, Size: 14)?
body: <p>i have an automation framework which will send an email when there are test failures, most of the time the code will work however i seem to be getting a strange exception occurring when the following Exception is triggered in turn not sending any emails due to the exception listed: </p>
  
  <pre><code>public class SendEmailFile {
      public static void sendEmailReport(String detailsEmailTitle, String detailsEmailBody) {
          {
              // Recipient's email ID needs to be mentioned.
              String to = "user123@mail.com";
              // Sender's email ID needs to be mentioned
              String from = "user123@mail.com";
  
              // Assuming you are sending email from localhost
              String host = "cluxxxx.eu.messagelabs.com";
              // Get system properties
              Properties properties = System.getProperties();
              // Setup mail server
              properties.setProperty("mail.smtp.host", host);
              // Get the default Session object.
              Session session = Session.getDefaultInstance(properties);
  
              try {
                  // Create a default MimeMessage object.
                  MimeMessage message = new MimeMessage(session);
  
                  // Set From: header field of the header.
                  message.setFrom(new InternetAddress(from));
  
                  // Set To: header field of the header.
                  message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  
                  //CC code works:
                  message.addRecipients(Message.RecipientType.CC, InternetAddress.parse("user321@buyagift.co.uk"));
  
                  // Set Subject: header field
                  message.setSubject(detailsEmailTitle);
  
                  // Create the message part
                  BodyPart messageBodyPart = new MimeBodyPart();
  
  
                  // Create a multipar message
                  Multipart multipart = new MimeMultipart();
  
                  // Set text message part
                  messageBodyPart.setContent(EmailTemplate.setupTemplate(), "text/html");
                  multipart.addBodyPart(messageBodyPart);
  
                  //messageBodyPart.setContent("&lt;h1&gt;This is actual message embedded in HTML tags&lt;/h1&gt;" + "&lt;h2&gt;example text&lt;/h2&gt;", "text/html");
  
                  /**Report Sending Functionality 
                  messageBodyPart = new MimeBodyPart(); 
                  String filename ="C:\\Users\\xxxxx\\Desktop\\xxxxx\\xxxx\\target\\surefire-reports\\emailable-report.html";
                  DataSource source = new FileDataSource(filename);
                  messageBodyPart.setDataHandler(new DataHandler(source));
                  messageBodyPart.setFileName(filename);
                  multipart.addBodyPart(messageBodyPart);
                  ***/
  
                  // Send the complete message parts
                  message.setContent(multipart);
  
                  // Send message
                  Transport.send(message);
                  System.out.println("Sent message successfully....");
  
              } catch (MessagingException mex) {
                  System.out.println("Unable to Send Email!, Exception: " + mex.getMessage());
                  mex.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>Exception which is captured within the Jenkins build when attempting to send an email:</p>
  
  <pre><code>TEST CLASS END TIME: 05.22.45
  Unable to send email which contains test details, Exception: Index: 14, Size: 14
  Tests run: 307, Failures: 81, Errors: 0, Skipped: 204, Time elapsed: 41.355 sec &lt;&lt;&lt; FAILURE! - in TestSuite
  clickOn_PaySecurelyNowButton(BuyFramework.complete.AddingDrivingExperienceToBasketTest)  Time elapsed: 0.246 sec  &lt;&lt;&lt; FAILURE!
  java.lang.NullPointerException
      at BuyFramework.complete.AddingDrivingExperienceToBasketTest.clickOn_PaySecurelyNowButton(AddingDrivingExperienceToBasketTest.java:51)
  
  EmailFormatAndDataCapture(BuyFramework.complete.AddingDrivingExperienceToBasketTest)  Time elapsed: 0.252 sec  &lt;&lt;&lt; FAILURE!
  org.openqa.selenium.UnhandledAlertException: 
  unexpected alert open: {Alert text : Click OK to confirm your personal message is correct as this is exactly how it will be printed.}
    (Session info: chrome=58.0.3029.110)
    (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.10240 x86_64) (WARNING: The server did not provide any stacktrace information)
  Command duration or timeout: 2 milliseconds: null
  Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
  System info: host: 'BG146', ip: '172.16.2.162', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
  Driver info: org.openqa.selenium.chrome.ChromeDriver
  Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\Gxxxx~1.BRU\AppData\Local\Temp\scoped_dir11396_18538}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
  Session ID: a9648e2aa647ebcbc6b69366b783b50c
  </code></pre>