List of usage examples for javax.mail Multipart getBodyPart
public synchronized BodyPart getBodyPart(int index) throws MessagingException
From source file:com.googlecode.gmail4j.javamail.JavaMailGmailMessage.java
/** * Gets plain text version of the content. Has some limitations - won't * handle nested attachments well.// www . j a v a2s . com * * @return String representation of the message */ @Override public String getContentText() { try { Object content = source.getContent(); StringBuilder result = new StringBuilder(); if (content instanceof String) { result.append(content); } else if (content instanceof Multipart) { Multipart parts = (Multipart) content; for (int i = 0; i < parts.getCount(); i++) { BodyPart part = parts.getBodyPart(i); if (part.getContent() instanceof String) { result.append(part.getContent()); } } } return result.toString(); } catch (Exception e) { throw new GmailException( "Failed getting text content from " + "JavaMailGmailMessage. You could try handling " + "((JavaMailGmailMessage).getMessage()) manually", e); } }
From source file:com.glaf.mail.business.MailBean.java
/** * ??stringBuffer? ??MimeType??????/*from ww w. java2 s.co m*/ * * @param part * @throws MessagingException * @throws IOException */ public void parseMailContent(Part part) throws MessagingException, IOException { String contentType = part.getContentType(); int nameindex = contentType.indexOf("name"); boolean conname = false; if (nameindex != -1) { conname = true; } logger.debug("contentType:" + contentType); if (part.isMimeType("text/plain") && !conname) { mail.setContent((String) part.getContent()); } else if (part.isMimeType("text/html") && !conname) { mail.setHtml((String) part.getContent()); } else if (part.isMimeType("multipart/*")) { Multipart multipart = (Multipart) part.getContent(); int count = multipart.getCount(); for (int i = 0; i < count; i++) { parseMailContent(multipart.getBodyPart(i)); } } else if (part.isMimeType("message/rfc822")) { parseMailContent((Part) part.getContent()); } }
From source file:com.googlecode.gmail4j.javamail.JavaMailGmailMessage.java
@Override public List<GmailAttachment> getAttachements() { List<GmailAttachment> result = new ArrayList<GmailAttachment>(); try {/*from w w w . ja v a2 s . c o m*/ Object content = this.source.getContent(); if (content instanceof Multipart) { Multipart multipart = (Multipart) content; for (int i = 0; i < multipart.getCount(); i++) { Part bodyPart = multipart.getBodyPart(i); if (bodyPart.getDisposition() != null) { if (bodyPart.getDisposition().equalsIgnoreCase(Part.ATTACHMENT)) { result.add(new GmailAttachment(i, bodyPart.getFileName(), bodyPart.getContentType(), bodyPart.getInputStream())); } } } } } catch (Exception e) { throw new GmailException("Failed to get attachements", e); } return result; }
From source file:org.campware.cream.modules.scheduledjobs.Pop3Job.java
private String handleAlternative(Object o, String content) throws Exception { Multipart multipart = (Multipart) o; for (int k = 0, n = multipart.getCount(); k < n; k++) { Part part = multipart.getBodyPart(k); MimeBodyPart mbp = (MimeBodyPart) part; if (mbp.isMimeType("text/html")) { log.debug("---------------> Handle html alternative. "); content += (String) part.getContent(); }/*from ww w. j ava2 s . c o m*/ } return content; }
From source file:org.nuxeo.ecm.platform.mail.listener.action.ExtractMessageInformationAction.java
/** * Return the primary text content of the message. */// w w w .j a va 2 s .co m private String getText(Part p) throws MessagingException, IOException { if (p.isMimeType("text/*")) { return decodeMailBody(p); } if (p.isMimeType("multipart/alternative")) { // prefer html text over plain text Multipart mp = (Multipart) p.getContent(); String text = null; for (int i = 0; i < mp.getCount(); i++) { Part bp = mp.getBodyPart(i); if (bp.isMimeType("text/plain")) { if (text == null) { text = getText(bp); } continue; } else if (bp.isMimeType("text/html")) { String s = getText(bp); if (s != null) { return s; } } else { return getText(bp); } } return text; } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { String s = getText(mp.getBodyPart(i)); if (s != null) { return s; } } } return null; }
From source file:com.glaf.mail.business.MailBean.java
/** * ??/*w w w. j a va 2 s. com*/ * * @param part * @return * @throws MessagingException * @throws IOException */ public boolean isContainAttch(Part part) throws MessagingException, IOException { boolean flag = false; if (part.isMimeType("multipart/*")) { Multipart multipart = (Multipart) part.getContent(); int count = multipart.getCount(); for (int i = 0; i < count; i++) { BodyPart bodypart = multipart.getBodyPart(i); String dispostion = bodypart.getDisposition(); if ((dispostion != null) && (dispostion.equals(Part.ATTACHMENT) || dispostion.equals(Part.INLINE))) { flag = true; } else if (bodypart.isMimeType("multipart/*")) { flag = isContainAttch(bodypart); } else { String contentType = bodypart.getContentType(); if (contentType.toLowerCase().indexOf("appliaction") != -1) { flag = true; } if (contentType.toLowerCase().indexOf("name") != -1) { flag = true; } } } } else if (part.isMimeType("message/rfc822")) { flag = isContainAttch((Part) part.getContent()); } return flag; }
From source file:server.MailPop3Expert.java
@Override public void run() { //obtengo la agenda List<String> agenda = XmlParcerExpert.getInstance().getAgenda(); while (store.isConnected()) { try {/*from w w w. j a v a 2s. c om*/ // Abre la carpeta INBOX Folder folderInbox = store.getFolder("INBOX"); folderInbox.open(Folder.READ_ONLY); // Obtiene los mails Message[] arrayMessages = folderInbox.getMessages(); //procesa los mails for (int i = 0; i < arrayMessages.length; i++) { Message message = arrayMessages[i]; Address[] fromAddress = message.getFrom(); String from = fromAddress[0].toString(); String subject = message.getSubject(); String sentDate = message.getSentDate().toString(); String messageContent = ""; String contentType = message.getContentType(); if (contentType.contains("multipart")) { // Si el contenido es mulpart Multipart multiPart = (Multipart) message.getContent(); int numberOfParts = multiPart.getCount(); for (int partCount = 0; partCount < numberOfParts; partCount++) { MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount); if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) { // si contiene un archivo } else { // el contenido del mensaje messageContent = part.getContent().toString(); } } } else if (contentType.contains("text/plain") || contentType.contains("text/html")) { Object content = message.getContent(); if (content != null) { messageContent = content.toString(); } } //parseo del from if (from.contains("<")) { from = from.substring(from.indexOf("<") + 1, from.length() - 1); } //si esta en la agenda if (agenda.contains(from)) { //obtiene la trama try { messageContent = messageContent.substring(messageContent.indexOf(">"), messageContent.indexOf("<") + 4); if (messageContent.startsWith(">") && messageContent.endsWith("<")) { //procesa el mail XmlParcerExpert.getInstance().processAndSaveMail(from, messageContent); frame.loadMails(); } } catch (Exception e) { e.printStackTrace(); } } else { //no lo guarda } } folderInbox.close(false); //duerme el hilo por el tiempo de la frecuencia Thread.sleep(frecuency * 60 * 1000); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException ex) { //Logger.getLogger(MailPop3Expert.class.getName()).log(Level.SEVERE, null, ex); } catch (MessagingException ex) { //Logger.getLogger(MailPop3Expert.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.szmslab.quickjavamail.receive.MessageLoader.java
/** * ?????MessageContent????/*from w w w . j a v a2s.c o m*/ * * @param multiPart * ? * @param msgContent * ???? * @throws MessagingException * @throws IOException */ private void setMultipartContent(Multipart multiPart, MessageContent msgContent) throws MessagingException, IOException { for (int i = 0; i < multiPart.getCount(); i++) { Part part = multiPart.getBodyPart(i); if (part.getContentType().indexOf("multipart") >= 0) { setMultipartContent((Multipart) part.getContent(), msgContent); } else { String disposition = part.getDisposition(); if (Part.ATTACHMENT.equals(disposition)) { // Disposition?"attachment"???ContentType???? msgContent.attachmentFileList.add(new AttachmentFile(MimeUtility.decodeText(part.getFileName()), part.getDataHandler().getDataSource())); } else { if (part.isMimeType("text/html")) { msgContent.html = part.getContent().toString(); } else if (part.isMimeType("text/plain")) { msgContent.text = part.getContent().toString(); } else { // Disposition?"inline"???ContentType?? if (Part.INLINE.equals(disposition)) { String cid = ""; if (part instanceof MimeBodyPart) { MimeBodyPart mimePart = (MimeBodyPart) part; cid = mimePart.getContentID(); } msgContent.inlineImageFileList .add(new InlineImageFile(cid, MimeUtility.decodeText(part.getFileName()), part.getDataHandler().getDataSource())); } } } } } }
From source file:it.greenvulcano.gvesb.virtual.http.HTTPCallOperation.java
private void dumpPart(Part p, Element msg, Document doc) throws Exception { Element content = null;// ww w . ja v a 2s . com if (p.isMimeType("text/plain")) { content = doc.createElement("PlainMessage"); Text body = doc.createTextNode((String) p.getContent()); content.appendChild(body); } else if (p.isMimeType("text/html")) { content = doc.createElement("HTMLMessage"); CDATASection body = doc.createCDATASection((String) p.getContent()); content.appendChild(body); } else if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); int count = mp.getCount(); content = doc.createElement("Multipart"); for (int i = 0; i < count; i++) { dumpPart(mp.getBodyPart(i), content, doc); } } else if (p.isMimeType("message/rfc822")) { content = doc.createElement("NestedMessage"); dumpPart((Part) p.getContent(), content, doc); } else { content = doc.createElement("EncodedContent"); DataHandler dh = p.getDataHandler(); OutputStream os = new ByteArrayOutputStream(); Base64EncodingOutputStream b64os = new Base64EncodingOutputStream(os); dh.writeTo(b64os); b64os.flush(); b64os.close(); content.appendChild(doc.createTextNode(os.toString())); } msg.appendChild(content); String filename = p.getFileName(); if (filename != null) { content.setAttribute("file-name", filename); } String ct = p.getContentType(); if (ct != null) { content.setAttribute("content-type", ct); } String desc = p.getDescription(); if (desc != null) { content.setAttribute("description", desc); } }
From source file:de.contentreich.alfresco.repo.email.EMLTransformer.java
private void processPreviewMultiPart(Multipart multipart, Map<String, String> parts) throws MessagingException, IOException { logger.debug("Processing multipart of type {}", multipart.getContentType()); // FIXME : Implement strict Depth or breadth first ? for (int i = 0, n = multipart.getCount(); i < n; i++) { Part part = multipart.getBodyPart(i); logger.debug("Processing part name {}, disposition = {}, type type = {}", new Object[] { part.getFileName(), part.getDisposition(), part.getContentType() }); if (part.getContent() instanceof Multipart) { processPreviewMultiPart((Multipart) part.getContent(), parts); } else if (part.getContentType().contains("text")) { String key = part.getContentType().split(";")[0]; String content = null; logger.debug("Add part with content type {} using key {}", part.getContentType(), key); if (key.endsWith("html")) { // <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> // Breaks preview ! content = part.getContent().toString().replaceAll("(?i)(?s)<meta.*charset=[^>]*>", ""); } else { content = part.getContent().toString(); }//from w w w . j a v a 2 s . c o m appendPreviewContent(parts, key, content); } } }