Reputation: 39
I have the following class called sendAttachment.java, and also Piechart.java, and covertExcelTOCSV.java. I am reading in an els file converting it to csv and then having piechart read the data make a pie chart save it and then send it into an email attachment. the bellow class sendAttachment.java is giving me a null pointer exception and i don't know how to fix it can someone help?
The error i get is this
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)
My code
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 = "[email protected]";
String to = "[email protected]";
// 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");
}
}
Upvotes: 1
Views: 8253
Reputation: 351
Add this:
fileDataSource.getOutputStream().close();
after you have created and used FileDataSource
object in your code. It should fix the problem (assuming fileDataSource is a FileDataSource object)
also if you are sending attachments in the multipart message first try zipping them in your code and then send . You had encoding issues in this problem , these encoders are friendly with .zip
s .
Upvotes: 2
Reputation: 12751
The NullPointerException is being thrown in MimeUtility (line 226), rather than in the class you posted (though the problem likely originates in your code).
It looks like it has something to do with encoding. Without being able to delve into the code it's hard to know. Some things you could experiment with...
Try removing the parameter when you create the MimeMultipart:
MimeMultipart multipart = new MimeMultipart();
Try attaching a different kind of file, e.g. a text file. Perhaps it can't detect the appropriate encoding for a "jpg" file.
Upvotes: 1