Getting Null Pointer Exception while sending mail in JAVA

5.5k views Asked by At

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 Transport.send() method.

// 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<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 = "<table border=  \'1\'  align=  \'center\'   cellpadding=  \'10\'  cellspacing=  \'10\'  frame=  \'box\'  rules=  \'all\' ><caption><h1><em><strong>JUnit Test Report</strong></em></h1></caption><tr><th width=\'200\'>Test ID</th><td>r1014_runtime</td></tr><tr><th>Users</th><td>gajoshi<br><br>dsathiya</td></tr><tr><th> Test Suits Failed</th><td><font color=\'blue\'>10</font></td></tr></table><br><footer>--<br><br><div id=\'mainContainer\' style=\'float:left\'></div></footer><br><br><br>";
                 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", "<image>");

                 // 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);
              } 

Now I got following error while running this 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)

Help me please.

1

There are 1 answers

0
jmehrens On BEST ANSWER

Upgrade your JavaMail version 1.4.7 or newer. This is fixed under Avoid NullPointerException when encountering a bad Content-Type.