use of com.zimbra.common.mime.ContentDisposition in project zm-mailbox by Zimbra.
the class MailboxTestUtil method generateMessageWithAttachment.
public static ParsedMessage generateMessageWithAttachment(String subject) throws Exception {
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
mm.setHeader("From", "Vera Oliphant <oli@example.com>");
mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
mm.setHeader("Subject", subject);
mm.setText("Good as gold");
MimeMultipart multi = new ZMimeMultipart("mixed");
ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
cdisp.setParameter("filename", "fun.txt");
ZMimeBodyPart bp = new ZMimeBodyPart();
// it gets called before setting Content-Type and CTE headers.
try {
bp.setDataHandler(new DataHandler(new ByteArrayDataSource("Feeling attached.", "text/plain")));
} catch (IOException e) {
throw new MessagingException("could not generate mime part content", e);
}
bp.addHeader("Content-Disposition", cdisp.toString());
bp.addHeader("Content-Type", "text/plain");
bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
multi.addBodyPart(bp);
mm.setContent(multi);
mm.saveChanges();
return new ParsedMessage(mm, false);
}
use of com.zimbra.common.mime.ContentDisposition in project zm-mailbox by Zimbra.
the class ParseMimeMessage method attachDocument.
private static void attachDocument(MimeMultipart mmp, Document doc, String contentID, ParseMessageContext ctxt) throws MessagingException, ServiceException {
ctxt.incrementSize("attached document", (long) (doc.getSize() * 1.33));
ContentType ct = new ContentType(doc.getContentType());
if (MimeConstants.isZimbraDocument(ct.getContentType())) {
ct = new ContentType(MimeConstants.CT_TEXT_HTML);
}
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setDataHandler(new DataHandler(new MailboxBlobDataSource(doc.getBlob(), ct.getContentType())));
mbp.setHeader("Content-Type", ct.cleanup().setParameter("name", doc.getName()).setCharset(ctxt.defaultCharset).toString());
mbp.setHeader("Content-Disposition", new ContentDisposition(Part.ATTACHMENT).setParameter("filename", doc.getName()).toString());
mbp.setContentID(contentID);
mmp.addBodyPart(mbp);
}
use of com.zimbra.common.mime.ContentDisposition in project zm-mailbox by Zimbra.
the class ParseMimeMessage method attachContact.
private static void attachContact(MimeMultipart mmp, ItemId iid, String contentID, ParseMessageContext ctxt) throws MessagingException, ServiceException {
if (!iid.isLocal()) {
attachRemoteItem(mmp, iid, contentID, ctxt, FETCH_CONTACT_PARAMS, null);
return;
}
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(iid.getAccountId());
VCard vcf = VCard.formatContact(mbox.getContactById(ctxt.octxt, iid.getId()));
ctxt.incrementSize("contact", vcf.getFormatted().length());
String filename = vcf.fn + ".vcf";
String charset = CharsetUtil.checkCharset(vcf.getFormatted(), ctxt.defaultCharset);
MimeBodyPart mbp = new ZMimeBodyPart();
mbp.setText(vcf.getFormatted(), charset);
mbp.setHeader("Content-Type", new ContentType("text/x-vcard", ctxt.use2231).setCharset(ctxt.defaultCharset).setParameter("name", filename).setParameter("charset", charset).toString());
mbp.setHeader("Content-Disposition", new ContentDisposition(Part.ATTACHMENT, ctxt.use2231).setCharset(ctxt.defaultCharset).setParameter("filename", filename).toString());
mbp.setContentID(contentID);
mmp.addBodyPart(mbp);
}
use of com.zimbra.common.mime.ContentDisposition in project zm-mailbox by Zimbra.
the class CalendarMailSender method createCalendarMessage.
public static MimeMessage createCalendarMessage(Account account, Address fromAddr, Address senderAddr, List<Address> toAddrs, String subject, String desc, String descHtml, String uid, ZCalendar.ZVCalendar cal, List<Attach> attaches, boolean replyToSender) throws ServiceException {
if (desc == null)
desc = "";
try {
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
MimeMultipart mpAlternatives = new ZMimeMultipart("alternative");
if (attaches != null && !attaches.isEmpty()) {
MimeMultipart mpMixed = new ZMimeMultipart("mixed");
mm.setContent(mpMixed);
MimeBodyPart mbpWrapper = new ZMimeBodyPart();
mbpWrapper.setContent(mpAlternatives);
mpMixed.addBodyPart(mbpWrapper);
for (Attach attach : attaches) {
byte[] rawData = attach.getDecodedData();
if (rawData == null) {
continue;
}
ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT, true);
String ctypeAsString = attach.getContentType();
if (ctypeAsString == null) {
ctypeAsString = MimeConstants.CT_APPLICATION_OCTET_STREAM;
}
ContentType ctype = new ContentType(ctypeAsString);
if (attach.getFileName() != null) {
ctype.setParameter("name", attach.getFileName());
cdisp.setParameter("filename", attach.getFileName());
}
MimeBodyPart mbp2 = new ZMimeBodyPart();
ByteArrayDataSource bads = new ByteArrayDataSource(rawData, ctypeAsString);
mbp2.setDataHandler(new DataHandler(bads));
mbp2.setHeader("Content-Type", ctype.toString());
mbp2.setHeader("Content-Disposition", cdisp.toString());
mbp2.setHeader("Content-Transfer-Encoding", "base64");
mpMixed.addBodyPart(mbp2);
}
} else {
mm.setContent(mpAlternatives);
}
// Add the text as DESCRIPTION property in the iCalendar part.
// MS Entourage for Mac wants this. It ignores text/plain and
// text/html MIME parts.
cal.addDescription(desc, null);
// ///////
// TEXT part (add me first!)
MimeBodyPart textPart = new ZMimeBodyPart();
textPart.setText(desc, MimeConstants.P_CHARSET_UTF8);
mpAlternatives.addBodyPart(textPart);
// HTML part is needed to keep Outlook happy as it doesn't know
// how to deal with a message with only text/plain but no HTML.
MimeBodyPart htmlPart = new ZMimeBodyPart();
if (descHtml != null) {
ContentType ct = new ContentType(MimeConstants.CT_TEXT_HTML);
ct.setParameter(MimeConstants.P_CHARSET, MimeConstants.P_CHARSET_UTF8);
htmlPart.setText(descHtml, MimeConstants.P_CHARSET_UTF8);
htmlPart.setHeader("Content-Type", ct.toString());
} else {
htmlPart.setDataHandler(new DataHandler(new HtmlPartDataSource(desc)));
}
mpAlternatives.addBodyPart(htmlPart);
// ///////
// CALENDAR part
MimeBodyPart icalPart = makeICalIntoMimePart(cal);
mpAlternatives.addBodyPart(icalPart);
// MESSAGE HEADERS
if (subject != null) {
mm.setSubject(subject, MimeConstants.P_CHARSET_UTF8);
}
if (toAddrs != null) {
Address[] addrs = new Address[toAddrs.size()];
toAddrs.toArray(addrs);
mm.addRecipients(javax.mail.Message.RecipientType.TO, addrs);
}
if (fromAddr != null)
mm.setFrom(fromAddr);
if (senderAddr != null) {
mm.setSender(senderAddr);
if (replyToSender) {
mm.setReplyTo(new Address[] { senderAddr });
}
}
mm.setSentDate(new Date());
mm.saveChanges();
return mm;
} catch (MessagingException e) {
throw ServiceException.FAILURE("Messaging Exception while building MimeMessage from invite", e);
}
}
use of com.zimbra.common.mime.ContentDisposition in project zm-mailbox by Zimbra.
the class ToXML method addPart.
private static Element addPart(VisitPhase phase, Element parent, Element root, MPartInfo mpi, Set<MPartInfo> bodies, String prefix, int maxSize, boolean neuter, boolean excludeCalendarParts, String defaultCharset, boolean swallowContentExceptions, MsgContent wantContent) throws ServiceException {
if (phase == VisitPhase.POSTVISIT) {
return null;
}
String ctype = StringUtil.stripControlCharacters(mpi.getContentType());
if (excludeCalendarParts && MimeConstants.CT_TEXT_CALENDAR.equalsIgnoreCase(ctype)) {
// that happens to be a .ics file.
try {
ContentType ct = new ContentType(mpi.getMimePart().getContentType());
if (ct.getParameter("method") != null) {
return null;
}
} catch (MessagingException e) {
}
}
Element el = parent.addNonUniqueElement(MailConstants.E_MIMEPART);
MimePart mp = mpi.getMimePart();
String part = mpi.getPartName();
part = prefix + (prefix.isEmpty() || part.isEmpty() ? "" : ".") + part;
el.addAttribute(MailConstants.A_PART, part);
String fname = Mime.getFilename(mp);
if (MimeConstants.CT_XML_ZIMBRA_SHARE.equals(ctype)) {
// the <shr> share info goes underneath the top-level <m>
Element shr = root.addNonUniqueElement(MailConstants.E_SHARE_NOTIFICATION);
try {
addContent(shr, mpi, maxSize, defaultCharset);
} catch (IOException e) {
if (!swallowContentExceptions) {
throw ServiceException.FAILURE("error serializing share XML", e);
} else {
LOG.warn("error writing body part", e);
}
} catch (MessagingException e) {
if (!swallowContentExceptions) {
throw ServiceException.FAILURE("error serializing share XML", e);
}
}
} else if (MimeConstants.CT_XML_ZIMBRA_DL_SUBSCRIPTION.equals(ctype)) {
// the <dlSubs> dl subscription info goes underneath the top-level <m>
Element dlSubs = root.addNonUniqueElement(MailConstants.E_DL_SUBSCRIPTION_NOTIFICATION);
try {
addContent(dlSubs, mpi, maxSize, defaultCharset);
} catch (IOException e) {
if (!swallowContentExceptions) {
throw ServiceException.FAILURE("error serializing DL subscription", e);
} else {
LOG.warn("error writing body part", e);
}
} catch (MessagingException e) {
if (!swallowContentExceptions) {
throw ServiceException.FAILURE("error serializing DL subscription", e);
}
}
} else if (MimeConstants.CT_TEXT_ENRICHED.equals(ctype)) {
// we'll be replacing text/enriched with text/html
ctype = MimeConstants.CT_TEXT_HTML;
} else if (fname != null && (MimeConstants.CT_APPLICATION_OCTET_STREAM.equals(ctype) || MimeConstants.CT_APPLICATION_TNEF.equals(ctype))) {
String guess = MimeDetect.getMimeDetect().detect(fname);
if (guess != null) {
ctype = guess;
}
}
el.addAttribute(MailConstants.A_CONTENT_TYPE, ctype);
if (mpi.isMultipart()) {
// none of the below stuff is relevant for a multipart, so just return now...
return el;
}
// figure out attachment size
try {
el.addAttribute(MailConstants.A_SIZE, Mime.getSize(mp));
} catch (Exception e) {
// don't put out size if we get exception
ZimbraLog.mailbox.warn("Unable to determine MIME part size: %s", e.getMessage());
}
// figure out attachment disposition
try {
String disp = mp.getHeader("Content-Disposition", null);
if (disp != null) {
ContentDisposition cdisp = new ContentDisposition(MimeUtility.decodeText(disp));
el.addAttribute(MailConstants.A_CONTENT_DISPOSITION, StringUtil.stripControlCharacters(cdisp.getDisposition()));
}
} catch (MessagingException e) {
} catch (UnsupportedEncodingException e) {
}
// figure out attachment name
try {
if (fname == null && MimeConstants.CT_MESSAGE_RFC822.equals(ctype)) {
// "filename" for attached messages is the Subject
Object content = Mime.getMessageContent(mp);
if (content instanceof MimeMessage) {
fname = Mime.getSubject((MimeMessage) content);
}
}
if (!Strings.isNullOrEmpty(fname)) {
el.addAttribute(MailConstants.A_CONTENT_FILENAME, StringUtil.stripControlCharacters(fname));
}
} catch (MessagingException me) {
} catch (IOException ioe) {
}
// figure out content-id (used in displaying attached images)
String cid = mpi.getContentID();
if (cid != null) {
el.addAttribute(MailConstants.A_CONTENT_ID, StringUtil.stripControlCharacters(cid));
}
// figure out content-location (used in displaying attached images)
try {
String cl = mp.getHeader("Content-Location", null);
if (cl != null) {
el.addAttribute(MailConstants.A_CONTENT_LOCATION, StringUtil.stripControlCharacters(cl));
}
} catch (MessagingException e) {
}
// or if it was requested to include all parts
if (bodies == null || bodies.contains(mpi)) {
if (bodies != null) {
el.addAttribute(MailConstants.A_BODY, true);
}
try {
addContent(el, mpi, maxSize, neuter, defaultCharset, wantContent);
} catch (IOException e) {
if (!swallowContentExceptions) {
throw ServiceException.FAILURE("error serializing part content", e);
} else {
LOG.warn("error writing body part", e);
}
} catch (MessagingException me) {
if (!swallowContentExceptions) {
throw ServiceException.FAILURE("error serializing part content", me);
}
}
}
return el;
}
Aggregations