0

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.

5
  • what is Transport? is it a class? Commented Jun 9, 2015 at 9:01
  • You take can an example from here mkyong.com/java/… to check if that code does the job. Commented Jun 9, 2015 at 9:04
  • @nafas you can use it by importing import javax.mail.Transport; Commented Jun 9, 2015 at 9:06
  • The cause is probably new FileDataSource("icon/TIB.png"). You are giving it a relative filename, which means it depends on the current directory of the Java process. Try putting this line before it: System.out.println(new File("icon/TIB.png").exists()); If it prints false, you've found your problem. I suspect you'd be better off accessing the file using Class.getResource and creating a URLDataSource instead. Commented Jun 9, 2015 at 9:15
  • Thanks @VGR .. It is due to the wrong file path for TIB.png... Thanks to all for reply... Commented Jun 10, 2015 at 10:10

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.