Example usage for javax.mail.internet MimeUtility mimeCharset

List of usage examples for javax.mail.internet MimeUtility mimeCharset

Introduction

In this page you can find the example usage for javax.mail.internet MimeUtility mimeCharset.

Prototype

public static String mimeCharset(String charset) 

Source Link

Document

Convert a java charset into its MIME charset name.

Usage

From source file:com.haulmont.cuba.core.app.EmailSender.java

protected MimeBodyPart createAttachmentPart(SendingAttachment attachment) throws MessagingException {
    DataSource source = new MyByteArrayDataSource(attachment.getContent());

    String mimeType = FileTypesHelper.getMIMEType(attachment.getName());
    String encodedFileName = encodeAttachmentName(attachment);

    String contentId = attachment.getContentId();
    if (contentId == null) {
        contentId = encodedFileName;/*from   w w w  .ja va2  s.  c  om*/
    }

    String disposition = attachment.getDisposition() != null ? attachment.getDisposition() : Part.INLINE;
    String charset = MimeUtility.mimeCharset(
            attachment.getEncoding() != null ? attachment.getEncoding() : StandardCharsets.UTF_8.name());
    String contentTypeValue = String.format("%s; charset=%s; name=\"%s\"", mimeType, charset, encodedFileName);

    MimeBodyPart attachmentPart = new MimeBodyPart();
    attachmentPart.setDataHandler(new DataHandler(source));
    attachmentPart.setHeader("Content-ID", "<" + contentId + ">");
    attachmentPart.setHeader("Content-Type", contentTypeValue);
    attachmentPart.setFileName(encodedFileName);
    attachmentPart.setDisposition(disposition);

    return attachmentPart;
}

From source file:immf.Util.java

public static String encodeParameter(String name, String value, String encoding, String lang) {
    StringBuffer result = new StringBuffer();
    StringBuffer encodedPart = new StringBuffer();

    boolean needWriteCES = !isAllAscii(value);
    boolean CESWasWritten = false;
    boolean encoded;
    boolean needFolding = false;
    int sequenceNo = 0;
    int column;/*from   w w w  . j  a v a  2s .  c om*/

    while (value.length() > 0) {
        // index of boundary of ascii/non ascii
        int lastIndex;
        boolean isAscii = value.charAt(0) < 0x80;
        for (lastIndex = 1; lastIndex < value.length(); lastIndex++) {
            if (value.charAt(lastIndex) < 0x80) {
                if (!isAscii)
                    break;
            } else {
                if (isAscii)
                    break;
            }
        }
        if (lastIndex != value.length())
            needFolding = true;

        RETRY: while (true) {
            encodedPart.delete(0, encodedPart.length());
            String target = value.substring(0, lastIndex);

            byte[] bytes;
            try {
                if (isAscii) {
                    bytes = target.getBytes("us-ascii");
                } else {
                    bytes = target.getBytes(encoding);
                }
            } catch (UnsupportedEncodingException e) {
                bytes = target.getBytes(); // use default encoding
                encoding = MimeUtility.mimeCharset(MimeUtility.getDefaultJavaCharset());
            }

            encoded = false;
            // It is not strict.
            column = name.length() + 7; // size of " " and "*nn*=" and ";"

            for (int i = 0; i < bytes.length; i++) {
                if (bytes[i] > ' ' && bytes[i] < 'z' && HeaderTokenizer.MIME.indexOf((char) bytes[i]) < 0) {
                    encodedPart.append((char) bytes[i]);
                    column++;
                } else {
                    encoded = true;
                    encodedPart.append('%');
                    String hex = Integer.toString(bytes[i] & 0xff, 16);
                    if (hex.length() == 1) {
                        encodedPart.append('0');
                    }
                    encodedPart.append(hex);
                    column += 3;
                }
                if (column > 76) {
                    needFolding = true;
                    lastIndex /= 2;
                    continue RETRY;
                }
            }

            result.append(";\r\n ").append(name);
            if (needFolding) {
                result.append('*').append(sequenceNo);
                sequenceNo++;
            }
            if (!CESWasWritten && needWriteCES) {
                result.append("*=");
                CESWasWritten = true;
                result.append(encoding).append('\'');
                if (lang != null)
                    result.append(lang);
                result.append('\'');
            } else if (encoded) {
                result.append("*=");
            } else {
                result.append('=');
            }
            result.append(new String(encodedPart));
            value = value.substring(lastIndex);
            break;
        }
    }
    return new String(result);
}