Search in sources :

Example 16 with ZMimeMessage

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);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream)

Example 17 with ZMimeMessage

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);
    }
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Enumeration(java.util.Enumeration) Header(javax.mail.Header) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Date(java.util.Date)

Example 18 with ZMimeMessage

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;
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress)

Example 19 with ZMimeMessage

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);
    }
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Enumeration(java.util.Enumeration) Header(javax.mail.Header) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Date(java.util.Date)

Example 20 with ZMimeMessage

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);
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) InputStream(java.io.InputStream) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Aggregations

ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)53 MimeMessage (javax.mail.internet.MimeMessage)50 Test (org.junit.Test)34 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)30 InputStream (java.io.InputStream)17 Session (javax.mail.Session)14 Transport (javax.mail.Transport)14 Mailbox (com.zimbra.cs.mailbox.Mailbox)13 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)13 JMSession (com.zimbra.cs.util.JMSession)13 Message (com.zimbra.cs.mailbox.Message)12 Account (com.zimbra.cs.account.Account)10 MessagingException (javax.mail.MessagingException)10 OperationContext (com.zimbra.cs.mailbox.OperationContext)9 ItemId (com.zimbra.cs.service.util.ItemId)9 InternetAddress (javax.mail.internet.InternetAddress)9 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)8 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)8 Date (java.util.Date)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6