Search in sources :

Example 41 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.

the class ZMimeMultipart method writeTo.

// JavaMail makes it impossible to set MimeMultipart.allowEmpty if you don't use their parser, and that
// means an empty multipart always throws an exception on MimeMultipart.writeTo regardless of what
// "mail.mime.multipart.allowempty" is set to.  So we have to copy the whole method from the superclass
// just so we can strip that conditional out.
@Override
public synchronized void writeTo(OutputStream os) throws IOException, MessagingException {
    if (ZPARSER) {
        parse();
        String boundary = "--" + getBoundary();
        LineOutputStream los = new LineOutputStream(os);
        // if there's a preamble, write it out
        String preamble = getPreamble();
        if (preamble != null) {
            byte[] pb = ASCIIUtility.getBytes(preamble);
            los.write(pb);
            // make sure it ends with a newline
            if (pb.length > 0 && !(pb[pb.length - 1] == '\r' || pb[pb.length - 1] == '\n')) {
                los.writeln();
            }
        // XXX - could force a blank line before start boundary
        }
        if (parts.size() == 0) {
            // write out a single empty body part
            // put out boundary
            los.writeln(boundary);
            // put out empty line
            los.writeln();
        } else {
            for (int i = 0; i < parts.size(); i++) {
                // put out boundary
                los.writeln(boundary);
                ((MimeBodyPart) parts.elementAt(i)).writeTo(os);
                // put out empty line
                los.writeln();
            }
        }
        // put out last boundary
        los.writeln(boundary + "--");
    } else {
        super.writeTo(os);
    }
}
Also used : LineOutputStream(com.sun.mail.util.LineOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 42 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.

the class ParseMimeMessageTest method attachZimbraDocument.

@Test
public void attachZimbraDocument() throws Exception {
    Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    OperationContext octxt = new OperationContext(acct);
    Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_APPLICATION_ZIMBRA_DOC, "author", "description", new ByteArrayInputStream("test123".getBytes()));
    Element el = new Element.JSONElement(MailConstants.E_MSG);
    el.addAttribute(MailConstants.E_SUBJECT, "attach message");
    el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
    el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
    el.addElement(MailConstants.E_ATTACH).addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
    ZimbraSoapContext zsc = getMockSoapContext();
    MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    MimeMultipart mmp = (MimeMultipart) mm.getContent();
    MimeBodyPart part = (MimeBodyPart) mmp.getBodyPart(1);
    Assert.assertEquals(MimeConstants.CT_TEXT_HTML, new ContentType(part.getContentType()).getContentType());
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ContentType(com.zimbra.common.mime.ContentType) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) Mailbox(com.zimbra.cs.mailbox.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 43 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.

the class ParseMimeMessageTest method staleReference.

@Test
public void staleReference() throws Exception {
    Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
    // first, create the original draft
    OperationContext octxt = new OperationContext(acct);
    Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test1", 8192));
    Document doc2 = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc2", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test2", 8192));
    Element el = new Element.JSONElement(MailConstants.E_MSG), attach;
    el.addAttribute(MailConstants.E_SUBJECT, "has attachment");
    el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
    el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
    attach = el.addElement(MailConstants.E_ATTACH);
    attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
    attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc2.getId());
    ZimbraSoapContext zsc = getMockSoapContext();
    MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    Message draft = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
    // then, create a new draft that references one of the original draft's attachments
    attach.detach();
    (attach = el.addElement(MailConstants.E_ATTACH)).addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_MESSAGE_ID, draft.getId()).addAttribute(MailConstants.A_PART, "3");
    mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
    // delete the draft itself and then try to save the new draft
    mbox.delete(octxt, draft.getId(), MailItem.Type.MESSAGE);
    Message draft2 = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
    // check that the attachment's content is present and correct
    MimeMultipart multi = (MimeMultipart) (draft2.getMimeMessage().getContent());
    Assert.assertEquals("2 parts in draft", 2, multi.getCount());
    Assert.assertEquals("attached part content", "test2", firstLine((MimeBodyPart) multi.getBodyPart(1)));
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Element(com.zimbra.common.soap.Element) Document(com.zimbra.cs.mailbox.Document) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 44 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart 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 45 with MimeBodyPart

use of javax.mail.internet.MimeBodyPart in project perun by CESNET.

the class MessagePreparator method prepare.

/*
	 * (non-Javadoc)
	 *
	 * @see org.springframework.mail.javamail.MimeMessagePreparator#prepare(javax.mail.internet.MimeMessage)
	 */
public void prepare(MimeMessage mimeMessage) throws Exception {
    MimeMultipart mpRoot = new MimeMultipart("mixed");
    Multipart mp = new MimeMultipart("alternative");
    // Create a body part to house the multipart/alternative Part
    MimeBodyPart contentPartRoot = new MimeBodyPart();
    contentPartRoot.setContent(mp);
    // Add the root body part to the root multipart
    mpRoot.addBodyPart(contentPartRoot);
    // adding recipients, cc and bcc
    if (getTo() != null) {
        for (String to : getTo()) {
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        }
    }
    if (getCc() != null) {
        for (String cc : getCc()) {
            mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
        }
    }
    if (getBcc() != null) {
        for (String bcc : getBcc()) {
            mimeMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
        }
    }
    // adding from and subject
    mimeMessage.setFrom(new InternetAddress(getFrom(), getFromText()));
    mimeMessage.setSubject(getSubject());
    if (getEmailType().equals(EmailType.HTML)) {
        mp.addBodyPart(createHtmlMessage());
    } else if (getEmailType().equals(EmailType.PLAIN)) {
        mp.addBodyPart(createTextMessage());
    }
    // Create an "ATTACHMENT" - we must put it to mpRoot(mixed content)
    if (getFileNames() != null) {
        for (String filename : getFileNames()) {
            mpRoot.addBodyPart(createAttachment(filename));
        }
    }
    mimeMessage.setContent(mpRoot);
    logger.debug("Message is prepared to send");
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

MimeBodyPart (javax.mail.internet.MimeBodyPart)172 MimeMultipart (javax.mail.internet.MimeMultipart)111 MimeMessage (javax.mail.internet.MimeMessage)65 MessagingException (javax.mail.MessagingException)64 IOException (java.io.IOException)49 DataHandler (javax.activation.DataHandler)39 ByteString (com.linkedin.data.ByteString)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 BodyPart (javax.mail.BodyPart)35 InternetAddress (javax.mail.internet.InternetAddress)31 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)30 Test (org.testng.annotations.Test)28 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)23 Multipart (javax.mail.Multipart)20 InputStream (java.io.InputStream)18 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)16 Session (javax.mail.Session)16 Date (java.util.Date)15 Properties (java.util.Properties)15