2011-04-23 93 views

回答

2

您可以發送音頻文件作爲某些音頻類型的電子郵件附件(請參閱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) { 
    // ... 
} 
+0

我得到一個異常時,我使用的代碼(javax.mail.MessagingException的:轉換附件數據失敗) – Sam 2011-04-23 17:29:51

+1

我糾正使用'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

+0

工作。多謝,夥計! – Sam 2011-04-24 00:56:42

相關問題