Search in sources :

Example 6 with Multipart

use of javax.mail.Multipart in project adempiere by adempiere.

the class RequestEMailProcessor method getDeliveryReport.

//	getMessage
/**
	 * 	Get Delivery Report
	 *	@param msg message
	 *	@return delivery info or null
	 */
private String getDeliveryReport(Part msg) {
    try {
        if (msg.isMimeType("multipart/report")) {
            String deliveryMessage = null;
            String otherStuff = null;
            //
            Multipart mp = (Multipart) msg.getContent();
            int count = mp.getCount();
            for (int i = 0; i < count; i++) {
                Part part = mp.getBodyPart(i);
                Object content = part.getContent();
                if (content == null)
                    continue;
                if (part.isMimeType("message/*"))
                    deliveryMessage = getDeliveredReportDetail(part);
                else
                    otherStuff = content.toString().trim();
            }
            if (deliveryMessage != null)
                return deliveryMessage;
            return otherStuff;
        } else if (msg.isMimeType("message/*")) {
            return getDeliveredReportDetail(msg);
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "getDeliveryReport", e);
    }
    //	Nothing
    return null;
}
Also used : Multipart(javax.mail.Multipart) Part(javax.mail.Part) MessagingException(javax.mail.MessagingException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 7 with Multipart

use of javax.mail.Multipart in project adempiere by adempiere.

the class EMailProcessor method dumpBody.

//	printEnvelope
/**
	 * 	Print Body
	 *	@param p
	 *	@throws Exception
	 */
private void dumpBody(Part p) throws Exception {
    //	http://www.iana.org/assignments/media-types/
    printOut("=================================================================");
    printOut("CONTENT-TYPE: " + p.getContentType());
    /**
		 * Using isMimeType to determine the content type avoids
		 * fetching the actual content data until we need it.
		 */
    if (p.isMimeType("text/plain")) {
        printOut("Plain text ---------------------------");
        System.out.println((String) p.getContent());
    } else if (p.getContentType().toUpperCase().startsWith("TEXT")) {
        printOut("Other text ---------------------------");
        System.out.println((String) p.getContent());
    } else if (p.isMimeType("multipart/*")) {
        printOut("Multipart ---------------------------");
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++) dumpBody(mp.getBodyPart(i));
    } else if (p.isMimeType("message/rfc822")) {
        printOut("Nested ---------------------------");
        dumpBody((Part) p.getContent());
    } else {
        /*
			 * If we actually want to see the data, and it's not a
			 * MIME type we know, fetch it and check its Java type.
			 */
        Object o = p.getContent();
        if (o instanceof String) {
            printOut("This is a string ---------------------------");
            System.out.println((String) o);
        } else if (o instanceof InputStream) {
            printOut("This is just an input stream ---------------------------");
            InputStream is = (InputStream) o;
            int c;
            while ((c = is.read()) != -1) System.out.write(c);
        } else {
            printOut("This is an unknown type ---------------------------");
            printOut(o.toString());
        }
    }
    printOut("=================================================================");
}
Also used : Multipart(javax.mail.Multipart) Part(javax.mail.Part) InputStream(java.io.InputStream)

Example 8 with Multipart

use of javax.mail.Multipart in project adempiere by adempiere.

the class EMailProcessor method getMessage.

//	getSubject
/**
	 * 	Get Message
	 *	@param msg Message
	 *	@return message or ""
	 */
private String getMessage(Part msg) {
    StringBuffer sb = new StringBuffer();
    try {
        //	Text
        if (msg.isMimeType("text/plain")) {
            sb.append(msg.getContent());
        } else //	Other Text (e.g. html/xml) 
        if (msg.isMimeType("text/*")) {
            sb.append(msg.getContent());
        } else //	Nested
        if (msg.isMimeType("message/rfc822")) {
            sb.append(msg.getContent());
        } else //	Multi Part Alternative
        if (msg.isMimeType("multipart/alternative")) {
            String plainText = null;
            String otherStuff = null;
            //
            Multipart mp = (Multipart) msg.getContent();
            int count = mp.getCount();
            for (int i = 0; i < count; i++) {
                Part part = mp.getBodyPart(i);
                Object content = part.getContent();
                if (content == null || content.toString().trim().length() == 0)
                    continue;
                if (part.isMimeType("text/plain"))
                    plainText = content.toString();
                else
                    otherStuff = content.toString();
            }
            if (plainText != null)
                sb.append(plainText);
            else if (otherStuff != null)
                sb.append(otherStuff);
        } else //	Multi Part
        if (msg.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) msg.getContent();
            int count = mp.getCount();
            for (int i = 0; i < count; i++) {
                String str = getMessage(mp.getBodyPart(i));
                if (str.length() > 0) {
                    if (sb.length() > 0)
                        sb.append("\n-----\n");
                    sb.append(str);
                }
            }
        } else {
            /*
				 * If we actually want to see the data, and it's not a
				 * MIME type we know, fetch it and check its Java type.
				 */
            Object o = msg.getContent();
            if (o instanceof String) {
                sb.append(o);
            }
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "getMessage", e);
    }
    return sb.toString().trim();
}
Also used : Multipart(javax.mail.Multipart) Part(javax.mail.Part) MessagingException(javax.mail.MessagingException)

Example 9 with Multipart

use of javax.mail.Multipart in project adempiere by adempiere.

the class EMailProcessor method getDeliveryReport.

//	getMessage
/**
	 * 	Get Delivery Report
	 *	@param msg message
	 *	@return delivery info or null
	 */
private String getDeliveryReport(Part msg) {
    try {
        if (msg.isMimeType("multipart/report")) {
            String deliveryMessage = null;
            String otherStuff = null;
            //
            Multipart mp = (Multipart) msg.getContent();
            int count = mp.getCount();
            for (int i = 0; i < count; i++) {
                Part part = mp.getBodyPart(i);
                Object content = part.getContent();
                if (content == null)
                    continue;
                if (part.isMimeType("message/*"))
                    deliveryMessage = getDeliveredReportDetail(part);
                else
                    otherStuff = content.toString().trim();
            }
            if (deliveryMessage != null)
                return deliveryMessage;
            return otherStuff;
        } else if (msg.isMimeType("message/*")) {
            return getDeliveredReportDetail(msg);
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "getDeliveryReport", e);
    }
    //	Nothing
    return null;
}
Also used : Multipart(javax.mail.Multipart) Part(javax.mail.Part) MessagingException(javax.mail.MessagingException)

Example 10 with Multipart

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

the class MessageParser method getRelatedPart.

/**
	 * 获取消息附件中的文件.
	 * @param part
	 *            Part 对象
	 * @param resourceList
	 *            文件的集合(每个 ResourceFileBean 对象中存放文件名和 InputStream 对象)
	 * @throws MessagingException
	 *             the messaging exception
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
private void getRelatedPart(Part part, List<ResourceFileBean> resourceList) throws MessagingException, IOException {
    //验证 Part 对象的 MIME 类型是否与指定的类型匹配。
    if (part.isMimeType("multipart/related")) {
        Multipart mulContent = (Multipart) part.getContent();
        for (int j = 0, m = mulContent.getCount(); j < m; j++) {
            Part contentPart = mulContent.getBodyPart(j);
            if (!contentPart.isMimeType("text/*")) {
                String fileName = "Resource";
                //此方法返回 Part 对象的内容类型。
                String type = contentPart.getContentType();
                if (type != null) {
                    type = type.substring(0, type.indexOf("/"));
                }
                fileName = fileName + "[" + type + "]";
                if (contentPart.getHeader("Content-Location") != null && contentPart.getHeader("Content-Location").length > 0) {
                    fileName = contentPart.getHeader("Content-Location")[0];
                }
                InputStream is = contentPart.getInputStream();
                resourceList.add(new ResourceFileBean(fileName, is));
            }
        }
    } else {
        Multipart mp = null;
        Object content = part.getContent();
        if (content instanceof Multipart) {
            mp = (Multipart) content;
        } else {
            return;
        }
        for (int i = 0, n = mp.getCount(); i < n; i++) {
            Part body = mp.getBodyPart(i);
            getRelatedPart(body, resourceList);
        }
    }
}
Also used : Multipart(javax.mail.Multipart) Part(javax.mail.Part) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream)

Aggregations

Multipart (javax.mail.Multipart)140 MimeMultipart (javax.mail.internet.MimeMultipart)101 MimeBodyPart (javax.mail.internet.MimeBodyPart)87 MimeMessage (javax.mail.internet.MimeMessage)79 BodyPart (javax.mail.BodyPart)60 InternetAddress (javax.mail.internet.InternetAddress)59 MessagingException (javax.mail.MessagingException)54 Session (javax.mail.Session)42 DataHandler (javax.activation.DataHandler)40 Properties (java.util.Properties)34 IOException (java.io.IOException)33 Date (java.util.Date)29 Message (javax.mail.Message)28 FileDataSource (javax.activation.FileDataSource)26 DataSource (javax.activation.DataSource)23 InputStream (java.io.InputStream)22 File (java.io.File)21 Part (javax.mail.Part)19 Test (org.junit.Test)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10