Search in sources :

Example 1 with ZMimeMultipart

use of com.zimbra.common.zmime.ZMimeMultipart 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 ZMimeMultipart

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

the class TestContentTransferEncoding method testBug98015.

/*
     * Tests that the message from bug 98015 retains the correct Content Transfer Encoding via the mechanism of
     * inferring it from the existing message referenced by the "origid" parameter.
     * This scenario also happens to test the case when a CTE header on a sub-part is inherited from the top-level.
     */
@Ignore("disabled until bug 98015 is fixed")
@Test
public void testBug98015() throws Exception {
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(getBug98015MimeString().getBytes()));
    ParsedMessage pm = new ParsedMessage(mimeMsg, true);
    DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
    Message msg = mbox.addMessage(null, pm, dopt, null);
    MsgToSend msgToSend = new MsgToSend();
    msgToSend.setOrigId(String.valueOf(msg.getId()));
    msgToSend.setReplyType("w");
    msgToSend.setSubject("Fwd: QP bug");
    msgToSend.setMimePart(MimePartInfo.createForContentTypeAndContent("text/plain", "\n\n----- Forwarded Message -----\nSubject: QP Bug\n\n\\รถ/\nid=577281"));
    AttachmentsInfo attach = new AttachmentsInfo();
    attach.addAttachment(new MimePartAttachSpec(String.valueOf(msg.getId()), "2"));
    msgToSend.setAttachments(attach);
    SendMsgRequest req = new SendMsgRequest();
    req.setMsg(msgToSend);
    MimeMessage parsed = sendForwardedMessage(req, msg);
    ZMimeMultipart mmp = (ZMimeMultipart) parsed.getContent();
    assertEquals("8bit", mmp.getBodyPart(0).getHeader("Content-Transfer-Encoding")[0]);
    assertEquals("base64", mmp.getBodyPart(1).getHeader("Content-Transfer-Encoding")[0]);
}
Also used : AttachmentsInfo(com.zimbra.soap.mail.type.AttachmentsInfo) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParseMimeMessage(com.zimbra.cs.service.mail.ParseMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParseMimeMessage(com.zimbra.cs.service.mail.ParseMimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MsgToSend(com.zimbra.soap.mail.type.MsgToSend) SendMsgRequest(com.zimbra.soap.mail.message.SendMsgRequest) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) MimePartAttachSpec(com.zimbra.soap.mail.type.MimePartAttachSpec) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ZMimeMultipart

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

the class TestContentTransferEncoding method testMultipartMimeMessage.

/*
     * This tests the CTE header of a forwarded message being inferred from the existing message when the message is a multipart message.
     */
@Ignore("disabled until bug 98015 is fixed")
@Test
public void testMultipartMimeMessage() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(getMultipartMimeString().getBytes()));
    ParsedMessage pm = new ParsedMessage(mimeMsg, true);
    DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
    Message msg = mbox.addMessage(null, pm, dopt, null);
    MsgToSend msgToSend = new MsgToSend();
    msgToSend.setOrigId(String.valueOf(msg.getId()));
    msgToSend.setReplyType("w");
    msgToSend.setSubject("Fwd: Multipart Test");
    MimePartInfo mpi = new MimePartInfo();
    mpi.setContentType("multipart/alternative");
    List<MimePartInfo> mimeParts = new LinkedList<MimePartInfo>();
    mimeParts.add(MimePartInfo.createForContentTypeAndContent("text/plain", "multipart test"));
    mimeParts.add(MimePartInfo.createForContentTypeAndContent("text/html", "multipart test"));
    mpi.setMimeParts(mimeParts);
    msgToSend.setMimePart(mpi);
    SendMsgRequest req = new SendMsgRequest();
    req.setMsg(msgToSend);
    MimeMessage parsed = sendForwardedMessage(req, msg);
    ZMimeMultipart mmp = (ZMimeMultipart) parsed.getContent();
    assertEquals("test1", mmp.getBodyPart(0).getHeader("Content-Transfer-Encoding")[0]);
    assertEquals("test2", mmp.getBodyPart(1).getHeader("Content-Transfer-Encoding")[0]);
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParseMimeMessage(com.zimbra.cs.service.mail.ParseMimeMessage) SendMsgRequest(com.zimbra.soap.mail.message.SendMsgRequest) LinkedList(java.util.LinkedList) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParseMimeMessage(com.zimbra.cs.service.mail.ParseMimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MsgToSend(com.zimbra.soap.mail.type.MsgToSend) MimePartInfo(com.zimbra.soap.mail.type.MimePartInfo) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with ZMimeMultipart

use of com.zimbra.common.zmime.ZMimeMultipart 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 5 with ZMimeMultipart

use of com.zimbra.common.zmime.ZMimeMultipart 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)

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