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

但是在谷歌应用引擎上,此代码失败并出现错误:无法发送电子邮件。MIME 类型应用程序/pdf 的错误无对象 DCH

请告知可能是什么问题。此调试错误出现在以下行

 msg.writeTo(out);
4

2 回答 2

1

尝试使用适用于 GAE 的修补过的 Amazon SES 库。

于 2012-08-06T06:18:40.697 回答
1

Amazon SES 具有有限的允许文件类型列表,附加文件名后缀和 MIME 类型必须匹配。有关允许的 MIME 类型列表,请参阅http://docs.amazonwebservices.com/ses/latest/DeveloperGuide/MIMETypes.html

我记得我很难使附件与 SES 一起工作 - 在某些情况下,MIME 类型在此过程中丢失(未出现在生成的原始电子邮件正文中),这可能是由于一些 JavaMail 错误。

无论如何,这是对我有用的片段:

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 回答