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: テキスト本文の変換に失敗しました)

  • for .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 に答える