我正在尝试在Google App Engine(Java)中发送带有内联图像的电子邮件。这是我的代码。
public void send(RecepientInfo[] toAddress, String subject, Attachment[] attachements) throws MessagingException, IOException {
try {
MimeBodyPart textPart = new MimeBodyPart();
MimeBodyPart htmlPart = new MimeBodyPart();
Multipart mp = new MimeMultipart();
msg = new MimeMessage(session);
msg.setFrom(utils.getSenderInfo());
for (RecepientInfo address : toAddress) {
msg.addRecipient(address.getType(), address.getAddress());
}
msg.setSubject(subject);
Template textTpl = cfg.getTemplate("text.ftl");
Template htmlTpl = cfg.getTemplate("html.ftl");
StringWriter swText = new StringWriter();
StringWriter swHtml = new StringWriter();
textTpl.process(parameters, swText);
htmlTpl.process(parameters, swHtml);
String html = swHtml.toString();
String text = swText.toString();
textPart.setText(text, "text/plain");
htmlPart.setContent(html, "text/html");
mp.addBodyPart(textPart);
mp.addBodyPart(htmlPart);
if (attachements != null) {
for (Attachment item : attachements) {
mp.addBodyPart(getImageMultiPart(item.base64Content.getBytes(), item.mimeType, item.contentId, item.fileName, MimeBodyPart.INLINE));
}
}
msg.setContent(mp);
Transport.send(msg);
if (Constants.IS_DEV_MODE) {
System.out.println(text);
}
} catch (TemplateException te) {
throw new IOException(te);
}
}
private BodyPart getImageMultiPart(byte[] base64EncodedImageContentByteArray, String mimeType, String contentId, String fileName, String bodyPart) throws MessagingException {
InternetHeaders headers = new InternetHeaders();
headers.addHeader("Content-Type", mimeType);
headers.addHeader("Content-Transfer-Encoding", "base64");
MimeBodyPart imagePart = new MimeBodyPart(headers, base64EncodedImageContentByteArray);
imagePart.setDisposition(bodyPart);
if (contentId != null) {
imagePart.setContentID("<" + contentId + ">");
}
if (fileName != null) {
imagePart.setFileName(fileName);
}
return imagePart;
}
但是当我尝试发送邮件时,我收到一些错误,如下所示:
com.google.api.server.spi.response.InternalServerErrorException: Send failure (javax.mail.MessagingException: Illegal Arguments (java.lang.IllegalArgumentException: Invalid Attachment Type: Invalid attachment type))
at com.fms.advocacy.api.content.ContentApi.email(ContentApi.java:187)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:130)
at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:363)
at com.google.api.server.spi.SystemServiceServlet.execute(SystemServiceServlet.java:113)
at com.google.api.server.spi.SystemServiceServlet.doPost(SystemServiceServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.fms.advocacy.filters.AccessRuleFilter.doFilter(AccessRuleFilter.java:49)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.fms.advocacy.filters.AuthFilter.doFilter(AuthFilter.java:37)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.fms.advocacy.filters.CorsFilter.doFilter(CorsFilter.java:27)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.fms.advocacy.filters.ErrorHandlerFilter.doFilter(ErrorHandlerFilter.java:36)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:128)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:512)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Illegal Arguments (java.lang.IllegalArgumentException: Invalid Attachment Type: Invalid attachment type))
at javax.mail.Transport.send(Transport.java:163)
at javax.mail.Transport.send(Transport.java:48)
at com.fms.advocacy.AdvocacyMail.send(AdvocacyMail.java:100)
at com.fms.advocacy.api.content.ContentApi.email(ContentApi.java:184)
... 53 more
Caused by: javax.mail.MessagingException: Illegal Arguments (java.lang.IllegalArgumentException: Invalid Attachment Type: Invalid attachment type)
at com.google.appengine.api.mail.stdimpl.GMTransport.sendMessage(GMTransport.java:231)
at javax.mail.Transport.send(Transport.java:95)
... 56 more
Caused by: java.lang.IllegalArgumentException: Invalid Attachment Type: Invalid attachment type
at com.google.appengine.api.mail.MailServiceImpl.doSend(MailServiceImpl.java:113)
at com.google.appengine.api.mail.MailServiceImpl.send(MailServiceImpl.java:34)
at com.google.appengine.api.mail.stdimpl.GMTransport.sendMessage(GMTransport.java:223)
... 57 more
谁能告诉我为什么我会收到此错误?
解决方案很愚蠢。
当我设置setFileName
时,我必须传递一个带有适当扩展名的字符串。
所以而不是
imagePart.setFileName(fileName);
我必须使用
imagePart.setFileName(fileName + "." + mimeType.substring(mimeType.indexOf("/") + 1));