Search in sources :

Example 1 with ZMimeBodyPart

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

the class MailboxTestUtil method generateMessageWithAttachment.

public static ParsedMessage generateMessageWithAttachment(String subject) throws Exception {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    mm.setHeader("From", "Vera Oliphant <oli@example.com>");
    mm.setHeader("To", "Jimmy Dean <jdean@example.com>");
    mm.setHeader("Subject", subject);
    mm.setText("Good as gold");
    MimeMultipart multi = new ZMimeMultipart("mixed");
    ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
    cdisp.setParameter("filename", "fun.txt");
    ZMimeBodyPart bp = new ZMimeBodyPart();
    // it gets called before setting Content-Type and CTE headers.
    try {
        bp.setDataHandler(new DataHandler(new ByteArrayDataSource("Feeling attached.", "text/plain")));
    } catch (IOException e) {
        throw new MessagingException("could not generate mime part content", e);
    }
    bp.addHeader("Content-Disposition", cdisp.toString());
    bp.addHeader("Content-Type", "text/plain");
    bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
    multi.addBodyPart(bp);
    mm.setContent(multi);
    mm.saveChanges();
    return new ParsedMessage(mm, false);
}
Also used : ContentDisposition(com.zimbra.common.mime.ContentDisposition) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 2 with ZMimeBodyPart

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

the class AutoProvision method sendNotifMessage.

protected void sendNotifMessage(Account acct, String password) throws ServiceException {
    String subject = fillTemplate(acct, domain.getAutoProvNotificationSubject());
    String body = fillTemplate(acct, domain.getAutoProvNotificationBody());
    String from = domain.getAutoProvNotificationFromAddress();
    if (from == null) {
        // TODO: should we use a seperate boolean control?
        return;
    }
    String toAddr = acct.getName();
    try {
        SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
        InternetAddress addr = null;
        try {
            addr = new JavaMailInternetAddress(from);
        } catch (AddressException e) {
            // log and try the next one
            ZimbraLog.autoprov.warn("invalid address in " + Provisioning.A_zimbraAutoProvNotificationFromAddress, e);
        }
        Address fromAddr = addr;
        Address replyToAddr = addr;
        // From
        out.setFrom(fromAddr);
        // Reply-To
        out.setReplyTo(new Address[] { replyToAddr });
        // To
        out.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(toAddr));
        // Date
        out.setSentDate(new Date());
        // Subject
        Locale locale = acct.getLocale();
        out.setSubject(subject);
        // NOTIFY=NEVER
        out.setNotifyOptions(SMTPMessage.NOTIFY_NEVER);
        // body
        MimeMultipart mmp = new ZMimeMultipart("alternative");
        // TEXT part (add me first!)
        String text = body;
        MimeBodyPart textPart = new ZMimeBodyPart();
        textPart.setText(text, MimeConstants.P_CHARSET_UTF8);
        mmp.addBodyPart(textPart);
        // HTML part
        StringBuilder html = new StringBuilder();
        html.append("<h4>\n");
        html.append("<p>" + body + "</p>\n");
        html.append("</h4>\n");
        html.append("\n");
        MimeBodyPart htmlPart = new ZMimeBodyPart();
        htmlPart.setDataHandler(new DataHandler(new HtmlPartDataSource(html.toString())));
        mmp.addBodyPart(htmlPart);
        out.setContent(mmp);
        // send it
        Transport.send(out);
        // log
        Address[] rcpts = out.getRecipients(javax.mail.Message.RecipientType.TO);
        StringBuilder rcptAddr = new StringBuilder();
        for (Address a : rcpts) rcptAddr.append(a.toString());
        ZimbraLog.autoprov.info("auto provision notification sent rcpt='" + rcptAddr + "' Message-ID=" + out.getMessageID());
    } catch (MessagingException e) {
        ZimbraLog.autoprov.warn("send auto provision notification failed rcpt='" + toAddr + "'", e);
    }
}
Also used : Locale(java.util.Locale) SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Address(javax.mail.Address) EmailAddress(com.zimbra.cs.account.names.NameUtil.EmailAddress) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) DataHandler(javax.activation.DataHandler) Date(java.util.Date) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) AddressException(javax.mail.internet.AddressException) 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 3 with ZMimeBodyPart

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

the class FilterUtil method reject.

public static void reject(OperationContext octxt, Mailbox mailbox, ParsedMessage parsedMessage, String reason, LmtpEnvelope envelope) throws MessagingException, ServiceException {
    MimeMessage mimeMessage = parsedMessage.getMimeMessage();
    if (isMailLoop(mailbox, mimeMessage, new String[] { HEADER_FORWARDED })) {
        // Detected a mail loop.  Do not send MDN, but just discard the message
        String error = String.format("Detected a mail loop for message %s. No MDN sent.", Mime.getMessageID(mimeMessage));
        ZimbraLog.filter.info(error);
        throw ServiceException.FAILURE(error, null);
    }
    String reportTo = null;
    if (envelope != null && envelope.hasSender()) {
        reportTo = envelope.getSender().getEmailAddress();
    }
    if (reportTo == null || reportTo.isEmpty()) {
        String[] returnPath = mimeMessage.getHeader(HEADER_RETURN_PATH);
        if (returnPath == null || returnPath.length == 0) {
            // >> NOT be generated if the MAIL FROM (or Return-Path) is empty.
            throw new MessagingException("Neither 'envelope from' nor 'Return-Path' specified. Can't locate the address to reject to (No MDN sent)");
        } else {
            // At least one 'return-path' should exist.
            reportTo = returnPath[0];
        }
    }
    Account owner = mailbox.getAccount();
    Locale locale = owner.getLocale();
    String charset = owner.getPrefMailDefaultCharset();
    if (charset == null) {
        charset = MimeConstants.P_CHARSET_UTF8;
    }
    SMTPMessage report = new SMTPMessage(JMSession.getSmtpSession());
    // add the forwarded header account names to detect the mail loop between accounts
    for (String headerFwdAccountName : Mime.getHeaders(mimeMessage, HEADER_FORWARDED)) {
        report.addHeader(HEADER_FORWARDED, headerFwdAccountName);
    }
    report.addHeader(HEADER_FORWARDED, owner.getName());
    // MDN header
    report.setEnvelopeFrom("<>");
    report.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(reportTo));
    String subject = L10nUtil.getMessage(MsgKey.seiveRejectMDNSubject, locale);
    report.setSubject(subject);
    report.setSentDate(new Date());
    InternetAddress address = new JavaMailInternetAddress(owner.getName());
    report.setFrom(address);
    MimeMultipart multi = new ZMimeMultipart("report");
    // part 1: human-readable notification
    String text = L10nUtil.getMessage(MsgKey.seiveRejectMDNErrorMsg, locale) + "\n" + reason;
    MimeBodyPart mpText = new ZMimeBodyPart();
    mpText.setText(text, CharsetUtil.checkCharset(text, charset));
    multi.addBodyPart(mpText);
    // part 2: disposition notification
    StringBuilder mdn = new StringBuilder();
    mdn.append("Final-Recipient: rfc822;").append(owner.getName()).append("\r\n");
    mdn.append("Disposition: automatic-action/MDN-sent-automatically");
    mdn.append("; deleted\r\n");
    MimeBodyPart mpMDN = new ZMimeBodyPart();
    mpMDN.setText(mdn.toString(), MimeConstants.P_CHARSET_UTF8);
    mpMDN.setHeader("Content-Type", "message/disposition-notification; charset=utf-8");
    multi.addBodyPart(mpMDN);
    // Assemble the MDN
    report.setContent(multi);
    report.setHeader("Content-Type", multi.getContentType() + "; report-type=disposition-notification");
    report.saveChanges();
    MailSender mailSender = mailbox.getMailSender().setSaveToSent(false);
    mailSender.setRecipients(reportTo);
    mailSender.setEnvelopeFrom("<>");
    mailSender.sendMimeMessage(octxt, mailbox, report);
}
Also used : Locale(java.util.Locale) Account(com.zimbra.cs.account.Account) SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MessagingException(javax.mail.MessagingException) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MailSender(com.zimbra.cs.mailbox.MailSender) Date(java.util.Date) 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 4 with ZMimeBodyPart

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

the class CalendarItem method createBlob.

/**
     * The Blob for the appointment/task is currently single Mime multipart/digest which has
     * each invite's MimeMessage stored as a part.
     *
     * @param invPm
     * @param firstInvite
     * @throws ServiceException
     */
private MailboxBlob createBlob(ParsedMessage invPm, Invite firstInvite) throws ServiceException {
    // Create blob only if there's an attachment or DESCRIPTION is too big to be stored in metadata.
    if (!firstInvite.hasAttachment() && (invPm == null || firstInvite.descInMeta())) {
        // If we're not going to create a blob after all, we must at least save the metadata to db.
        // It's weird that the db update is implicitly required of this method, but that's the way
        // it is, unfortunately.  If we're creating a blob, the implicit db update is done by
        // storeUpdatedBlob() call.  (see below)
        saveMetadata();
        return null;
    }
    try {
        // create the toplevel multipart/digest...
        MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
        MimeMultipart mmp = new ZMimeMultipart("digest");
        mm.setContent(mmp);
        // add the invite
        MimeBodyPart mbp = new ZMimeBodyPart();
        mbp.setDataHandler(new DataHandler(new PMDataSource(invPm)));
        mbp.addHeader("invId", Integer.toString(firstInvite.getMailItemId()));
        mmp.addBodyPart(mbp);
        mm.saveChanges();
        return storeUpdatedBlob(mm);
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("MessagingException " + e, e);
    } catch (IOException e) {
        throw ServiceException.FAILURE("IOException " + e, e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 5 with ZMimeBodyPart

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

the class ParsedContact method generateMimeMessage.

private static MimeMessage generateMimeMessage(List<Attachment> attachments) throws MessagingException {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    MimeMultipart multi = new ZMimeMultipart("mixed");
    int part = 1;
    for (Attachment attach : attachments) {
        ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
        cdisp.setParameter("filename", attach.getFilename()).setParameter("field", attach.getName());
        MimeBodyPart bp = new ZMimeBodyPart();
        // it gets called before setting Content-Type and CTE headers.
        try {
            bp.setDataHandler(new DataHandler(new ByteArrayDataSource(attach.getContent(), attach.getContentType())));
        } catch (IOException e) {
            throw new MessagingException("could not generate mime part content", e);
        }
        bp.addHeader("Content-Disposition", cdisp.toString());
        bp.addHeader("Content-Type", attach.getContentType());
        bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
        multi.addBodyPart(bp);
        attach.setPartName(Integer.toString(part++));
    }
    mm.setContent(multi);
    mm.saveChanges();
    return mm;
}
Also used : ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ContentDisposition(com.zimbra.common.mime.ContentDisposition) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Aggregations

ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)24 MimeBodyPart (javax.mail.internet.MimeBodyPart)22 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)20 MimeMultipart (javax.mail.internet.MimeMultipart)20 MimeMessage (javax.mail.internet.MimeMessage)16 MessagingException (javax.mail.MessagingException)14 DataHandler (javax.activation.DataHandler)11 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)7 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)7 Date (java.util.Date)7 ContentDisposition (com.zimbra.common.mime.ContentDisposition)6 IOException (java.io.IOException)6 InternetAddress (javax.mail.internet.InternetAddress)6 SMTPMessage (com.sun.mail.smtp.SMTPMessage)5 ContentType (com.zimbra.common.mime.ContentType)5 Account (com.zimbra.cs.account.Account)4 Locale (java.util.Locale)4 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)4 ServiceException (com.zimbra.common.service.ServiceException)3 Mailbox (com.zimbra.cs.mailbox.Mailbox)3