use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class ParseMimeMessage method setMultipartContent.
private static void setMultipartContent(ContentType contentType, MimeMessage mm, MimeMultipart mmp, Element elem, MimeBodyPart[] alternatives, ParseMessageContext ctxt) throws MessagingException, ServiceException, IOException {
// do we need to add a multipart/alternative for the alternatives?
if (alternatives == null || contentType.getSubType().equals("alternative")) {
// no need to add an extra multipart/alternative!
// create the MimeMultipart and attach it to the existing structure:
MimeMultipart mmpNew = new ZMimeMultipart(contentType);
if (mmp == null) {
// there were no multiparts at all, we need to create one
mm.setContent(mmpNew);
} else {
// there was already a multipart/mixed at the top of the mm
MimeBodyPart mbpWrapper = new ZMimeBodyPart();
mbpWrapper.setContent(mmpNew);
mmp.addBodyPart(mbpWrapper);
}
// add each part in turn (recursively) below
for (Element subpart : elem.listElements()) {
setContent(mm, mmpNew, subpart, null, ctxt);
}
// finally, add the alternatives if there are any...
if (alternatives != null) {
for (int i = 0; i < alternatives.length; i++) {
ctxt.incrementSize("alternative", alternatives[i].getSize());
mmpNew.addBodyPart(alternatives[i]);
}
}
} else {
// create a multipart/alternative to hold all the client's struct + the alternatives
MimeMultipart mmpNew = new ZMimeMultipart("alternative");
if (mmp == null) {
mm.setContent(mmpNew);
} else {
MimeBodyPart mbpWrapper = new ZMimeBodyPart();
mbpWrapper.setContent(mmpNew);
mmp.addBodyPart(mbpWrapper);
}
// add the entire client's multipart/whatever here inside our multipart/alternative
setContent(mm, mmpNew, elem, null, ctxt);
// add all the alternatives
for (int i = 0; i < alternatives.length; i++) {
ctxt.incrementSize("alternative", alternatives[i].getSize());
mmpNew.addBodyPart(alternatives[i]);
}
}
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class CalendarMailSender method createForwardNotifyMessage.
public static MimeMessage createForwardNotifyMessage(Account senderAcct, Account toAcct, String to, Address[] rcpts, Invite inv) throws MessagingException, ServiceException {
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
Locale lc = toAcct.getLocale();
mm.setSubject(L10nUtil.getMessage(MsgKey.calendarForwardNotificationSubject, lc, inv.getName()), MimeConstants.P_CHARSET_UTF8);
mm.setSentDate(new Date());
String postmaster = senderAcct.getAttr(Provisioning.A_zimbraNewMailNotificationFrom);
Map<String, String> vars = new HashMap<String, String>();
vars.put("RECIPIENT_DOMAIN", senderAcct.getDomainName());
postmaster = StringUtil.fillTemplate(postmaster, vars);
mm.setSender(new JavaMailInternetAddress(postmaster));
mm.setFrom(new JavaMailInternetAddress(senderAcct.getName()));
mm.setRecipient(RecipientType.TO, new JavaMailInternetAddress(to));
MimeMultipart mmp = new ZMimeMultipart("alternative");
mm.setContent(mmp);
String sender = senderAcct.getCn() + " <" + senderAcct.getName() + ">";
String time = FriendlyCalendaringDescription.getTimeDisplayString(inv.getStartTime(), inv.getEndTime(), inv.isRecurrence(), inv.isAllDayEvent(), lc, toAcct);
StringBuilder sb = new StringBuilder();
StringBuilder sbHtml = new StringBuilder();
for (Address rcpt : rcpts) {
sb.append(rcpt.toString()).append("\n\t");
InternetAddress address = new JavaMailInternetAddress(rcpt.toString());
sbHtml.append("<a href=\"mailto:").append(address.getAddress()).append("\">");
if (address.getPersonal() != null) {
sbHtml.append(address.getPersonal()).append("</a>").append("<br>");
} else {
sbHtml.append(address.getAddress()).append("</a>").append("<br>");
}
}
String recipients = sb.toString();
String recipientsHtml = sbHtml.toString();
if (inv.isRecurrence()) {
ZRecur zr = FriendlyCalendaringDescription.getRecur(inv);
time += " (" + FriendlyCalendaringDescription.getRecurrenceDisplayString(zr, inv.getStartTime().getCalendarCopy(), lc) + ")";
}
String text = L10nUtil.getMessage(MsgKey.calendarForwardNotificationBody, lc, sender, inv.getName(), time, recipients);
MimeBodyPart textPart = new ZMimeBodyPart();
textPart.setText(text, MimeConstants.P_CHARSET_UTF8);
mmp.addBodyPart(textPart);
sender = "<a href=\"mailto:" + senderAcct.getName() + "\">" + senderAcct.getCn() + "</a>";
String html = L10nUtil.getMessage(MsgKey.calendarForwardNotificationBodyHtml, lc, sender, inv.getName(), time, recipientsHtml);
MimeBodyPart htmlPart = new ZMimeBodyPart();
htmlPart.setContent(html, MimeConstants.CT_TEXT_HTML + "; " + MimeConstants.P_CHARSET + "=" + MimeConstants.P_CHARSET_UTF8);
mmp.addBodyPart(htmlPart);
mm.saveChanges();
return mm;
}
use of javax.mail.internet.MimeBodyPart 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 javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class ForwardCalendarItem method getInstanceFwdMsg.
protected static Pair<MimeMessage, MimeMessage> getInstanceFwdMsg(Account senderAcct, Invite inv, ZVCalendar cal, MimeMessage mmInv, MimeMessage mmFwdWrapper) throws ServiceException {
// Get plain and html texts entered by the forwarder.
DescDetectVisitor visitor = new DescDetectVisitor();
try {
visitor.accept(mmFwdWrapper);
} catch (MessagingException e) {
throw ServiceException.FAILURE("Messaging Exception while retrieving description text", e);
}
MimeBodyPart plainDescPart = visitor.getPlainDescPart();
MimeBodyPart htmlDescPart = visitor.getHtmlDescPart();
try {
return makeFwdMsg(senderAcct, inv, mmInv, cal, mmFwdWrapper, plainDescPart, htmlDescPart, true);
} catch (IOException e) {
throw ServiceException.FAILURE("error creating forward message", e);
} catch (MessagingException e) {
throw ServiceException.FAILURE("error creating forward message", e);
}
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class ForwardAppointmentInvite method getMessagePair.
public static Pair<MimeMessage, MimeMessage> getMessagePair(Mailbox mbox, Account senderAcct, Message msg, MimeMessage mmFwdWrapper) throws ServiceException {
Pair<MimeMessage, MimeMessage> msgPair;
mbox.lock.lock();
try {
MimeMessage mmInv = msg.getMimeMessage();
List<Invite> invs = new ArrayList<Invite>();
for (Iterator<CalendarItemInfo> iter = msg.getCalendarItemInfoIterator(); iter.hasNext(); ) {
CalendarItemInfo cii = iter.next();
Invite inv = cii.getInvite();
if (inv != null) {
invs.add(inv);
}
}
ZVCalendar cal = null;
Invite firstInv = null;
if (!invs.isEmpty()) {
// Recreate the VCALENDAR from Invites.
boolean first = true;
for (Invite inv : invs) {
if (first) {
first = false;
firstInv = inv;
cal = inv.newToICalendar(true);
} else {
ZComponent comp = inv.newToVComponent(true, true);
cal.addComponent(comp);
}
}
} else {
// If no invites found in metadata, parse from text/calendar MIME part.
try {
CalPartDetectVisitor visitor = new CalPartDetectVisitor();
visitor.accept(mmInv);
MimeBodyPart calPart = visitor.getCalendarPart();
if (calPart != null) {
String ctHdr = calPart.getContentType();
ContentType ct = new ContentType(ctHdr);
String charset = ct.getParameter(MimeConstants.P_CHARSET);
if (charset == null || charset.length() == 0)
charset = MimeConstants.P_CHARSET_UTF8;
InputStream is = calPart.getInputStream();
try {
cal = ZCalendarBuilder.build(is, charset);
} finally {
ByteUtil.closeStream(is);
}
List<Invite> invList = Invite.createFromCalendar(senderAcct, msg.getFragment(), cal, false);
if (invList != null && !invList.isEmpty())
firstInv = invList.get(0);
if (firstInv == null)
throw ServiceException.FAILURE("Error building Invite for calendar part in message " + msg.getId(), null);
}
} catch (MessagingException e) {
throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
} catch (IOException e) {
throw ServiceException.FAILURE("Error getting calendar part in message " + msg.getId(), null);
}
}
msgPair = getInstanceFwdMsg(senderAcct, firstInv, cal, mmInv, mmFwdWrapper);
} finally {
mbox.lock.release();
}
return msgPair;
}
Aggregations