1

我有以下名为 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");

    }

}
4

3 回答 3

2

添加这个:

fileDataSource.getOutputStream().close();

在您的代码中创建和使用FileDataSource对象之后。它应该解决问题(假设 fileDataSource 是 FileDataSource 对象)

退房: http: //kenai.com/projects/javamail/sources/mercurial/content/mail/src/main/java/javax/mail/internet/MimeUtility.java ?rev=469

此外,如果您在多部分消息中发送附件,请先尝试将它们压缩到您的代码中,然后发送 . 您在这个问题中遇到了编码问题,这些编码器对.zips 很友好。

于 2012-12-19T08:42:21.200 回答
2

无法读取附件时会发生此错误。

于 2012-08-06T09:19:01.330 回答
1

NullPointerException 是在 MimeUtility (第 226 行)中引发的,而不是在您发布的类中(尽管问题可能源于您的代码)。

看起来它与编码有关。如果无法深入研究代码,就很难知道。有些东西你可以尝试...

创建 MimeMultipart 时尝试删除参数:

MimeMultipart multipart = new MimeMultipart();

尝试附加不同类型的文件,例如文本文件。也许它无法检测到“jpg”文件的适当编码。

于 2012-07-19T17:19:49.553 回答