How can I construct a file from a byte array to send the file as an attachment to an email without saving the file or file path to disk?


Daveson Davey

In Java, I have an array of bytes, usually a file from an external API call.

I need to convert this byte array back to a File and send it as an attachment in an email without creating an actual File or referencing the file path in Disk .

I'm fine with making external API calls and building emails with file attachments. Except for the fact that the file I want to create is saved to disk and then emailed as an attachment.

Tried the usual way of writing to a file using FileOutputStream.

//Convert Byte Array to File
byte[] byteArrayFileObj = someProcess();
File attachmentFile = new File("FileName.abc");
OutputStream os = new FileOutputStream(attachmentFile);
os.write(byteArrayFileObj);
os.close();

//Attach the File as an E-Mail Attachment
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.addAttachment(attachmentFile.getName(), attachmentFile);

I need to send an email with a file attached without saving the file to disk. In my case it saves a copy of the file to disk.

Google a lot. However, there is no answer to this. Thanks in advance. !

David Pérez Cabrera:

Try this:

final InputStreamSource fileStreamSource = new ByteArrayResource(byteArrayFileObj);
helper.addAttachment("Some-file-name", fileStreamSource);

Related


byte array to file object without saving to disk

Kyle Varnson I have a method that byte[]comes from a different part of a .txt or .docx file . I want to create a new object from bytes in order to read the content later without saving the file to disk. is it possible? Or is there a better way to get content f

byte array to file object without saving to disk

Kyle Varnson I have a method that byte[]comes from a different part of a .txt or .docx file . I want to create a new object from bytes in order to read the content later without saving the file to disk. is it possible? Or is there a better way to get content f

byte array to file object without saving to disk

Kyle Varnson I have a method that byte[]comes from a different part of a .txt or .docx file . I want to create a new object from bytes in order to read the content later without saving the file to disk. is it possible? Or is there a better way to get content f

Get file to send as attachment from byte array

Karel I have an ASP.NET MVC application that has to send an email to a list of recipients with an attachment detailing a specific "item". I can get details about this problem from the report by using SQL Server Reporting Services (SSRS). I've never really used

Get file to send as attachment from byte array

Karel I have an ASP.NET MVC application that has to send an email to a list of recipients with an attachment detailing a specific "item". I can get details about this problem from the report by using SQL Server Reporting Services (SSRS). I've never really used

Send uploaded file as attachment without saving to server

Simon Erasmus I have a contact form on my website that allows users to send files as attachments to emails. Now, my reasoning is that I don't need or necessarily want to upload every image to the website server, but I will upload it anyway when using the form

Send uploaded file as attachment without saving to server

Simon Erasmus I have a contact form on my website that allows users to send files as attachments to emails. Now, my reasoning is that I don't need or necessarily want to upload every image to the website server, but I will upload it anyway when using the form

How can I measure the size of a file without saving it to disk?

Marlon Jack I am making a postgres base dump file. By using pg_dump base > base.sql, postgres is created, base.sqlbut until the process is complete, the file appears as 0 bytes. Is there a way to access the size of the file to make some kind of progress bar? I

How can I measure the size of a file without saving it to disk?

Marlon Jack I am making a postgres base dump file. By using pg_dump base > base.sql, postgres is created, base.sqlbut until the process is complete, the file appears as 0 bytes. Is there a way to access the size of the file to make some kind of progress bar? I

Get the file to send as an attachment as a byte array

Karel I have an ASP.NET MVC application that has to send an email to a list of recipients with an attachment detailing a specific "item". I can get details about this action from the report by using SQL Server Reporting Services (SSRS). I've never really used

How can I store a byte array on disk as an image file?

Yatendra Goel: I have a byte array representation of an image. How to save it on disk as an image file. i have already done OutputStream out = new FileOutputStream("a.jpg"); out.write(byteArray); out.flush(); out.close(); However, when I open the image by dou