Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Email Attachement using byte[] with Java-Mail

Tags:

java

email

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?

like image 893
Nuno Furtado Avatar asked Nov 26 '09 17:11

Nuno Furtado


People also ask

How to send email with attachments using JavaMail API?

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.

How do I send an email in Java?

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.

How to send mimemessage to multiple recipients using Java Mail?

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.

How do I send an email with attachments in it?

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.


2 Answers

Creating a DataSource is the right approach. You don't have to write your own, though. Just use the ByteArrayDataSource from JavaMail.

like image 56
erickson Avatar answered Oct 21 '22 14:10

erickson


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()); 
like image 31
Ashok Patel Avatar answered Oct 21 '22 13:10

Ashok Patel