Search in sources :

Example 76 with Multipart

use of javax.mail.Multipart in project alfresco-repository by Alfresco.

the class AttachmentsExtractor method extractAttachments.

public void extractAttachments(NodeRef messageRef, MimeMessage originalMessage) throws IOException, MessagingException {
    NodeRef attachmentsFolderRef = null;
    String attachmentsFolderName = null;
    boolean createFolder = false;
    switch(attachmentsExtractorMode) {
        case SAME:
            attachmentsFolderRef = nodeService.getPrimaryParent(messageRef).getParentRef();
            break;
        case COMMON:
            attachmentsFolderRef = this.attachmentsFolderRef;
            break;
        case SEPARATE:
        default:
            String messageName = (String) nodeService.getProperty(messageRef, ContentModel.PROP_NAME);
            attachmentsFolderName = messageName + "-attachments";
            createFolder = true;
            break;
    }
    if (!createFolder) {
        nodeService.createAssociation(messageRef, attachmentsFolderRef, ImapModel.ASSOC_IMAP_ATTACHMENTS_FOLDER);
    }
    Object content = originalMessage.getContent();
    if (content instanceof Multipart) {
        Multipart multipart = (Multipart) content;
        for (int i = 0, n = multipart.getCount(); i < n; i++) {
            Part part = multipart.getBodyPart(i);
            if ("attachment".equalsIgnoreCase(part.getDisposition())) {
                if (createFolder) {
                    attachmentsFolderRef = createAttachmentFolder(messageRef, attachmentsFolderName);
                    createFolder = false;
                }
                createAttachment(messageRef, attachmentsFolderRef, part);
            }
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Multipart(javax.mail.Multipart) Part(javax.mail.Part)

Example 77 with Multipart

use of javax.mail.Multipart in project alfresco-repository by Alfresco.

the class SubethaEmailMessage method parseMessagePart.

private void parseMessagePart(Part messagePart) {
    try {
        if (messagePart.isMimeType(MIME_PLAIN_TEXT) || messagePart.isMimeType(MIME_HTML_TEXT)) {
            if (log.isDebugEnabled()) {
                log.debug("Text or HTML part was found. ContentType: " + messagePart.getContentType());
            }
            addBody(messagePart);
        } else if (messagePart.isMimeType(MIME_XML_TEXT)) {
            if (log.isDebugEnabled()) {
                log.debug("XML part was found.");
            }
            addAttachment(messagePart);
        } else if (messagePart.isMimeType(MIME_APPLICATION)) {
            if (log.isDebugEnabled()) {
                log.debug("Application part was found.");
            }
            addAttachment(messagePart);
        } else if (messagePart.isMimeType(MIME_IMAGE)) {
            if (log.isDebugEnabled()) {
                log.debug("Image part was found.");
            }
            addAttachment(messagePart);
        } else if (messagePart.isMimeType(MIME_MULTIPART)) {
            // if multipart, this method will be called recursively
            // for each of its parts
            Multipart mp = (Multipart) messagePart.getContent();
            int count = mp.getCount();
            if (log.isDebugEnabled()) {
                log.debug("MULTIPART with " + count + " part(s) found. Processin each part...");
            }
            for (int i = 0; i < count; i++) {
                BodyPart bp = mp.getBodyPart(i);
                if (bp.getContent() instanceof MimeMultipart) {
                    // It's multipart.  Recurse.
                    parseMessagePart(bp);
                } else {
                    // It's the body
                    addBody(bp);
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("MULTIPART processed.");
            }
        } else if (messagePart.isMimeType(MIME_RFC822)) {
            // if rfc822, call this method with its content as the part
            if (log.isDebugEnabled()) {
                log.debug("MIME_RFC822 part found. Processing inside part...");
            }
            parseMessagePart((Part) messagePart.getContent());
            if (log.isDebugEnabled()) {
                log.debug("MIME_RFC822 processed.");
            }
        } else {
            // Actually we don't know what it is.
            if (log.isDebugEnabled()) {
                log.debug("Unrecognized part was found. Put it into attachments.");
            }
            addAttachment(messagePart);
        }
    } catch (IOException e) {
        throw new EmailMessageException(ERR_PARSE_MESSAGE, e.getMessage());
    } catch (MessagingException e) {
        throw new EmailMessageException(ERR_PARSE_MESSAGE, e.getMessage());
    }
}
Also used : BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) Part(javax.mail.Part) BodyPart(javax.mail.BodyPart) EmailMessagePart(org.alfresco.service.cmr.email.EmailMessagePart) EmailMessageException(org.alfresco.service.cmr.email.EmailMessageException) IOException(java.io.IOException)

Example 78 with Multipart

use of javax.mail.Multipart in project weicoder by wdcode.

the class EmailJava method sendEmail.

/**
 * 发送Email
 *
 * @param to      发送地址
 * @param subject 邮件标题
 * @param msg     邮件内容
 * @param attach  附件
 * @param flag    是否html
 */
private void sendEmail(String[] to, String subject, String msg, String attach, boolean flag) {
    try {
        // 参数设置
        Properties props = new Properties();
        // 指定SMTP服务器
        props.put("mail.host", getHost());
        // 是否需要SMTP验证
        props.put("mail.smtp.auth", isAuth());
        if (isAuth())
            props.put("mail.smtp.password", getPassword());
        // 获得Session
        Session mailSession = Session.getDefaultInstance(props, isAuth() ? new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        } : null);
        // 创建细信息类
        Message message = new MimeMessage(mailSession);
        // 设置邮件服务器
        message.setFrom(new InternetAddress(getFrom()));
        // 收件人
        for (int i = 0; i < to.length; i++) {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
        }
        // 邮件主题
        message.setSubject(subject);
        // 是否支持HTML
        if (flag) {
            // HTML
            message.setContent(msg, getCharset());
        } else {
            // 普通文本
            message.setText(msg);
        }
        // 添加附件
        if (!U.E.isEmpty(attach)) {
            // 附件
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = null;
            // 判断是本地文件还是远程
            if (attach.indexOf("http") == -1) {
                // 本地文件
                source = new FileDataSource(attach);
            } else {
                // 远程文件
                source = new URLDataSource(new URL(attach));
            }
            messageBodyPart.setDataHandler(new DataHandler(source));
            // 设置描述名字等
            String name = StringUtil.subStringLast(attach, StringConstants.BACKSLASH, StringConstants.POINT);
            // 添加文件名和描述
            messageBodyPart.setText(name);
            messageBodyPart.setFileName(name);
            // 附件
            Multipart multipart = new MimeMultipart();
            // 添加附件
            multipart.addBodyPart(messageBodyPart);
            // 添加到正文中
            message.setContent(multipart);
        }
        // 保存设置
        message.saveChanges();
        // 发送邮件
        Transport.send(message);
    } catch (Exception e) {
        Logs.error(e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) URLDataSource(javax.activation.URLDataSource) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) URL(java.net.URL) FileDataSource(javax.activation.FileDataSource) URLDataSource(javax.activation.URLDataSource) DataSource(javax.activation.DataSource) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) MimeBodyPart(javax.mail.internet.MimeBodyPart) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 79 with Multipart

use of javax.mail.Multipart in project translationstudio8 by heartsome.

the class MessageParser method getFiles.

/**
	 * 获取消息附件中的文件.
	 * @return List&lt;ResourceFileBean&gt;
	 * 				文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象)
	 * @throws MessagingException
	 *             the messaging exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
public List<ResourceFileBean> getFiles() throws MessagingException, IOException {
    List<ResourceFileBean> resourceList = new ArrayList<ResourceFileBean>();
    Object content = message.getContent();
    Multipart mp = null;
    if (content instanceof Multipart) {
        mp = (Multipart) content;
    } else {
        return resourceList;
    }
    for (int i = 0, n = mp.getCount(); i < n; i++) {
        Part part = mp.getBodyPart(i);
        //此方法返回 Part 对象的部署类型。
        String disposition = part.getDisposition();
        //Part.INLINE 指示 Part 对象以内联方式显示。
        if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) {
            //part.getFileName():返回 Part 对象的文件名。
            String fileName = MimeUtility.decodeText(part.getFileName());
            //此方法为 Part 对象返回一个 InputStream 对象
            InputStream is = part.getInputStream();
            resourceList.add(new ResourceFileBean(fileName, is));
        } else if (disposition == null) {
            //附件也可以没有部署类型的方式存在
            getRelatedPart(part, resourceList);
        }
    }
    return resourceList;
}
Also used : Multipart(javax.mail.Multipart) Part(javax.mail.Part) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList)

Example 80 with Multipart

use of javax.mail.Multipart in project jodd by oblac.

the class ReceivedEmail method processPart.

/**
	 * Process single part of received message. All parts are simple added to the message, i.e. hierarchy is not saved.
	 */
protected void processPart(ReceivedEmail email, Part part) throws IOException, MessagingException {
    Object content = part.getContent();
    if (content instanceof String) {
        String stringContent = (String) content;
        String disposition = part.getDisposition();
        if (disposition != null && disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
            String contentType = part.getContentType();
            String mimeType = EmailUtil.extractMimeType(contentType);
            String encoding = EmailUtil.extractEncoding(contentType);
            String fileName = part.getFileName();
            String contentId = (part instanceof MimePart) ? ((MimePart) part).getContentID() : null;
            if (encoding == null) {
                encoding = StringPool.US_ASCII;
            }
            email.addAttachment(fileName, mimeType, contentId, stringContent.getBytes(encoding));
        } else {
            String contentType = part.getContentType();
            String encoding = EmailUtil.extractEncoding(contentType);
            String mimeType = EmailUtil.extractMimeType(contentType);
            if (encoding == null) {
                encoding = StringPool.US_ASCII;
            }
            email.addMessage(stringContent, mimeType, encoding);
        }
    } else if (content instanceof Multipart) {
        Multipart mp = (Multipart) content;
        int count = mp.getCount();
        for (int i = 0; i < count; i++) {
            Part innerPart = mp.getBodyPart(i);
            processPart(email, innerPart);
        }
    } else if (content instanceof InputStream) {
        String fileName = EmailUtil.resolveFileName(part);
        String contentId = (part instanceof MimePart) ? ((MimePart) part).getContentID() : null;
        String mimeType = EmailUtil.extractMimeType(part.getContentType());
        InputStream is = (InputStream) content;
        FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
        StreamUtil.copy(is, fbaos);
        email.addAttachment(fileName, mimeType, contentId, fbaos.toByteArray());
    } else if (content instanceof MimeMessage) {
        MimeMessage mimeMessage = (MimeMessage) content;
        addAttachmentMessage(new ReceivedEmail(mimeMessage));
    } else {
        String fileName = part.getFileName();
        String contentId = (part instanceof MimePart) ? ((MimePart) part).getContentID() : null;
        String mimeType = EmailUtil.extractMimeType(part.getContentType());
        InputStream is = part.getInputStream();
        FastByteArrayOutputStream fbaos = new FastByteArrayOutputStream();
        StreamUtil.copy(is, fbaos);
        StreamUtil.close(is);
        email.addAttachment(fileName, mimeType, contentId, fbaos.toByteArray());
    }
}
Also used : Multipart(javax.mail.Multipart) FastByteArrayOutputStream(jodd.io.FastByteArrayOutputStream) MimeMessage(javax.mail.internet.MimeMessage) Part(javax.mail.Part) MimePart(javax.mail.internet.MimePart) InputStream(java.io.InputStream) MimePart(javax.mail.internet.MimePart)

Aggregations

Multipart (javax.mail.Multipart)148 MimeMultipart (javax.mail.internet.MimeMultipart)107 MimeBodyPart (javax.mail.internet.MimeBodyPart)92 MimeMessage (javax.mail.internet.MimeMessage)82 BodyPart (javax.mail.BodyPart)62 InternetAddress (javax.mail.internet.InternetAddress)61 MessagingException (javax.mail.MessagingException)58 Session (javax.mail.Session)44 DataHandler (javax.activation.DataHandler)42 IOException (java.io.IOException)37 Properties (java.util.Properties)37 Date (java.util.Date)30 Message (javax.mail.Message)30 FileDataSource (javax.activation.FileDataSource)27 DataSource (javax.activation.DataSource)24 InputStream (java.io.InputStream)23 File (java.io.File)21 Part (javax.mail.Part)21 Test (org.junit.Test)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11