jQuery in Action, 2nd edition*
The moose likes I/O and Streams and the fly likes Zip text send as email attachment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Zip text send as email attachment" Watch "Zip text send as email attachment" New topic

Zip text send as email attachment

David Meyers

Joined: Jul 03, 2001
Posts: 9
I'm trying to send an email with an attachement that is a zip file. However I don't actually have a file. I create a String that is extremely large, which I want to attach to a MimeMessage as a zip file. The problem is that MimeBodyPart takes an InputStream, but I have an OutputStream at this point.
Here is what I've tried so far (note imports are at the end):
* Create the MimeMessage that will be sent as an email
private static MimeMessage createMime(String attachmentString,
String attachmentName, String from, String subject,
List toEmailAddresses) {
MimeMessage msg = new MimeMessage();
MimeBodyPart part = null;
try {
FileOutputStream outStream = new FileOutputStream
ZipOutputStream zipOut = new ZipOutputStream(outStream);
ZipEntry zipEntry = new ZipEntry(attachmentName);
part = new MimeBodyPart();
} catch (FileNotFoundException fnfe) {
} catch (IOException ie) {
part.addHeader(Constants.contentType, "application/x-zip-compressed");
msg.setContent(part, "");
msg.addHeader(Header.SUBJECT, subject);
if (toEmailAddresses != null && !toEmailAddresses.isEmpty()) {
int emailSize = toEmailAddresses.size();
InternetAddress[] to = new InternetAddress[emailSize];
for (int i = 0; i < emailSize; i++) {
String email = (String)toEmailAddresses.get(i);
if (email != null) {
to[i] = new InternetAddress(email);
msg.addRecipients(MimeMessage.TO, to);
msg.addHeader(Header.FROM, from);
return msg;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import com.ibm.network.mail.smtp.protocol.CoreProtocolBean;
import com.ibm.network.mail.smtp.event.MessageEvent;
import com.ibm.network.mail.smtp.event.StatusEvent;
import com.ibm.network.mail.smtp.event.StatusListener;
import com.ibm.network.mail.base.Constants;
import com.ibm.network.mail.base.InternetAddress;
import com.ibm.network.mail.base.Header;
import com.ibm.network.mail.base.MimeMessage;
import com.ibm.network.mail.base.MimeBodyPart;
import com.ibm.network.mail.base.MimePart;
David Meyers

Joined: Jul 03, 2001
Posts: 9
Ok, so I got it to send a zip file using piped input and piped output, but I'm getting a deadlock situation. I can't use new threads becuase I am inside a j2ee application (that would break the contract). My code is below. Any suggestions?
//pipe the output to the input
PipedInputStream in = new PipedInputStream();
OutputStream out = new PipedOutputStream(in);
ZipOutputStream zipOut = new ZipOutputStream(out);
ZipEntry zipEntry = new ZipEntry(attachmentName + ".txt");
MimeBodyPart part = new MimeBodyPart(in);
juan Tsui

Joined: Oct 26, 2001
Posts: 13
I cannot give you any suggestions. I only can thanks you .
I know how to zip files from yours.
I agree. Here's the link: http://aspose.com/file-tools
subject: Zip text send as email attachment
Similar Threads
Having Problem in sending mail
Sending mail through Exchange Server
Attachment in email not working in server
Email generator