我有以下名为 sendAttachment.java 的类,还有 Piechart.java 和covertExcelTOCSV.java。我正在阅读一个将其转换为 csv 的 els 文件,然后让饼图读取数据,制作饼图保存它,然后将其发送到电子邮件附件中。波纹管类 sendAttachment.java 给了我一个空指针异常,我不知道如何解决它有人可以帮忙吗?
我得到的错误是这个
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 de.vogella.jfreechart.swing.pie.sendAttachment.main(sendAttachment.java:61)
我的代码
package de.vogella.jfreechart.swing.pie;
import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class sendAttachment
{
public static void main(String[] args) throws AddressException, MessagingException, IOException
{
String host = "relay.apple.com";
String from = "jsherif@apple.com";
String to = "techjomana@gmail.com";
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Important Message From VMO");
message.setText("Please see the attached Chart");
// Handle attachment 1
MimeBodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.attachFile("/Users/jomanasherif/Documents/mychart.jpg");
// // Handle attachment 2
// MimeBodyPart messageBodyPart2 = new MimeBodyPart();
// messageBodyPart2.attachFile("c:/Temp/b.txt");
MimeMultipart multipart = new MimeMultipart("mixed");
multipart.addBodyPart(messageBodyPart1);
// multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("message sent");
}
}