我有以下代码发送邮件,我收到以下错误。我在Windows 7机器上使用JAVA 7,Eclipse Luna。我正在使用smtp.gmail.com服务器,但每次运行此代码时,我都会在Transport.send()
方法中收到nullpointer异常的错误。
// 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);
}
现在我在运行此代码时出现以下错误。
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)
请帮帮我。
答案 0 :(得分:1)
升级JavaMail 1.4.7或更高版本。这是在Avoid NullPointerException when encountering a bad Content-Type下修复的。