无法发送附件的电子邮件 - 谷歌应用引擎

时间:2011-10-06 17:22:41

标签: google-app-engine javamail attachment

我正在使用app引擎邮件服务,但我无法发送任何包含附件的邮件。上面是实现邮件处理程序的代码和我得到的异常。尝试使用Transport.send(msg)方法发送消息时出现异常。

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();
    }
}
}

异常

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

提前致谢, Stefanos Antaris

1 个答案:

答案 0 :(得分:0)

ssantaris@gmail.com是谷歌应用引擎帐户中使用的注册开发人员/管理员吗?如果没有,请尝试使用已注册为一个帐户的电子邮件ID。

  

要设置发件人地址,应用程序会调用MimeMessage对象上的setFrom()方法。发件人必须是该应用程序的注册开发人员的地址,或者是使用Google帐户登录的当前请求的用户地址。