i am using app engine mail service but i cannot send any mail which contains attachment. Above is the code that implements the mail handler and the exception that i get. I get the exception while trying to send the message using the Transport.send(msg) method.
Mail.java
public class Mail {
private Properties props;
private Session session;
private Message msg;
public Mail() {
props = new Properties();
开发者_高级运维 session = Session.getDefaultInstance(props, null);
msg = new MimeMessage(session);
}
public void sendMail(String subject, String msgBody, String recipient,
String recipientName) {
try {
msg.setFrom(new InternetAddress("ssantaris@gmail.com", "Sourcebay"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
recipient, recipientName));
msg.setSubject(subject);
msg.setText(msgBody);
Transport.send(msg);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sendMimeMessage(String subject, String msgBody,
String recipient, String recipientName, byte[] attachment,
BlobInfo blobInfo) {
try {
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(msgBody, "text/html");
mp.addBodyPart(htmlPart);
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setFileName(blobInfo.getFilename());
attachmentPart.setContent(attachment, blobInfo.getContentType());
mp.addBodyPart(attachmentPart);
msg.setContent(mp);
msg.setFrom(new InternetAddress("ssantaris@gmail.com", "Sourcebay"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
recipient, recipientName));
Transport.send(msg);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Exception
javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Converting attachment data failed)
at javax.mail.Transport.send(Transport.java:163)
at javax.mail.Transport.send(Transport.java:48)
at com.sourcebay.server.mail.Mail.sendMimeMessage(Mail.java:77)
at com.sourcebay.server.source.SourceDownloadImpl.downloadSource(SourceDownloadImpl.java:64)
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:616)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:100)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
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.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:35)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:58)
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:122)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.BackendServersFilter.doFilter(BackendServersFilter.java:97)
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.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:70)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:351)
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.MessagingException: Converting attachment data failed
at com.google.appengine.api.mail.stdimpl.GMTransport.sendMessage(GMTransport.java:231)
at javax.mail.Transport.send(Transport.java:95)
... 43 more
Thanks in advance, Stefanos Antaris
Is ssantaris@gmail.com a registered developer/admin used in the google app engine account? If not, try using the email id of the account that is registered as one.
To set the sender address, the app calls the setFrom() method on the MimeMessage object. The sender must be either the address of a registered developer for the application, or the address of the user for the current request signed in with a Google Account.
精彩评论