use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class TestUtil method getHeaderValue.
public static String getHeaderValue(ZMailbox mbox, ZMessage msg, String headerName) throws Exception {
String content = msg.getContent();
if (content == null) {
content = getContent(mbox, msg.getId());
}
assertNotNull("Content was not fetched from the server", content);
MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
return mimeMsg.getHeader(headerName, null);
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class CalendarMailSender method createCalendarMessage.
public static MimeMessage createCalendarMessage(Account account, Address fromAddr, Address senderAddr, List<Address> toAddrs, MimeMessage srcMm, Invite inv, ZVCalendar cal, boolean replyToSender) throws ServiceException {
try {
String uid = inv.getUid();
if (srcMm != null) {
// Get a copy so we can modify it.
MimeMessage mm = new ZMimeMessage(srcMm);
// Discard all old headers except Subject and Content-*.
Enumeration eh = srcMm.getAllHeaders();
while (eh.hasMoreElements()) {
Header hdr = (Header) eh.nextElement();
String hdrNameUpper = hdr.getName().toUpperCase();
if (!hdrNameUpper.startsWith("CONTENT-") && !hdrNameUpper.equals("SUBJECT")) {
mm.removeHeader(hdr.getName());
}
}
mm.setSentDate(new Date());
if (toAddrs != null) {
Address[] addrs = new Address[toAddrs.size()];
toAddrs.toArray(addrs);
mm.setRecipients(javax.mail.Message.RecipientType.TO, addrs);
} else {
mm.setRecipients(javax.mail.Message.RecipientType.TO, (Address[]) null);
}
mm.setRecipients(javax.mail.Message.RecipientType.CC, (Address[]) null);
mm.setRecipients(javax.mail.Message.RecipientType.BCC, (Address[]) null);
if (fromAddr != null)
mm.setFrom(fromAddr);
if (senderAddr != null) {
mm.setSender(senderAddr);
if (replyToSender)
mm.setReplyTo(new Address[] { senderAddr });
}
// Find and replace the existing calendar part with the new calendar object.
CalendarPartReplacingVisitor visitor = new CalendarPartReplacingVisitor(uid, cal);
visitor.accept(mm);
mm.saveChanges();
return mm;
} else {
String subject = inv.getName();
String desc = inv.getDescription();
String descHtml = inv.getDescriptionHtml();
return createCalendarMessage(account, fromAddr, senderAddr, toAddrs, subject, desc, descHtml, uid, cal, false);
}
} catch (MessagingException e) {
throw ServiceException.FAILURE("Messaging Exception while building calendar message from source MimeMessage", e);
}
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class CalendarMailSender method createForwardedInviteMessage.
public static MimeMessage createForwardedInviteMessage(MimeMessage mmOrig, String origSenderEmail, String forwarderEmail, String[] forwardTo) {
List<Address> rcpts = new ArrayList<Address>();
for (String to : forwardTo) {
try {
rcpts.add(new JavaMailInternetAddress(to));
} catch (AddressException e) {
ZimbraLog.calendar.warn("Ignoring invalid address \"" + to + "\" during invite forward");
}
}
if (rcpts.isEmpty())
return null;
MimeMessage mm = null;
try {
mm = new ZMimeMessage(mmOrig);
mm.removeHeader("To");
mm.removeHeader("Cc");
mm.removeHeader("Bcc");
mm.addRecipients(RecipientType.TO, rcpts.toArray(new Address[0]));
// Set Reply-To to the original sender.
mm.setReplyTo(new Address[] { new JavaMailInternetAddress(origSenderEmail) });
mm.removeHeader("Date");
mm.removeHeader("Message-ID");
mm.removeHeader("Return-Path");
mm.removeHeader("Received");
// Set special header to indicate the forwarding attendee.
mm.setHeader(CalendarMailSender.X_ZIMBRA_CALENDAR_INTENDED_FOR, forwarderEmail);
mm.saveChanges();
} catch (MessagingException e) {
ZimbraLog.calendar.warn("Unable to compose email for invite forwarding", e);
}
return mm;
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class ForwardCalendarItem method createMergedMessage.
// Take mmInv and mutate it. text/calendar part is replaced by cal and plain and html parts
// are replaced by plainDescPart and htmlDescPart.
private static MimeMessage createMergedMessage(Account account, Address fromAddr, Address senderAddr, MimeMessage mmInv, Invite inv, ZVCalendar cal, MimeBodyPart plainDescPart, MimeBodyPart htmlDescPart) throws ServiceException {
try {
String uid = inv.getUid();
if (mmInv != null) {
// Get a copy so we can modify it.
MimeMessage mm = new ZMimeMessage(mmInv);
// Discard all old headers except Subject and Content-*.
@SuppressWarnings("rawtypes") Enumeration eh = mmInv.getAllHeaders();
while (eh.hasMoreElements()) {
Header hdr = (Header) eh.nextElement();
String hdrNameUpper = hdr.getName().toUpperCase();
if (!hdrNameUpper.startsWith("CONTENT-") && !hdrNameUpper.equals("SUBJECT")) {
mm.removeHeader(hdr.getName());
}
}
mm.setSentDate(new Date());
mm.setRecipients(javax.mail.Message.RecipientType.TO, (Address[]) null);
mm.setRecipients(javax.mail.Message.RecipientType.CC, (Address[]) null);
mm.setRecipients(javax.mail.Message.RecipientType.BCC, (Address[]) null);
if (fromAddr != null)
mm.setFrom(fromAddr);
if (senderAddr != null)
mm.setSender(senderAddr);
// Find and replace the existing calendar part with the new calendar object.
ReplacingVisitor visitor = new ReplacingVisitor(uid, cal, plainDescPart, htmlDescPart);
visitor.accept(mm);
mm.saveChanges();
return mm;
} else {
String subject = inv.getName();
String desc, descHtml;
try {
Object plainContent = plainDescPart != null ? plainDescPart.getContent() : null;
desc = plainContent != null ? plainContent.toString() : null;
Object htmlContent = htmlDescPart != null ? htmlDescPart.getContent() : null;
descHtml = htmlContent != null ? htmlContent.toString() : null;
} catch (MessagingException e) {
throw ServiceException.FAILURE("Messaging Exception while retrieving description", e);
} catch (IOException e) {
throw ServiceException.FAILURE("Messaging Exception while retrieving description", e);
}
return CalendarMailSender.createCalendarMessage(account, fromAddr, senderAddr, null, subject, desc, descHtml, uid, cal, false);
}
} catch (MessagingException e) {
throw ServiceException.FAILURE("Messaging Exception while building calendar message from source MimeMessage", e);
}
}
use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.
the class AddressTest method testAddressContainingComma.
@Test
public void testAddressContainingComma() {
try {
Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
RuleManager.clearCachedRules(account);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
String filterScript = "if address :comparator \"i;ascii-casemap\" :matches \"to\" \"\\\"user,1\\\"@cosmonaut.zimbra.com\" {" + " tag \"TestComma\";" + "}";
account.setMailSieveScript(filterScript);
InputStream is = getClass().getResourceAsStream("TestFilter-testQuestionMarkCommaInAddress.msg");
MimeMessage mm = new ZMimeMessage(JMSession.getSession(), is);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(mm, false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Message msg = mbox.getMessageById(null, ids.get(0).getId());
Assert.assertEquals("TestComma", ArrayUtil.getFirstElement(msg.getTags()));
} catch (Exception e) {
fail("No exception should be thrown" + e);
}
}
Aggregations