Example usage for javax.mail Part setHeader

List of usage examples for javax.mail Part setHeader

Introduction

In this page you can find the example usage for javax.mail Part setHeader.

Prototype

public void setHeader(String header_name, String header_value) throws MessagingException;

Source Link

Document

Set the value for this header_name.

Usage

From source file:immf.Util.java

public static void setFileName(Part part, String filename, String charset, String lang)
        throws MessagingException {

    ContentDisposition disposition;/*from  ww  w . j a va 2s .com*/
    String[] strings = part.getHeader("Content-Disposition");
    if (strings == null || strings.length < 1) {
        disposition = new ContentDisposition(Part.ATTACHMENT);
    } else {
        disposition = new ContentDisposition(strings[0]);
        disposition.getParameterList().remove("filename");
    }

    part.setHeader("Content-Disposition",
            disposition.toString() + encodeParameter("filename", filename, charset, lang));

    ContentType cType;
    strings = part.getHeader("Content-Type");
    if (strings == null || strings.length < 1) {
        cType = new ContentType(part.getDataHandler().getContentType());
    } else {
        cType = new ContentType(strings[0]);
    }

    try {
        // I want to public the MimeUtility#doEncode()!!!
        String mimeString = MimeUtility.encodeWord(filename, charset, "B");
        // cut <CRLF>...
        StringBuffer sb = new StringBuffer();
        int i;
        while ((i = mimeString.indexOf('\r')) != -1) {
            sb.append(mimeString.substring(0, i));
            mimeString = mimeString.substring(i + 2);
        }
        sb.append(mimeString);

        cType.setParameter("name", new String(sb));
    } catch (UnsupportedEncodingException e) {
        throw new MessagingException("Encoding error", e);
    }
    part.setHeader("Content-Type", cType.toString());
}

From source file:com.szmslab.quickjavamail.send.MailSender.java

/**
 * ??????/*from  w  w  w  .  ja v a 2  s  . c  om*/
 *
 * @param part
 *            ?
 * @throws MessagingException
 */
private void setHeaderToPart(Part part) throws MessagingException {
    for (Iterator<Map.Entry<Object, Object>> itr = headers.entrySet().iterator(); itr.hasNext();) {
        Map.Entry<Object, Object> head = itr.next();
        part.setHeader(head.getKey().toString(), head.getValue().toString());
    }
}

From source file:com.aimluck.eip.mail.util.ALMailUtils.java

/**
 * ??? Part#setText() ???????????/*from  ww  w.j  a v a 2  s. c o  m*/
 */
public static void setTextContent(Part p, String s) throws MessagingException {
    p.setDataHandler(new DataHandler(new JISDataSource(s)));
    p.setHeader(ALLocalMailMessage.CONTENT_TRANSFER_ENCORDING, "7bit");
}

From source file:mitm.common.security.smime.handler.SMIMEInfoHandlerImpl.java

private void setHeader(String headerName, String headerValue, int level, Part part) throws MessagingException {
    if (headerValue == null) {
        headerValue = "";
    }//from  w  w  w  .ja v a2  s  . co  m

    headerName = headerName + "-" + Integer.toString(level);

    headerValue = MiscStringUtils.restrictLength(headerValue, maxHeaderLength);

    try {
        /* make sure the header only contains ASCII characters and is folded
         * when necessary
         */
        headerValue = HeaderUtils.encodeHeaderValue(headerName, headerValue);
    } catch (UnsupportedEncodingException e) {
        logger.warn("Header value cannot be encoded. Message: " + e.getMessage());
    }

    part.setHeader(headerName, headerValue);
}

From source file:org.orbeon.oxf.processor.EmailProcessor.java

private void handlePart(PipelineContext pipelineContext, String dataInputSystemId, Part parentPart,
        Element partOrBodyElement) throws Exception {
    final String name = partOrBodyElement.attributeValue("name");
    String contentTypeAttribute = partOrBodyElement.attributeValue("content-type");
    final String contentType = NetUtils.getContentTypeMediaType(contentTypeAttribute);
    final String charset;
    {/* w ww.  ja v a  2  s  . c o  m*/
        final String c = NetUtils.getContentTypeCharset(contentTypeAttribute);
        charset = (c != null) ? c : DEFAULT_CHARACTER_ENCODING;
    }
    final String contentTypeWithCharset = contentType + "; charset=" + charset;
    final String src = partOrBodyElement.attributeValue("src");

    // Either a String or a FileItem
    final Object content;
    if (src != null) {
        // Content of the part is not inline

        // Generate a FileItem from the source
        final SAXSource source = getSAXSource(EmailProcessor.this, pipelineContext, src, dataInputSystemId,
                contentType);
        content = handleStreamedPartContent(pipelineContext, source);
    } else {
        // Content of the part is inline

        // In the cases of text/html and XML, there must be exactly one root element
        final boolean needsRootElement = "text/html".equals(contentType);// || ProcessorUtils.isXMLContentType(contentType);
        if (needsRootElement && partOrBodyElement.elements().size() != 1)
            throw new ValidationException(
                    "The <body> or <part> element must contain exactly one element for text/html",
                    (LocationData) partOrBodyElement.getData());

        // Create Document and convert it into a String
        final Element rootElement = (Element) (needsRootElement ? partOrBodyElement.elements().get(0)
                : partOrBodyElement);
        final Document partDocument = new NonLazyUserDataDocument();
        partDocument.setRootElement((Element) rootElement.clone());
        content = handleInlinePartContent(partDocument, contentType);
    }

    if (!XMLUtils.isTextOrJSONContentType(contentType)) {
        // This is binary content (including application/xml)
        if (content instanceof FileItem) {
            final FileItem fileItem = (FileItem) content;
            parentPart.setDataHandler(new DataHandler(new DataSource() {
                public String getContentType() {
                    return contentType;
                }

                public InputStream getInputStream() throws IOException {
                    return fileItem.getInputStream();
                }

                public String getName() {
                    return name;
                }

                public OutputStream getOutputStream() throws IOException {
                    throw new IOException("Write operation not supported");
                }
            }));
        } else {
            byte[] data = NetUtils.base64StringToByteArray((String) content);
            parentPart.setDataHandler(new DataHandler(new SimpleBinaryDataSource(name, contentType, data)));
        }
    } else {
        // This is text content (including text/xml)
        if (content instanceof FileItem) {
            // The text content was encoded when written to the FileItem
            final FileItem fileItem = (FileItem) content;
            parentPart.setDataHandler(new DataHandler(new DataSource() {
                public String getContentType() {
                    // This always contains a charset
                    return contentTypeWithCharset;
                }

                public InputStream getInputStream() throws IOException {
                    // This is encoded with the appropriate charset (user-defined, or the default)
                    return fileItem.getInputStream();
                }

                public String getName() {
                    return name;
                }

                public OutputStream getOutputStream() throws IOException {
                    throw new IOException("Write operation not supported");
                }
            }));
        } else {
            parentPart.setDataHandler(
                    new DataHandler(new SimpleTextDataSource(name, contentTypeWithCharset, (String) content)));
        }
    }

    // Set content-disposition header
    String contentDisposition = partOrBodyElement.attributeValue("content-disposition");
    if (contentDisposition != null)
        parentPart.setDisposition(contentDisposition);

    // Set content-id header
    String contentId = partOrBodyElement.attributeValue("content-id");
    if (contentId != null)
        parentPart.setHeader("content-id", "<" + contentId + ">");
    //part.setContentID(contentId);
}