2

我正在尝试使用以下代码在 GAE Java 上发送带有附件的电子邮件:

        String htmlBody = "<html><body><p>this is some text</p></body></html>";
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("someemail@someemail.com", "My Name"));
        msg.addRecipient(Message.RecipientType.TO,
                         new InternetAddress(payerEmail));
        msg.setSubject("Your order confirmation");

        Multipart mp = new MimeMultipart();

        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(htmlBody, "text/html");
        mp.addBodyPart(htmlPart);

        MimeBodyPart attachment = new MimeBodyPart();
        attachment.setFileName("tc.MyCustomFile");
        attachment.setContent(byteArray, "application/octet-stream");
        mp.addBodyPart(attachment);
        msg.setContent(mp);
        //msg.set
        Transport.send(msg);

我得到这个例外:

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.xyz.samplepaypal.IPNListenerServlet.doPost(IPNListenerServlet.java:168)
    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.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:102)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35)
    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 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.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:249)
    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.headerComplete(HttpConnection.java:923)
    at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:135)
    at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:477)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:449)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:455)
    at com.google.tracing.TraceContext.runInContext(TraceContext.java:695)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:333)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:325)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:453)
    at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:251)
    at java.lang.Thread.run(Thread.java:679)
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)
    ... 34 more
4

1 回答 1

0

我对 .txt 和 .csv 文件都有同样的问题。这是我得到的错误:

  • 对于 .txt:javax.mail.SendFailedException:发送失败(javax.mail.MessagingException:转换文本正文失败)

  • 对于 .csv:javax.mail.SendFailedException:发送失败(javax.mail.MessagingException:转换附件数据失败)

我发现一种解决方法是使用mar_novice提出的低级 API :

import com.google.appengine.api.mail.MailService.Attachment;
import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailServiceFactory;

...

private void sendEmail(String toEmail, String subject, String attachmentString) throws IOException {
    String msgBody = "Here is your timeseries data for the topic in the subject.";
    byte[] attachmentData = attachmentString.getBytes();
    String fileName = "timeseries.csv";

    MailService service = MailServiceFactory.getMailService(); 
    MailService.Message msg = new MailService.Message(); 

    msg.setSender("<<<<<<REPLACE_WITH_YOUR_SENDER_EMAIL_ADDRESS>>>>>>>"); 
    msg.setTo(toEmail); 

    msg.setSubject(subject); 
    msg.setTextBody(msgBody); 

    Attachment attachmentObject = new Attachment(fileName, attachmentData);
    msg.setAttachments(attachmentObject);// file in binary         

    service.send(msg);
}
于 2014-11-13T23:27:33.780 回答