gpt4 book ai didi

java - App Engine 创建音频文件并通过电子邮件发送

转载 作者:行者123 更新时间:2023-12-02 08:19:20 25 4
gpt4 key购买 nike

我需要从字节数组(由移动应用程序发布)创建一个音频文件,然后将其作为应用程序引擎(Java)中的附件通过电子邮件发送。有人有什么建议吗?感谢您的帮助。

最佳答案

对于某些音频类型,您可以将音频文件作为电子邮件附件发送(请参阅 list of supported formats )。您可以使用这样的代码:

Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);

try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("user@example.com", "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) {
// ...
}

关于java - App Engine 创建音频文件并通过电子邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5765059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com