0
我需要從字節數組(由移動應用程序發佈)創建音頻文件,然後將其作爲附件發送給應用程序引擎(Java)。任何人有任何建議?謝謝你的幫助。App Engine創建併發送電子郵件音頻文件
我需要從字節數組(由移動應用程序發佈)創建音頻文件,然後將其作爲附件發送給應用程序引擎(Java)。任何人有任何建議?謝謝你的幫助。App Engine創建併發送電子郵件音頻文件
您可以發送音頻文件作爲某些音頻類型的電子郵件附件(請參閱list of supported formats)。 您可以使用這樣的代碼:
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]", "Example.com Admin"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]", "Mr. User"));
msg.setSubject("My subject");
String htmlBody; // ...
byte[] attachmentData; // your audio file as an array of bits
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlBody, "text/html");
mp.addBodyPart(htmlPart);
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("myfile.mp3"); // we will use mp3 as an example
DataSource src = new ByteArrayDataSource(attachmentData, "audio/mpeg");
attachment.setDataHandler(new DataHandler(src));
mp.addBodyPart(attachment);
msg.setContent(mp);
Transport.send(msg);
} catch (AddressException e) {
// ...
} catch (MessagingException e) {
// ...
}
我得到一個異常時,我使用的代碼(javax.mail.MessagingException的:轉換附件數據失敗) – Sam 2011-04-23 17:29:51
我糾正使用'DataHandler'和代碼' ByteArrayDataSource'使用此[Google Group thread](http://groups.google.com/group/google-appengine-java/browse_thread/thread/e4bc13e091ba2e26/a54e006f0fda4a7e?#a54e006f0fda4a7e)。你可以試試看,並告訴我它是否有效嗎? – olivierlemasle 2011-04-23 18:47:52
工作。多謝,夥計! – Sam 2011-04-24 00:56:42