程序部分代码如下:
DataSource ds2 = new FileDataSource( "F:\\java\\logo.jpg");
DataHandler dh2 = new DataHandler(ds2);
attach2.setDataHandler(dh2);
attach1.setFileName("logo.jpg");
老是提示错误:
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 cn.itcast.javamial2.Demo3.main(Demo3.java:87)
原因:网上搜的都说是文件找不到才导致的原因。
首先文件的的确确是存在的。但是使用ds2.getOutputStream().close();也会报错。这就很奇怪了!!还会报其他什么“(文件名、目录名或卷标语法不正确。)”的错误,接着修改成:
String file2 = "F:\\java\\logo.jpg";
DataSource ds2 = new FileDataSource(file2);
就行了。后来这个错误也重现不了。至今百思不得其解。