Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序引擎创建并通过电子邮件发送音频文件_Java_Google App Engine_Audio_Attachment - Fatal编程技术网

Java 应用程序引擎创建并通过电子邮件发送音频文件

Java 应用程序引擎创建并通过电子邮件发送音频文件,java,google-app-engine,audio,attachment,Java,Google App Engine,Audio,Attachment,我需要从字节数组(由移动应用程序发布)创建一个音频文件,然后将其作为附件发送到应用程序引擎(Java)中。有人有什么建议吗?感谢您的帮助。对于某些音频类型,您可以将音频文件作为电子邮件附件发送(请参阅)。 您可以使用如下代码: Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); try { Message msg = new MimeMessage

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

对于某些音频类型,您可以将音频文件作为电子邮件附件发送(请参阅)。 您可以使用如下代码:

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) {
    // ...
}

对于某些音频类型,您可以将音频文件作为电子邮件附件发送(请参阅)。 您可以使用如下代码:

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) {
    // ...
}

当我使用该代码(javax.mail.MessagineException:转换附件数据失败)时,我得到了一个异常。我使用该代码更正了代码,以使用
DataHandler
ByteArrayDataSource
。你能试试并告诉我它是否有效吗?我在使用该代码时遇到了一个异常(javax.mail.MessaginException:转换附件数据失败),我更正了该代码,使用了
DataHandler
ByteArrayDataSource
。你能试试看,告诉我它是否有效吗?