Search in sources :

Example 41 with DataHandler

use of javax.activation.DataHandler in project zm-mailbox by Zimbra.

the class ZMimeBodyPart method recalculateTransferEncoding.

static void recalculateTransferEncoding(ZMimePart part) throws MessagingException {
    // don't overwrite an explicitly-set transfer encoding
    if (part.getHeader("Content-Transfer-Encoding") != null)
        return;
    // don't set transfer encoding on messages or multiparts
    DataHandler dh = part.getDataHandler();
    ZContentType ctype = new ZContentType(dh.getContentType());
    if (ctype.getBaseType().equals("message/rfc822") || ctype.getPrimaryType().equals("multipart"))
        return;
    boolean isText = ctype.getPrimaryType().equals("text");
    String disp = part.getDisposition();
    boolean isAttachment = disp != null && disp.equals(ATTACHMENT);
    String encoding = "base64";
    if (dh.getName() == null) {
        try {
            EncodingOutputStream eos = new EncodingOutputStream();
            dh.writeTo(eos);
            encoding = eos.tester.getEncoding(isAttachment, isText);
        } catch (Exception e) {
        }
    } else {
        EncodingInputStream eis = null;
        try {
            eis = new EncodingInputStream(dh.getDataSource().getInputStream());
            ByteUtil.drain(eis, false);
            encoding = eis.tester.getEncoding(isAttachment, isText);
        } catch (IOException e) {
        } finally {
            ByteUtil.closeStream(eis);
        }
    }
    part.setHeader("Content-Transfer-Encoding", encoding);
}
Also used : DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 42 with DataHandler

use of javax.activation.DataHandler in project zm-mailbox by Zimbra.

the class ZMimeMessage method cacheContent.

public void cacheContent(Object o) {
    dh = new DataHandler(o, null);
    boolean placeholder = content == null && contentStream == null;
    if (placeholder) {
        content = ZMimeBodyPart.CONTENT_PLACEHOLDER;
    }
    try {
        super.getContent();
    } catch (Exception e) {
    // should never happen
    } finally {
        dh = null;
        if (placeholder) {
            content = null;
        }
    }
}
Also used : DataHandler(javax.activation.DataHandler) AddressException(javax.mail.internet.AddressException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 43 with DataHandler

use of javax.activation.DataHandler 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 44 with DataHandler

use of javax.activation.DataHandler 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 DataHandler

use of javax.activation.DataHandler in project perun by CESNET.

the class MessagePreparator method createAttachment.

/**
	 * Creates attachment from filename
	 *
	 * @param filename
	 * @return
	 * @throws Exception
	 */
private BodyPart createAttachment(String filename) throws Exception {
    BodyPart attachBodypart = new MimeBodyPart();
    File file = new File(filename);
    FileDataSource fds = new FileDataSource(file);
    DataHandler dh = new DataHandler(fds);
    attachBodypart.setFileName(file.getName());
    attachBodypart.setDisposition(Part.ATTACHMENT);
    attachBodypart.setDescription("Attached file: " + file.getName());
    attachBodypart.setDataHandler(dh);
    logger.debug("ATTACHMENT ADDED ; filename: " + filename);
    return attachBodypart;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

DataHandler (javax.activation.DataHandler)180 Exchange (org.apache.camel.Exchange)39 MimeBodyPart (javax.mail.internet.MimeBodyPart)38 FileDataSource (javax.activation.FileDataSource)33 Test (org.junit.Test)33 DataSource (javax.activation.DataSource)32 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)29 IOException (java.io.IOException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 InputStream (java.io.InputStream)25 MessagingException (javax.mail.MessagingException)25 MimeMultipart (javax.mail.internet.MimeMultipart)25 MimeMessage (javax.mail.internet.MimeMessage)23 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Message (org.apache.camel.Message)21 OMElement (org.apache.axiom.om.OMElement)17 Processor (org.apache.camel.Processor)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 File (java.io.File)13 PipedInputStream (java.io.PipedInputStream)13