Java Code Examples for javax.mail.BodyPart#getDisposition()

The following examples show how to use javax.mail.BodyPart#getDisposition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MimePackage.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the body is from the content type and returns it if not attachment
 *
 * @param mimePart
 * @param contentType
 * @return null if not with specific content type or part is attachment
 */
private String getBodyIfNotAttachment(
                                       BodyPart mimePart,
                                       String contentType ) throws MessagingException, IOException {

    String mimePartContentType = mimePart.getContentType().toLowerCase();
    if (mimePartContentType.startsWith(contentType)) { // found a part with given mime type
        String contentDisposition = mimePart.getDisposition();
        if (!Part.ATTACHMENT.equalsIgnoreCase(contentDisposition)) {
            Object partContent = mimePart.getContent();
            if (partContent instanceof InputStream) {
                return IoUtils.streamToString((InputStream) partContent);
            } else {
                return partContent.toString();
            }
        }
    }
    return null;
}
 
Example 2
Source File: MiscellaneousFunctions.java    From kspl-selenium-helper with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This function returns string of the body from Mime Type
 * @param message -Pass your message
 * @return -Get string of the body.
 * @throws MessagingException - This is thrown when there is any exception in Messaging
 * @throws IOException - This mostly occurs during connection error
    */
public static String bodyFromMimeType(Object message)
		throws MessagingException, IOException {
	String body = message.toString();
	if (message instanceof Multipart) {
		Multipart multipart = (Multipart) message;
		for (int x = 0; x < multipart.getCount(); x++) {
			BodyPart bodyPart = multipart.getBodyPart(x);
			String disposition = bodyPart.getDisposition();
			if (disposition != null
					&& disposition.equals(BodyPart.ATTACHMENT))
				continue;
			else {
				return bodyPart.getContent().toString();
			}

		}
	}
	return body;
}
 
Example 3
Source File: Pop3Util.java    From anyline with Apache License 2.0 5 votes vote down vote up
/** 
 * 判断邮件中是否包含附件 
 *  
 * @param part part 
 * @return 邮件中存在附件返回true,不存在返回false 
 * @throws MessagingException MessagingException
 * @throws IOException IOException
 * @return boolean
 */
public static boolean isContainAttachment(Part part)  throws MessagingException, IOException { 
	boolean flag = false; 
	if (part.isMimeType("multipart/*")) { 
		MimeMultipart multipart = (MimeMultipart) part.getContent(); 
		int partCount = multipart.getCount(); 
		for (int i = 0; i < partCount; i++) { 
			BodyPart bodyPart = multipart.getBodyPart(i); 
			String disp = bodyPart.getDisposition(); 
			if (disp != null 
					&& (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp 
							.equalsIgnoreCase(Part.INLINE))) { 
				flag = true; 
			} else if (bodyPart.isMimeType("multipart/*")) { 
				flag = isContainAttachment(bodyPart); 
			} else { 
				String contentType = bodyPart.getContentType(); 
				if (contentType.indexOf("application") != -1) { 
					flag = true; 
				} 

				if (contentType.indexOf("name") != -1) { 
					flag = true; 
				} 
			} 

			if (flag) 
				break; 
		} 
	} else if (part.isMimeType("message/rfc822")) { 
		flag = isContainAttachment((Part) part.getContent()); 
	} 
	return flag; 
}
 
Example 4
Source File: MimeMessageWrapper.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public String getPartDisposition(String index) {
    BodyPart part = getPart(index);
    if (part != null) {
        try {
            return part.getDisposition();
        } catch (MessagingException e) {
            Debug.logError(e, module);
            return null;
        }
    }
    return null;
}
 
Example 5
Source File: MailUtils.java    From scada with MIT License 5 votes vote down vote up
/**
 * �ж��ʼ����Ƿ��������
 * @param msg �ʼ�����
 * @return �ʼ��д��ڸ�������true�������ڷ���false
 */ 
public static boolean isContainAttachment(Part part) throws MessagingException, IOException { 
    boolean flag = false; 
    if (part.isMimeType("multipart/*")) { 
        MimeMultipart multipart = (MimeMultipart) part.getContent(); 
        int partCount = multipart.getCount(); 
        for (int i = 0; i < partCount; i++) { 
            BodyPart bodyPart = multipart.getBodyPart(i); 
            String disp = bodyPart.getDisposition(); 
            if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { 
                flag = true; 
            } else if (bodyPart.isMimeType("multipart/*")) { 
                flag = isContainAttachment(bodyPart); 
            } else { 
                String contentType = bodyPart.getContentType(); 
                if (contentType.indexOf("application") != -1) { 
                    flag = true; 
                }   
                 
                if (contentType.indexOf("name") != -1) { 
                    flag = true; 
                }  
            } 
             
            if (flag) break; 
        } 
    } else if (part.isMimeType("message/rfc822")) { 
        flag = isContainAttachment((Part)part.getContent()); 
    } 
    return flag; 
}
 
Example 6
Source File: MailUtils.java    From scada with MIT License 5 votes vote down vote up
/** 
 * ���渽�� 
 * @param part �ʼ��ж��������е�����һ������� 
 * @param destDir  ��������Ŀ¼ 
 */ 
public static void saveAttachment(Part part, String destDir) throws UnsupportedEncodingException, MessagingException, 
        FileNotFoundException, IOException { 
    if (part.isMimeType("multipart/*")) { 
        Multipart multipart = (Multipart) part.getContent();    //�������ʼ� 
        //�������ʼ���������ʼ��� 
        int partCount = multipart.getCount(); 
        for (int i = 0; i < partCount; i++) { 
            //��ø������ʼ�������һ���ʼ��� 
            BodyPart bodyPart = multipart.getBodyPart(i); 
            //ijһ���ʼ���Ҳ�п������ɶ���ʼ�����ɵĸ����� 
            String disp = bodyPart.getDisposition(); 
            if (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { 
                InputStream is = bodyPart.getInputStream(); 
                saveFile(is, destDir, decodeText(bodyPart.getFileName())); 
            } else if (bodyPart.isMimeType("multipart/*")) { 
                saveAttachment(bodyPart,destDir); 
            } else { 
                String contentType = bodyPart.getContentType(); 
                if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) { 
                    saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName())); 
                } 
            } 
        } 
    } else if (part.isMimeType("message/rfc822")) { 
        saveAttachment((Part) part.getContent(),destDir); 
    } 
}
 
Example 7
Source File: Pop3Util.java    From anyline with Apache License 2.0 4 votes vote down vote up
/** 
 * 保存附件 
 *  
 * @param part  邮件中多个组合体中的其中一个组合体 
 * @param dir  附件保存目录 
 * @param sendTime sendTime
 * @param files files
 * @throws UnsupportedEncodingException UnsupportedEncodingException 
 * @throws MessagingException MessagingException 
 * @throws FileNotFoundException FileNotFoundException 
 * @throws IOException  IOException
 * @return List
 */ 

public static List<File> downloadAttachment(Part part, String dir, Date sendTime, List<File> files) throws UnsupportedEncodingException, MessagingException, FileNotFoundException, IOException { 
	if (BasicUtil.isEmpty(files)) { 
		files = new ArrayList<File>(); 
	} 
	dir = dir.replace("{send_date}", DateUtil.format(sendTime,"yyyyMMdd")); 
	dir = dir.replace("{send_time}", DateUtil.format(sendTime,"yyyyMMddhhmmss")); 
	if (part.isMimeType("multipart/*")) { 
		Multipart multipart = (Multipart) part.getContent(); // 复杂体邮件 
		int partCount = multipart.getCount(); 
		for (int i = 0; i < partCount; i++) { 
			boolean result = false; 
			BodyPart bodyPart = multipart.getBodyPart(i); 
			String disp = bodyPart.getDisposition(); 
			String name = decode(bodyPart.getFileName()); 
			String path = null; 
			if(dir.contains("{file}")){ 
				path = dir.replace("{file}", name); 
			}else{ 
				path = FileUtil.mergePath(dir, name); 
			} 
			File file =  new File(path); 
			if (BasicUtil.isNotEmpty(name) && disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) { 
				result = FileUtil.save(bodyPart.getInputStream(), file); 
			} else if (bodyPart.isMimeType("multipart/*")) { 
				downloadAttachment(bodyPart, dir, sendTime, files); 
			} else if(BasicUtil.isNotEmpty(name)){ 
				String contentType = bodyPart.getContentType(); 
				if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) { 
					result = FileUtil.save(bodyPart.getInputStream(), file); 
				} 
			} 
			if (result) { 
				files.add(file); 
			} 
		} 
	} else if (part.isMimeType("message/rfc822")) { 
		downloadAttachment((Part) part.getContent(), dir, sendTime, files); 
	} 
	return files; 
}