Search in sources :

Example 11 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 12 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 13 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 14 with ZMimeMessage

use of com.zimbra.common.zmime.ZMimeMessage in project zm-mailbox by Zimbra.

the class TestSmtpClient method testMimeMessage.

@Test
public void testMimeMessage() throws Exception {
    TestUtil.createAccount(USER_NAME);
    TestUtil.createAccount(USER2_NAME);
    // Assemble the message.
    MimeMessage mm = new ZMimeMessage(JMSession.getSession());
    InternetAddress addr = new JavaMailInternetAddress(TestUtil.getAddress(USER_NAME));
    mm.setFrom(addr);
    mm.setRecipient(RecipientType.TO, addr);
    String subject = NAME_PREFIX + " testMimeMessage";
    mm.setSubject(subject);
    mm.setText("testMimeMessage");
    mm.saveChanges();
    // Initialize SMTP client.
    SmtpConfig config = new SmtpConfig(mHost, mPort, "localhost");
    SmtpConnection conn = new SmtpConnection(config);
    conn.sendMessage(mm);
    // Make sure it arrived.
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
    // Send the same message to a different envelope recipient.
    conn.sendMessage(addr.getAddress(), new String[] { TestUtil.getAddress(USER2_NAME) }, mm);
    mbox = TestUtil.getZMailbox(USER2_NAME);
    TestUtil.waitForMessage(mbox, "in:inbox subject:\"" + subject + "\"");
}
Also used : SmtpConfig(com.zimbra.cs.mailclient.smtp.SmtpConfig) SmtpConnection(com.zimbra.cs.mailclient.smtp.SmtpConnection) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMailbox(com.zimbra.client.ZMailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Test(org.junit.Test)

Example 15 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)

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