这是有时在Appengine上尝试发送带有附件的邮件时有时会遇到的异常:
java.lang.NoClassDefFoundError: java.awt.Component is a restricted class. Please see the Google App Engine developer's guide for more details.
at com.google.apphosting.runtime.security.shared.stub.java.awt.Component.<clinit>(Component.java)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:375)
at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:588)
at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:542)
at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:617)
at javax.activation.DataHandler.getInputStream(DataHandler.java:240)
at javax.activation.DataHandlerDataSource.getInputStream(DataHandler.java:708)
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:764)
at javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:718)
at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:548)
at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:133)
at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:1393)
at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:1366)
at javax.mail.Transport.send(Transport.java:76)
at javax.mail.Transport.send(Transport.java:48)
我使用此代码发送邮件:
Properties props = new Properties();
Session mailSession = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(mailSession);
MimeMultipart multipart = new MimeMultipart();
// first part (the html)
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlText, "text/html");
multipart.addBodyPart(htmlPart);
// second part (the image)
BodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setFileName("screenshot.jpg");
DataSource imageSrc = new ByteArrayDataSource(image, "image/jpeg");
attachmentPart.setDataHandler(new DataHandler(imageSrc));
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
message.saveChanges();
Transport.send(message);
当实例发生此异常时,它将变得不稳定,并且不再能够发送带有附件的电子邮件。我发现的唯一解决方案是杀死实例并启动一个新实例。
最佳答案
“图像”真的是字节数组吗?
堆栈跟踪表明您已将附件设置为Image对象,因此JAF试图找到DataContentHandler来将其转换为字节数组,并且DataContentHandler使用的是不允许的AWT类。默认情况下没有配置这样的DataCOntentHandler,所以我不知道它来自哪里。也许GAE具有不同的配置?
另外,您正在使用什么版本的JavaMail?