Search in sources :

Example 16 with ZMimeMultipart

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

the class Notification method interceptIfNecessary.

/**
     * If <tt>zimbraInterceptAddress</tt> is specified, sends a message to that
     * address with the given message attached.
     *
     * @param operation name of the operation being performed (send, add message, save draft, etc.)
     * @param folder the folder that the message was filed into, or <tt>null</tt>
     */
void interceptIfNecessary(Mailbox mbox, MimeMessage msg, String operation, Folder folder) throws ServiceException {
    // Don't do anything if intercept is turned off.
    Account account = mbox.getAccount();
    String[] interceptAddresses = account.getMultiAttr(Provisioning.A_zimbraInterceptAddress);
    if (interceptAddresses.length == 0) {
        return;
    }
    for (String interceptAddress : interceptAddresses) {
        try {
            ZimbraLog.mailbox.info("Sending intercept of message %s to %s.", msg.getMessageID(), interceptAddress);
            // Fill templates
            String folderName = "none";
            String folderId = "none";
            if (folder != null) {
                folderName = folder.getName();
                folderId = Integer.toString(folder.getId());
            }
            Map<String, String> vars = new HashMap<String, String>();
            vars.put("ACCOUNT_DOMAIN", getDomain(account.getName()));
            vars.put("ACCOUNT_ADDRESS", account.getName());
            vars.put("MESSAGE_SUBJECT", Mime.getSubject(msg));
            vars.put("OPERATION", operation);
            vars.put("FOLDER_NAME", folderName);
            vars.put("FOLDER_ID", folderId);
            vars.put("NEWLINE", "\r\n");
            String from = StringUtil.fillTemplate(account.getAttr(Provisioning.A_zimbraInterceptFrom), vars);
            String subject = StringUtil.fillTemplate(account.getAttr(Provisioning.A_zimbraInterceptSubject), vars);
            String bodyText = StringUtil.fillTemplate(account.getAttr(Provisioning.A_zimbraInterceptBody), vars);
            // Assemble outgoing message
            MimeMessage attached = msg;
            boolean headersOnly = account.getBooleanAttr(Provisioning.A_zimbraInterceptSendHeadersOnly, false);
            if (headersOnly) {
                attached = new MimeMessageWithId(msg.getMessageID());
                Enumeration e = msg.getAllHeaderLines();
                while (e.hasMoreElements()) {
                    attached.addHeaderLine((String) e.nextElement());
                }
                attached.setContent("", msg.getContentType());
                attached.saveChanges();
            }
            SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
            out.setHeader("Auto-Submitted", "auto-replied (zimbra; intercept)");
            InternetAddress address = new JavaMailInternetAddress(from);
            out.setFrom(address);
            address = new JavaMailInternetAddress(interceptAddress);
            out.setRecipient(javax.mail.Message.RecipientType.TO, address);
            String charset = getCharset(account, subject);
            out.setSubject(subject, charset);
            charset = getCharset(account, bodyText);
            MimeMultipart multi = new ZMimeMultipart();
            // Add message body
            MimeBodyPart part = new ZMimeBodyPart();
            part.setText(bodyText, charset);
            multi.addBodyPart(part);
            // Add original message
            MimeBodyPart part2 = new ZMimeBodyPart();
            part2.setContent(attached, MimeConstants.CT_MESSAGE_RFC822);
            multi.addBodyPart(part2);
            out.setContent(multi);
            String envFrom = "<>";
            out.setEnvelopeFrom(envFrom);
            out.saveChanges();
            Transport.send(out);
            // clean up after ourselves...
            multi.removeBodyPart(part2);
        } catch (MessagingException e) {
            ZimbraLog.lmtp.warn("Unable to send intercept message to %s.", interceptAddress, e);
        }
    }
}
Also used : Account(com.zimbra.cs.account.Account) SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 17 with ZMimeMultipart

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

the class FeedManager method generateMessage.

private static ParsedMessage generateMessage(String title, String content, String ctype, InternetAddress addr, Date date, List<Enclosure> attach) throws ServiceException {
    // cull out invalid enclosures
    if (attach != null) {
        for (Iterator<Enclosure> it = attach.iterator(); it.hasNext(); ) {
            if (it.next().getLocation() == null) {
                it.remove();
            }
        }
    }
    boolean hasAttachments = attach != null && !attach.isEmpty();
    // clean up whitespace in the title
    if (title != null) {
        title = title.replaceAll("\\s+", " ");
    }
    // create the MIME message and wrap it
    try {
        MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
        MimePart body = hasAttachments ? new ZMimeBodyPart() : (MimePart) mm;
        body.setText(content, "utf-8");
        body.setHeader("Content-Type", ctype);
        if (hasAttachments) {
            // encode each enclosure as an attachment with Content-Location set
            MimeMultipart mmp = new ZMimeMultipart("mixed");
            mmp.addBodyPart((BodyPart) body);
            for (Enclosure enc : attach) {
                MimeBodyPart part = new ZMimeBodyPart();
                part.setText("");
                part.addHeader("Content-Location", enc.getLocation());
                part.addHeader("Content-Type", enc.getContentType());
                if (enc.getDescription() != null) {
                    part.addHeader("Content-Description", enc.getDescription());
                }
                part.addHeader("Content-Disposition", "attachment");
                mmp.addBodyPart(part);
            }
            mm.setContent(mmp);
        }
        mm.setSentDate(date);
        mm.addFrom(new InternetAddress[] { addr });
        mm.setSubject(title, "utf-8");
        // more stuff here!
        mm.saveChanges();
        return new ParsedMessage(mm, date.getTime(), false);
    } catch (MessagingException e) {
        throw ServiceException.PARSE_ERROR("error wrapping feed item in MimeMessage", e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimePart(javax.mail.internet.MimePart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 18 with ZMimeMultipart

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

the class Mime method getMultipartContent.

/** Returns the MimeMultipart object encapsulating the body of a MIME
     *  part with content-type "multipart/*".  Use this method instead of
     *  {@link Part#getContent()} to work around JavaMail's fascism about
     *  proper MIME format and failure to support RFC 2184. */
public static MimeMultipart getMultipartContent(MimePart multipartPart, String contentType) throws IOException, MessagingException {
    MimeMultipart mmp = null;
    Object content = multipartPart.getContent();
    if (content instanceof MimeMultipart) {
        mmp = (MimeMultipart) content;
    } else if (content instanceof InputStream) {
        try {
            // handle unparsed content due to miscapitalization of content-type value
            mmp = new ZMimeMultipart(new InputStreamDataSource((InputStream) content, contentType));
        } catch (Exception e) {
        } finally {
            ByteUtil.closeStream((InputStream) content);
        }
    }
    return validateMultipart(mmp, multipartPart);
}
Also used : ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) SharedFileInputStream(javax.mail.util.SharedFileInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) EncoderException(org.apache.commons.codec.EncoderException) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParseException(javax.mail.internet.ParseException) IOException(java.io.IOException)

Example 19 with ZMimeMultipart

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

the class MessageBuilder method create.

public String create() throws MessagingException, ServiceException, IOException {
    if (toRecipient == null) {
        toRecipient = "user1";
    }
    if (from == null) {
        from = "jspiccoli";
    }
    if (date == null) {
        date = new Date();
    }
    if (contentType == null) {
        contentType = MimeConstants.CT_TEXT_PLAIN;
    }
    if (body == null) {
        body = MessageBuilder.DEFAULT_MESSAGE_BODY;
    }
    from = TestUtil.addDomainIfNecessary(from);
    toRecipient = TestUtil.addDomainIfNecessary(toRecipient);
    sender = TestUtil.addDomainIfNecessary(sender);
    MimeMessage msg = addMessageIdHeader ? new ZMimeMessage(JMSession.getSession()) : new MimeMessageWithNoId();
    msg.setRecipient(RecipientType.TO, new JavaMailInternetAddress(toRecipient));
    if (ccRecipient != null) {
        ccRecipient = TestUtil.addDomainIfNecessary(ccRecipient);
        msg.setRecipient(RecipientType.CC, new JavaMailInternetAddress(ccRecipient));
    }
    msg.setFrom(new JavaMailInternetAddress(from));
    if (sender != null) {
        msg.setSender(new JavaMailInternetAddress(sender));
    }
    msg.setSentDate(date);
    msg.setSubject(subject);
    if (attachment == null) {
        // Need to specify the data handler explicitly because JavaMail
        // doesn't know what to do with text/enriched.
        msg.setDataHandler(new DataHandler(new ByteArrayDataSource(body.getBytes(), contentType)));
    } else {
        MimeMultipart multi = new ZMimeMultipart("mixed");
        MimeBodyPart body = new ZMimeBodyPart();
        body.setDataHandler(new DataHandler(new ByteArrayDataSource(this.body.getBytes(), contentType)));
        multi.addBodyPart(body);
        MimeBodyPart attachment = new ZMimeBodyPart();
        attachment.setContent(this.attachment, attachmentContentType);
        attachment.setHeader("Content-Disposition", "attachment; filename=" + attachmentFilename);
        multi.addBodyPart(attachment);
        msg.setContent(multi);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.writeTo(out);
    return new String(out.toByteArray());
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 20 with ZMimeMultipart

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

the class CalItemEmailReminderTask method sendReminder.

@Override
protected void sendReminder(CalendarItem calItem, Invite invite) throws Exception {
    Account account = calItem.getAccount();
    Locale locale = account.getLocale();
    TimeZone tz = Util.getAccountTimeZone(account);
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
    String to = account.getAttr(Provisioning.A_zimbraPrefCalendarReminderEmail);
    if (to == null) {
        ZimbraLog.scheduler.info("Unable to send calendar reminder email since %s is not set", Provisioning.A_zimbraPrefCalendarReminderEmail);
        return;
    }
    mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(to));
    mm.setSubject(L10nUtil.getMessage(calItem.getType() == MailItem.Type.APPOINTMENT ? L10nUtil.MsgKey.apptReminderEmailSubject : L10nUtil.MsgKey.taskReminderEmailSubject, locale, calItem.getSubject()), MimeConstants.P_CHARSET_UTF8);
    if (invite.getDescriptionHtml() == null) {
        mm.setText(getBody(calItem, invite, false, locale, tz), MimeConstants.P_CHARSET_UTF8);
    } else {
        MimeMultipart mmp = new ZMimeMultipart("alternative");
        mm.setContent(mmp);
        MimeBodyPart textPart = new ZMimeBodyPart();
        textPart.setText(getBody(calItem, invite, false, locale, tz), MimeConstants.P_CHARSET_UTF8);
        mmp.addBodyPart(textPart);
        MimeBodyPart htmlPart = new ZMimeBodyPart();
        htmlPart.setContent(getBody(calItem, invite, true, locale, tz), MimeConstants.CT_TEXT_HTML + "; " + MimeConstants.P_CHARSET + "=" + MimeConstants.P_CHARSET_UTF8);
        mmp.addBodyPart(htmlPart);
    }
    mm.setSentDate(new Date());
    mm.saveChanges();
    MailSender mailSender = calItem.getMailbox().getMailSender();
    mailSender.sendMimeMessage(null, calItem.getMailbox(), mm);
}
Also used : Locale(java.util.Locale) Account(com.zimbra.cs.account.Account) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MailSender(com.zimbra.cs.mailbox.MailSender) Date(java.util.Date) TimeZone(java.util.TimeZone) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)22 MimeMultipart (javax.mail.internet.MimeMultipart)19 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)18 MimeBodyPart (javax.mail.internet.MimeBodyPart)17 MimeMessage (javax.mail.internet.MimeMessage)17 MessagingException (javax.mail.MessagingException)14 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)9 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)7 IOException (java.io.IOException)7 Date (java.util.Date)7 DataHandler (javax.activation.DataHandler)7 SMTPMessage (com.sun.mail.smtp.SMTPMessage)5 Account (com.zimbra.cs.account.Account)5 InternetAddress (javax.mail.internet.InternetAddress)5 ContentDisposition (com.zimbra.common.mime.ContentDisposition)4 Locale (java.util.Locale)4 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)4 Element (com.zimbra.common.soap.Element)3 Message (com.zimbra.cs.mailbox.Message)3 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)3