aspose file tools*
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
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Zip text send as email attachment" Watch "Zip text send as email attachment" New topic
Author

Zip text send as email attachment

David Meyers
Greenhorn

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
("out.zip");
ZipOutputStream zipOut = new ZipOutputStream(outStream);
zipOut.setMethod(ZipOutputStream.DEFLATED);
ZipEntry zipEntry = new ZipEntry(attachmentName);
zipOut.putNextEntry(zipEntry);
zipOut.write(attachmentString.getBytes("US-ASCII"));
zipOut.flush();
part = new MimeBodyPart();
part.putByteStream(zipOut);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
}
part.addHeader(Constants.contentType, "application/x-zip-compressed");
part.setEncoding(MimePart.BASE64);
part.setDisposition(MimePart.ATTACHMENT);
part.setFileName("ZipFile.txt");
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
Greenhorn

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");
zipOut.putNextEntry(zipEntry);
zipOut.write(attachmentString.getBytes());
zipOut.closeEntry();
zipOut.close();
out.close();
MimeBodyPart part = new MimeBodyPart(in);
juan Tsui
Greenhorn

Joined: Oct 26, 2001
Posts: 13
hello:
I cannot give you any suggestions. I only can thanks you .
I know how to zip files from yours.
Thanks!
 
 
subject: Zip text send as email attachment