title: Getting Null Pointer Exception while sending mail in JAVA
body: <p>I have the following code for sending mail, and I got the following error. I am using JAVA 7, Eclipse Luna, on a Windows 7 machine. I am using the smtp.gmail.com server, but every time I run this code I'm getting an error for nullpointer exception at the <code>Transport.send()</code> method.</p>
  
  <pre><code>// Recipient's email ID needs to be mentioned.
                String to = "***@tibco.com,***@tibco.com";
                String[] mailAddressTo = to.split(",");
  
                // Sender's email ID needs to be mentioned
                String from = "PHD";
  
                // Assuming you are sending email from localhost
                String host = "10.106.136.29";
  
                // 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));
  
                 //can put multiple receivers in the array
                   InternetAddress[] mailAddress_TO = new InternetAddress [mailAddressTo.length] ;
                   for(int i=0;i&lt;mailAddressTo.length;i++){
                       mailAddress_TO[i] = new InternetAddress(mailAddressTo[i]);
                   }
                   message.addRecipients(Message.RecipientType.TO, mailAddress_TO);
  
                   // Set Subject: header field
                   message.setSubject("Test Report!");
  
                // This mail has 2 part, the BODY and the embedded image
                   MimeMultipart multipart = new MimeMultipart("related");
  
                   // first part (the html)
                   BodyPart messageBodyPart = new MimeBodyPart();
                   String htmlText = "&lt;table border=  \'1\'  align=  \'center\'   cellpadding=  \'10\'  cellspacing=  \'10\'  frame=  \'box\'  rules=  \'all\' &gt;&lt;caption&gt;&lt;h1&gt;&lt;em&gt;&lt;strong&gt;JUnit Test Report&lt;/strong&gt;&lt;/em&gt;&lt;/h1&gt;&lt;/caption&gt;&lt;tr&gt;&lt;th width=\'200\'&gt;Test ID&lt;/th&gt;&lt;td&gt;r1014_runtime&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt;Users&lt;/th&gt;&lt;td&gt;gajoshi&lt;br&gt;&lt;br&gt;dsathiya&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;th&gt; Test Suits Failed&lt;/th&gt;&lt;td&gt;&lt;font color=\'blue\'&gt;10&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br&gt;&lt;footer&gt;--&lt;br&gt;&lt;br&gt;&lt;div id=\'mainContainer\' style=\'float:left\'&gt;&lt;/div&gt;&lt;/footer&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;";
                   messageBodyPart.setContent(htmlText, "text/html");
                   // add it
                   multipart.addBodyPart(messageBodyPart);
  
                   // second part (the image)
                   messageBodyPart = new MimeBodyPart();
                   DataSource fds = new FileDataSource("icon/TIB.png");
  
  
  
                   messageBodyPart.setDataHandler(new DataHandler(fds));
                   messageBodyPart.setHeader("Content-ID", "&lt;image&gt;");
  
                   // add image to the multipart
                   multipart.addBodyPart(messageBodyPart);
  
                   // put everything together
                   message.setContent(multipart);
                   // Send message
                   try{
                   Transport.send(message);}
                   catch(NullPointerException e){
                       System.out.println(e + " is occured");
                   }
  
                   System.out.println("Sent message successfully....");
  
                } catch (MessagingException e) {
                   throw new RuntimeException(e);
                } 
  </code></pre>
  
  <p>Now I got following error while running this code.</p>
  
  <pre><code> Exception in thread "main" java.lang.NullPointerException
  at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
  at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
  at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)
  at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
  at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
  at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
  at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
  at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
  at javax.mail.Transport.send(Transport.java:123)
  at SendMail.main(SendMail.java:111)
  </code></pre>
  
  <p>Help me please.</p>