I have a byte array wich i wish to add as an attachement to an email i am sending.
Unfortunally i can't find how to attach it as a byte array, the solution i have uses disk files (which i dont want since i dont want to write the byte array just so i can attach it).
I've found one solution that involves creating an object that extends DataSource and use this as a wrapper for the byte array and then feed that to the MimeBodyPart.
Anyone know of a better solution?
Sending email with attachment using JavaMail API. There are total 7 steps for sending attachment with email. They are: Get the session object. compose message. create MimeBodyPart object and set your message text. create new MimeBodyPart object and set DataHandler object to this object.
Java - Sending Email. To send an e-mail using your Java Application is simple enough but to start with you should have JavaMail API and Java Activation Framework (JAF) installed on your machine. You can download latest version of JavaMail (Version 1.2) from Java's standard website.
Create a MimeMessage object by passing the session object created in previous step. import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; Sending Email to multiple recipients is same as that for single recipient. The difference is that to send a mail to multiple recipients you should add multiple recipients.
Sending Mail With Attachments First, we need to configure the email service provider's credentials. Then, the Session object is created by providing the email host, port, username, and password. All these details are provided by the email host service. We can use any fake SMTP testing servers for our code.
Creating a DataSource
is the right approach. You don't have to write your own, though. Just use the ByteArrayDataSource
from JavaMail.
Here is the code for your requirement...store attachment file as BLOB in DB and fetch that for sending it as a attachment in mail...............
import java.io.*;
import java.util.*;
import javax.activation.*;
public class BufferedDataSource implements DataSource {
private byte[] _data;
private java.lang.String _name;
public BufferedDataSource(byte[] data, String name) {
_data = data;
_name = name;
}
public String getContentType() { return "application/octet-stream";}
public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(_data);}
public String getName() { return _name;}
/**
* Returns an OutputStream from the DataSource
* @returns OutputStream Array of bytes converted into an OutputStream
*/
public OutputStream getOutputStream() throws IOException {
OutputStream out = new ByteArrayOutputStream();
out.write(_data);
return out;
}
}
===========================================================
//Getting ByteArray From BLOB
byte[] bytearray;
BLOB blob = ((OracleResultSet) rs).getBLOB("IMAGE_GIF");
if (blob != null) {
BufferedInputStream bis = new BufferedInputStream(blob.getBinaryStream());
ByteArrayOutputStream bao = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int length = 0;
while ((length = bis.read(buffer)) != -1) {
bao.write(buffer, 0, length);
}
bao.close();
bis.close();
bytearray = bao.toByteArray();
}
===============================================================
//Attach File for mail
MimeBodyPart att = new MimeBodyPart();
BufferedDataSource bds = new BufferedDataSource(bytearray, "AttName");
att.setDataHandler(new DataHandler(bds));
att.setFileName(bds.getName());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With