0

私は次のコードを持っています:

try{
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
    Message msg = new MimeMessage(session);
    msg.setSubject(emailSubjectTxt);
    msg.setFrom(new InternetAddress(emailFromAddress));
    msg.setRecipient(
        Message.RecipientType.TO, 
        new InternetAddress("vik.ceo@gmail.com"));

    MimeMultipart mp = new MimeMultipart();
    BodyPart part = new MimeBodyPart();
    part.setContent(emailMsgTxt, "text/html");
    mp.addBodyPart(part);
    msg.setContent(mp);
    MimeBodyPart attachment = new MimeBodyPart();
    attachment.setFileName("SupportBySkill.pdf");
    BufferedInputStream bis = new BufferedInputStream(
            SendMail.class.getResourceAsStream("SupportBySkill.pdf"));

    attachment.setContent(bis, "application/pdf");
    mp.addBodyPart(attachment);

      // Capture the raw message
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.writeTo(out);

    RawMessage rm = new RawMessage();
    rm.setData(ByteBuffer.wrap(out.toString().getBytes()));

    ClientConfiguration cc = new ClientConfiguration();
    cc.setHttpClientFactory(new HttpClientFactory() {
        public HttpClient createHttpClient(ClientConfiguration config) {
            return new DefaultHttpClient(new GAEConnectionManager(),
                    new BasicHttpParams());
        }
    });

    // Set AWS access credentials
    AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(
            new BasicAWSCredentials("XXXXXX",
                    "XXXXXX"), cc);

    // Call Amazon SES to send the message
    try {
        client.sendRawEmail(new SendRawEmailRequest().withRawMessage(rm));
    } catch (AmazonClientException e) {
        System.out.println(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }

}catch(Exception e){e.printStackTrace();
log.severe("Could not send email. with error" + e.getMessage());
}

ただし、Google App Engineでは、このコードは次のエラーで失敗します:メールを送信できませんでした。エラーありMIMEタイプapplication/pdfのオブジェクトDCHなし

何が悪いのか教えてください。デバッグ時のこのエラーは次の行に表示されます

 msg.writeTo(out);
4

2 に答える 2

1

Amazon SES には、許可されているファイル タイプの限られたリストがあり、添付されたファイル名のサフィックスと MIME タイプが一致する必要があります。許可されている MIME タイプのリストについては、http://docs.amazonwebservices.com/ses/latest/DeveloperGuide/MIMETypes.htmlを参照してください。

添付ファイルを SES で機能させるのに苦労したことを覚えています。JavaMail のバグが原因で、途中で MIME タイプが失われる場合がありました (生成された未加工の電子メール本文に表示されません)。

とにかく、ここに私のために働くスニペットがあります:

byte[] bytes = getMyFileBytes();
DataSource ds = new ByteArrayDataSource(bytes, getMyMimeType());
MimeBodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(ds));
attachment.setHeader("Content-Type", getMyMimeType()); 
attachment.setFileName(getMyFilename());
multipart.addBodyPart(attachment);
于 2012-08-07T10:37:33.740 に答える
1

GAE で動作する、パッチが適用された Amazon SES ライブラリを使用してみてください。

于 2012-08-06T06:18:40.697 に答える