List of usage examples for javax.mail Header getValue
public String getValue()
From source file:MainClass.java
public static void main(String[] args) throws Exception { URLName server = new URLName("protocol://username@host/foldername"); Session session = Session.getDefaultInstance(new Properties(), new MailAuthenticator()); Folder folder = session.getFolder(server); if (folder != null) { folder.open(Folder.READ_ONLY);//from w ww . j a v a 2 s . co m Message[] messages = folder.getMessages(); for (int i = 0; i < messages.length; i++) { System.out.println((i + 1)); Enumeration headers = messages[i].getAllHeaders(); while (headers.hasMoreElements()) { Header h = (Header) headers.nextElement(); System.out.println(h.getName() + ": " + h.getValue()); } } folder.close(false); } }
From source file:ReadHeaders.java
/** Simple demo showing how to use it */ public static void printit(ReadHeaders r) { while (r.more()) { Header h = r.next(); System.out.println(h.getValue()); }//w ww . j a va 2 s.co m }
From source file:com.canoo.webtest.plugins.emailtest.EmailMessageStructureFilter.java
private static String processHeader(final Header h) { return " <header name=\"" + h.getName() + "\" value=\"" + h.getValue() + "\"/>" + LS; }
From source file:com.mgmtp.jfunk.core.mail.MessageUtils.java
/** * Returns the specified message as text. * //from w ww . j a v a 2 s. c om * @param message * the message * @param includeHeaders * specifies whether message headers are to be included in the returned text * @return the message text */ public static String messageAsText(final Message message, final boolean includeHeaders) { try { StrBuilder sb = new StrBuilder(300); if (includeHeaders) { @SuppressWarnings("unchecked") Enumeration<Header> headers = message.getAllHeaders(); while (headers.hasMoreElements()) { Header header = headers.nextElement(); sb.append(header.getName()).append('=').appendln(header.getValue()); } sb.appendln(""); } Object content = message.getContent(); if (content instanceof String) { String body = (String) content; sb.appendln(body); sb.appendln(""); } else if (content instanceof Multipart) { parseMultipart(sb, (Multipart) content); } return sb.toString(); } catch (MessagingException ex) { throw new MailException("Error getting mail content.", ex); } catch (IOException ex) { throw new MailException("Error getting mail content.", ex); } }
From source file:com.mirth.connect.connectors.http.HttpMessageConverter.java
private static void processContent(DonkeyElement contentElement, Object content, ContentType contentType, boolean parseMultipart, BinaryContentTypeResolver resolver) throws DonkeyElementException, MessagingException, IOException { if (resolver == null) { resolver = defaultResolver;//from w w w .j a v a2s . co m } if (parseMultipart && content instanceof MimeMultipart) { contentElement.setAttribute("multipart", "yes"); MimeMultipart multipart = (MimeMultipart) content; String boundary = contentType.getParameter("boundary"); if (StringUtils.isNotBlank(boundary)) { contentElement.setAttribute("boundary", boundary); } if (StringUtils.isNotEmpty(multipart.getPreamble())) { contentElement.addChildElement("Preamble", multipart.getPreamble()); } for (int partIndex = 0; partIndex < multipart.getCount(); partIndex++) { BodyPart part = multipart.getBodyPart(partIndex); DonkeyElement partElement = contentElement.addChildElement("Part"); DonkeyElement headersElement = partElement.addChildElement("Headers"); ContentType partContentType = contentType; for (Enumeration<javax.mail.Header> en = part.getAllHeaders(); en.hasMoreElements();) { javax.mail.Header header = en.nextElement(); headersElement.addChildElement(header.getName(), header.getValue()); if (header.getName().equalsIgnoreCase("Content-Type")) { try { partContentType = ContentType.parse(header.getValue()); } catch (RuntimeException e) { } } } processContent(partElement.addChildElement("Content"), part.getContent(), partContentType, true, resolver); } } else { contentElement.setAttribute("multipart", "no"); String charset = getDefaultHttpCharset( contentType.getCharset() != null ? contentType.getCharset().name() : null); // Call the resolver to determine if the content should be Base64 encoded if (resolver.isBinaryContentType(contentType)) { contentElement.setAttribute("encoding", "Base64"); byte[] contentByteArray = null; if (content instanceof StreamSource) { StreamSource streamSource = (StreamSource) content; if (streamSource.getInputStream() != null) { contentByteArray = IOUtils.toByteArray(streamSource.getInputStream()); } else if (streamSource.getReader() != null) { contentByteArray = IOUtils.toString(streamSource.getReader()).getBytes(charset); } } else if (content instanceof InputStream) { contentByteArray = IOUtils.toByteArray((InputStream) content); } else if (content instanceof byte[]) { contentByteArray = (byte[]) content; } if (contentByteArray == null) { contentByteArray = (content != null ? content.toString() : "").getBytes(charset); } contentElement.setTextContent(new String(Base64Util.encodeBase64(contentByteArray), "US-ASCII")); } else { String contentText = null; if (content instanceof StreamSource) { StreamSource streamSource = (StreamSource) content; if (streamSource.getInputStream() != null) { contentText = IOUtils.toString(streamSource.getInputStream(), charset); } else if (streamSource.getReader() != null) { contentText = IOUtils.toString(streamSource.getReader()); } } else if (content instanceof InputStream) { contentText = IOUtils.toString((InputStream) content, charset); } else if (content instanceof byte[]) { contentText = new String((byte[]) content, charset); } if (contentText == null) { contentText = content != null ? content.toString() : ""; } contentElement.setTextContent(contentText); } } }
From source file:com.stimulus.archiva.domain.EmailID.java
public static synchronized String generateUniqueID(Email email) { try {//w w w. j a va2s . c o m MimeMessage raw = email; // we need a backup plan here if (raw == null) { return DateUtil.convertDatetoString(new Date()); } Enumeration<Header> headers = raw.getAllHeaders(); LinkedList<String> orderedHeaders = new LinkedList<String>(); while (headers.hasMoreElements()) { Header header = headers.nextElement(); if (Compare.equalsIgnoreCase(header.getName(), "Date") || Compare.equalsIgnoreCase(header.getName(), "CC") || Compare.equalsIgnoreCase(header.getName(), "BCC") || Compare.equalsIgnoreCase(header.getName(), "Subject") || Compare.equalsIgnoreCase(header.getName(), "To") || Compare.equalsIgnoreCase(header.getName(), "From")) orderedHeaders.add(header.getName() + header.getValue()); } Collections.sort(orderedHeaders); StringBuffer allHeaders = new StringBuffer(); for (String header : orderedHeaders) allHeaders.append(header); MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] bytes = allHeaders.toString().getBytes(); InputStream is = new ByteArrayInputStream(bytes); DigestInputStream dis = new DigestInputStream(is, sha); while (dis.read() != -1) ; dis.close(); byte[] digest = sha.digest(); return toHex(digest); } catch (Exception e) { logger.error("failed to generate a uniqueid for a message"); return null; } }
From source file:com.naryx.tagfusion.cfm.mail.cfPOP3.java
private static String formatHeader(Message thisMessage) throws Exception { Enumeration<Header> E = thisMessage.getAllHeaders(); StringBuilder tmp = new StringBuilder(128); while (E.hasMoreElements()) { Header hdr = E.nextElement(); tmp.append(hdr.getName());/* w w w . j av a 2 s .c om*/ tmp.append(": "); tmp.append(hdr.getValue()); tmp.append("\r\n"); } return tmp.toString(); }
From source file:de.saly.elasticsearch.support.IndexableMailMessage.java
public static IndexableMailMessage fromJavaMailMessage(final Message jmm, final boolean withTextContent, final boolean withAttachments, final boolean stripTags, List<String> headersToFields) throws MessagingException, IOException { final IndexableMailMessage im = new IndexableMailMessage(); @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = jmm.getAllHeaders(); final Set<IndexableHeader> headerList = new HashSet<IndexableHeader>(); while (allHeaders.hasMoreElements()) { final Header h = allHeaders.nextElement(); headerList.add(new IndexableHeader(h.getName(), h.getValue())); }// w w w . ja v a 2 s .c o m im.setHeaders(headerList.toArray(new IndexableHeader[headerList.size()])); im.setSelectedHeaders(extractHeaders(im.getHeaders(), headersToFields)); if (jmm.getFolder() instanceof POP3Folder) { im.setPopId(((POP3Folder) jmm.getFolder()).getUID(jmm)); im.setMailboxType("POP"); } else { im.setMailboxType("IMAP"); } if (jmm.getFolder() instanceof UIDFolder) { im.setUid(((UIDFolder) jmm.getFolder()).getUID(jmm)); } im.setFolderFullName(jmm.getFolder().getFullName()); im.setFolderUri(jmm.getFolder().getURLName().toString()); im.setContentType(jmm.getContentType()); im.setSubject(jmm.getSubject()); im.setSize(jmm.getSize()); im.setSentDate(jmm.getSentDate()); if (jmm.getReceivedDate() != null) { im.setReceivedDate(jmm.getReceivedDate()); } if (jmm.getFrom() != null && jmm.getFrom().length > 0) { im.setFrom(Address.fromJavaMailAddress(jmm.getFrom()[0])); } if (jmm.getRecipients(RecipientType.TO) != null) { im.setTo(Address.fromJavaMailAddress(jmm.getRecipients(RecipientType.TO))); } if (jmm.getRecipients(RecipientType.CC) != null) { im.setCc(Address.fromJavaMailAddress(jmm.getRecipients(RecipientType.CC))); } if (jmm.getRecipients(RecipientType.BCC) != null) { im.setBcc(Address.fromJavaMailAddress(jmm.getRecipients(RecipientType.BCC))); } if (withTextContent) { // try { String textContent = getText(jmm, 0); if (stripTags) { textContent = stripTags(textContent); } im.setTextContent(textContent); // } catch (final Exception e) { // logger.error("Unable to retrieve text content for message {} due to {}", // e, ((MimeMessage) jmm).getMessageID(), e); // } } if (withAttachments) { try { final Object content = jmm.getContent(); // look for attachments if (jmm.isMimeType("multipart/*") && content instanceof Multipart) { List<ESAttachment> attachments = new ArrayList<ESAttachment>(); final Multipart multipart = (Multipart) content; for (int i = 0; i < multipart.getCount(); i++) { final BodyPart bodyPart = multipart.getBodyPart(i); if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) && !StringUtils.isNotBlank(bodyPart.getFileName())) { continue; // dealing with attachments only } final InputStream is = bodyPart.getInputStream(); final byte[] bytes = IOUtils.toByteArray(is); IOUtils.closeQuietly(is); attachments.add(new ESAttachment(bodyPart.getContentType(), bytes, bodyPart.getFileName())); } if (!attachments.isEmpty()) { im.setAttachments(attachments.toArray(new ESAttachment[attachments.size()])); im.setAttachmentCount(im.getAttachments().length); attachments.clear(); attachments = null; } } } catch (final Exception e) { logger.error( "Error indexing attachments (message will be indexed but without attachments) due to {}", e, e.toString()); } } im.setFlags(IMAPUtils.toStringArray(jmm.getFlags())); im.setFlaghashcode(jmm.getFlags().hashCode()); return im; }
From source file:de.saly.elasticsearch.importer.imap.support.IndexableMailMessage.java
public static IndexableMailMessage fromJavaMailMessage(final Message jmm, final boolean withTextContent, final boolean withHtmlContent, final boolean preferHtmlContent, final boolean withAttachments, final boolean stripTags, List<String> headersToFields) throws MessagingException, IOException { final IndexableMailMessage im = new IndexableMailMessage(); @SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = jmm.getAllHeaders(); final Set<IndexableHeader> headerList = new HashSet<IndexableHeader>(); while (allHeaders.hasMoreElements()) { final Header h = allHeaders.nextElement(); headerList.add(new IndexableHeader(h.getName(), h.getValue())); }/*from w w w . j a va2 s .c o m*/ im.setHeaders(headerList.toArray(new IndexableHeader[headerList.size()])); im.setSelectedHeaders(extractHeaders(im.getHeaders(), headersToFields)); if (jmm.getFolder() instanceof POP3Folder) { im.setPopId(((POP3Folder) jmm.getFolder()).getUID(jmm)); im.setMailboxType("POP"); } else { im.setMailboxType("IMAP"); } if (jmm.getFolder() instanceof UIDFolder) { im.setUid(((UIDFolder) jmm.getFolder()).getUID(jmm)); } im.setFolderFullName(jmm.getFolder().getFullName()); im.setFolderUri(jmm.getFolder().getURLName().toString()); im.setContentType(jmm.getContentType()); im.setSubject(jmm.getSubject()); im.setSize(jmm.getSize()); im.setSentDate(jmm.getSentDate()); if (jmm.getReceivedDate() != null) { im.setReceivedDate(jmm.getReceivedDate()); } if (jmm.getFrom() != null && jmm.getFrom().length > 0) { im.setFrom(Address.fromJavaMailAddress(jmm.getFrom()[0])); } if (jmm.getRecipients(RecipientType.TO) != null) { im.setTo(Address.fromJavaMailAddress(jmm.getRecipients(RecipientType.TO))); } if (jmm.getRecipients(RecipientType.CC) != null) { im.setCc(Address.fromJavaMailAddress(jmm.getRecipients(RecipientType.CC))); } if (jmm.getRecipients(RecipientType.BCC) != null) { im.setBcc(Address.fromJavaMailAddress(jmm.getRecipients(RecipientType.BCC))); } if (withTextContent) { // try { String textContent = getText(jmm, 0, preferHtmlContent); if (stripTags) { textContent = stripTags(textContent); } im.setTextContent(textContent); // } catch (final Exception e) { // logger.error("Unable to retrieve text content for message {} due to {}", // e, ((MimeMessage) jmm).getMessageID(), e); // } } if (withHtmlContent) { // try { String htmlContent = getText(jmm, 0, true); im.setHtmlContent(htmlContent); // } catch (final Exception e) { // logger.error("Unable to retrieve text content for message {} due to {}", // e, ((MimeMessage) jmm).getMessageID(), e); // } } if (withAttachments) { try { final Object content = jmm.getContent(); // look for attachments if (jmm.isMimeType("multipart/*") && content instanceof Multipart) { List<ESAttachment> attachments = new ArrayList<ESAttachment>(); final Multipart multipart = (Multipart) content; for (int i = 0; i < multipart.getCount(); i++) { final BodyPart bodyPart = multipart.getBodyPart(i); if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) && !StringUtils.isNotBlank(bodyPart.getFileName())) { continue; // dealing with attachments only } final InputStream is = bodyPart.getInputStream(); final byte[] bytes = IOUtils.toByteArray(is); IOUtils.closeQuietly(is); attachments.add(new ESAttachment(bodyPart.getContentType(), bytes, bodyPart.getFileName())); } if (!attachments.isEmpty()) { im.setAttachments(attachments.toArray(new ESAttachment[attachments.size()])); im.setAttachmentCount(im.getAttachments().length); attachments.clear(); attachments = null; } } } catch (final Exception e) { logger.error( "Error indexing attachments (message will be indexed but without attachments) due to {}", e, e.toString()); } } im.setFlags(IMAPUtils.toStringArray(jmm.getFlags())); im.setFlaghashcode(jmm.getFlags().hashCode()); return im; }
From source file:com.adaptris.core.mail.MetadataMailHeaders.java
@Override public void handle(MimeMessage mime, AdaptrisMessage msg) throws MessagingException { Set<MetadataElement> metadata = new HashSet<MetadataElement>(); Enumeration headers = mime.getAllHeaders(); String pfx = StringUtils.defaultIfBlank(getPrefix(), ""); while (headers.hasMoreElements()) { Header h = (Header) headers.nextElement(); metadata.add(new MetadataElement(pfx + h.getName(), h.getValue())); }// w ww . j a v a2 s .c om msg.setMetadata(filter().filter(metadata).toSet()); }