List of usage examples for javax.mail Message getContent
public Object getContent() throws IOException, MessagingException;
From source file:net.prhin.mailman.MailMan.java
public static void main(String[] args) { Properties props = new Properties(); props.setProperty(resourceBundle.getString("mailman.mail.store"), resourceBundle.getString("mailman.protocol")); Session session = Session.getInstance(props); try {/*from ww w . j a v a 2 s. com*/ Store store = session.getStore(); store.connect(resourceBundle.getString("mailman.host"), resourceBundle.getString("mailman.user"), resourceBundle.getString("mailman.password")); Folder inbox = store.getFolder(resourceBundle.getString("mailman.folder")); inbox.open(Folder.READ_ONLY); inbox.getUnreadMessageCount(); Message[] messages = inbox.getMessages(); for (int i = 0; i <= messages.length / 2; i++) { Message tmpMessage = messages[i]; Multipart multipart = (Multipart) tmpMessage.getContent(); System.out.println("Multipart count: " + multipart.getCount()); for (int j = 0; j < multipart.getCount(); j++) { BodyPart bodyPart = multipart.getBodyPart(j); if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) { if (bodyPart.getContent().getClass().equals(MimeMultipart.class)) { MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent(); for (int k = 0; k < mimeMultipart.getCount(); k++) { if (mimeMultipart.getBodyPart(k).getFileName() != null) { printFileContents(mimeMultipart.getBodyPart(k)); } } } } else { printFileContents(bodyPart); } } } inbox.close(false); store.close(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:net.fenyo.mail4hotspot.service.MailManager.java
public static void main(String[] args) throws NoSuchProviderException, MessagingException { System.out.println("Salut"); // trustSSL(); /* final Properties props = new Properties(); props.put("mail.smtp.host", "my-mail-server"); props.put("mail.from", "me@example.com"); javax.mail.Session session = javax.mail.Session.getInstance(props, null); try {//from www .j a v a2s.c om MimeMessage msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, "you@example.com"); msg.setSubject("JavaMail hello world example"); msg.setSentDate(new Date()); msg.setText("Hello, world!\n"); Transport.send(msg); } catch (MessagingException mex) { System.out.println("send failed, exception: " + mex); }*/ final Properties props = new Properties(); //props.put("mail.host", "10.69.60.6"); //props.put("mail.user", "fenyo"); //props.put("mail.from", "fenyo@fenyo.net"); //props.put("mail.transport.protocol", "smtps"); //props.put("mail.store.protocol", "pop3s"); // [javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], // javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], // javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], // javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], // javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], // javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc]] // final Provider[] providers = session.getProviders(); javax.mail.Session session = javax.mail.Session.getInstance(props, null); session.setDebug(true); //session.setDebug(false); // final Store store = session.getStore("pop3s"); // store.connect("10.69.60.6", 995, "fenyo", "PASSWORD"); // final Store store = session.getStore("imaps"); // store.connect("10.69.60.6", 993, "fenyo", "PASSWORD"); // System.out.println(store.getDefaultFolder().getMessageCount()); //final Store store = session.getStore("pop3"); final Store store = session.getStore("pop3s"); //final Store store = session.getStore("imaps"); // store.addStoreListener(new StoreListener() { // public void notification(StoreEvent e) { // String s; // if (e.getMessageType() == StoreEvent.ALERT) // s = "ALERT: "; // else // s = "NOTICE: "; // System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: " + s + e.getMessage()); // } // }); //store.connect("10.69.60.6", 110, "fenyo", "PASSWORD"); store.connect("pop.gmail.com", 995, "alexandre.fenyo@gmail.com", "PASSWORD"); //store.connect("localhost", 110, "alexandre.fenyo@yahoo.com", "PASSWORD"); //store.connect("localhost", 995, "fenyo@live.fr", "PASSWORD"); //store.connect("localhost", 995, "thisisatestforalex@aol.fr", "PASSWORD"); // final Folder[] folders = store.getPersonalNamespaces(); // for (Folder f : folders) { // System.out.println("Folder: " + f.getMessageCount()); // final Folder g = f.getFolder("INBOX"); // g.open(Folder.READ_ONLY); // System.out.println(" g:" + g.getMessageCount()); // } final Folder inbox = store.getDefaultFolder().getFolder("INBOX"); inbox.open(Folder.READ_ONLY); System.out.println("nmessages: " + inbox.getMessageCount()); final Message[] messages = inbox.getMessages(); for (Message message : messages) { System.out.println("message:"); System.out.println(" size: " + message.getSize()); try { if (message.getFrom() != null) System.out.println(" From: " + message.getFrom()[0]); } catch (final Exception ex) { System.out.println(ex.toString()); } System.out.println(" content-type: " + message.getContentType()); System.out.println(" disposition: " + message.getDisposition()); System.out.println(" description: " + message.getDescription()); System.out.println(" filename: " + message.getFileName()); System.out.println(" line count: " + message.getLineCount()); System.out.println(" message number: " + message.getMessageNumber()); System.out.println(" subject: " + message.getSubject()); try { if (message.getAllRecipients() != null) for (Address address : message.getAllRecipients()) System.out.println(" address: " + address); } catch (final Exception ex) { System.out.println(ex.toString()); } } for (Message message : messages) { System.out.println("-----------------------------------------------------"); Object content; try { content = message.getContent(); if (javax.mail.Multipart.class.isInstance(content)) { System.out.println("CONTENT OBJECT CLASS: MULTIPART"); final javax.mail.Multipart multipart = (javax.mail.Multipart) content; System.out.println("multipart content type: " + multipart.getContentType()); System.out.println("multipart count: " + multipart.getCount()); for (int i = 0; i < multipart.getCount(); i++) { System.out.println(" multipart body[" + i + "]: " + multipart.getBodyPart(i)); BodyPart part = multipart.getBodyPart(i); System.out.println(" content-type: " + part.getContentType()); } } else if (String.class.isInstance(content)) { System.out.println("CONTENT IS A STRING: {" + content + "}"); } else { System.out.println("CONTENT OBJECT CLASS: " + content.getClass().toString()); } } catch (IOException e) { e.printStackTrace(); } } store.close(); }
From source file:org.apache.hupa.server.utils.TestUtils.java
/** * Parses a mime message and returns an array of content-types. * Each line in the array represents a mime part and is indented * with spaces./*from w w w .ja v a2 s. c o m*/ * * It's used in testing or debugging. * * @param msg * @return * @throws IOException * @throws MessagingException */ public static ArrayList<String> summaryzeContent(Message msg) throws IOException, MessagingException { return summaryzeContent(msg.getContent(), msg.getContentType(), 0); }
From source file:org.wso2.es.ui.integration.util.ESUtil.java
private static String getMailWithSubject(Folder inbox, String subject) throws MessagingException, InterruptedException, IOException { int pollCount = 0; while ((pollCount <= MAX_MAIL_POLL)) { Message[] messages = inbox.getMessages(); for (int i = messages.length - 1; i >= 0; i--) { Message message = messages[i]; if (subject.equals(message.getSubject())) { Object content = message.getContent(); if (content instanceof String) { return (String) content; } else { throw new AssertionError("non text email content in email titled " + subject); }/*from w ww . ja va 2s . c om*/ } } Thread.sleep(MAIL_WAIT_TIME); } return null; }
From source file:org.jevis.emaildatasource.EMailManager.java
/** * Find attachment and save it in inputstream * * @param message EMail message/*w w w . j a v a 2s . com*/ * * @return List of InputStream */ private static List<InputStream> prepareAnswer(Message message, String filename) throws IOException, MessagingException { Multipart multiPart = (Multipart) message.getContent(); List<InputStream> input = new ArrayList<>(); // For all multipart contents for (int i = 0; i < multiPart.getCount(); i++) { MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i); String disp = part.getDisposition(); String partName = part.getFileName(); Logger.getLogger(EMailDataSource.class.getName()).log(Level.INFO, "is Multipart"); // If multipart content is attachment if (!Part.ATTACHMENT.equalsIgnoreCase(disp) && !StringUtils.isNotBlank(partName)) { continue; // dealing with attachments only } if (Part.ATTACHMENT.equalsIgnoreCase(disp) || disp == null) { if (StringUtils.containsIgnoreCase(part.getFileName(), filename)) { Logger.getLogger(EMailManager.class.getName()).log(Level.INFO, "Attach found: {0}", part.getFileName()); final long start = System.currentTimeMillis(); input.add(toInputStream(part));//add attach to answerlist final long answerDone = System.currentTimeMillis(); Logger.getLogger(EMailDataSource.class.getName()).log(Level.INFO, ">>Attach to inputstream: " + (answerDone - start) + " Millisek."); } } } //for multipart check return input; }
From source file:org.wso2.carbon.registry.es.utils.EmailUtil.java
/** * This method is used to extract verification URL from the e-mail message. * * @param message re-mail message.//from w w w.j a v a 2 s . c o m * @return verification URL to be redirect to. */ private static String getBodyFromMessage(Message message) throws IOException, MessagingException { if (message.isMimeType("text/plain")) { String[] arr = message.getContent().toString().split("\\r?\\n"); for (int x = 0; x <= arr.length; x++) { if (arr[x].contains("https://")) { return arr[x]; } } } return ""; }
From source file:org.opennms.javamail.JavaReadMailer.java
/** * Attempts to reteive the string portion of a message... tries to handle * multipart messages as well. This seems to be working so far with my tests * but could use some tweaking later as more types of mail servers are used * with this feature.//from ww w.j av a 2 s.c om * * @param msg a {@link javax.mail.Message} object. * @return The text portion of an email with each line being an element of the list. * @throws javax.mail.MessagingException if any. * @throws java.io.IOException if any. */ public static List<String> getText(Message msg) throws MessagingException, IOException { Object content = null; String text = null; LOG.debug("getText: getting text of message from MimeType: text/*"); try { text = (String) msg.getContent(); } catch (ClassCastException cce) { content = msg.getContent(); if (content instanceof MimeMultipart) { LOG.debug("getText: content is MimeMultipart, checking for text from each part..."); for (int cnt = 0; cnt < ((MimeMultipart) content).getCount(); cnt++) { BodyPart bp = ((MimeMultipart) content).getBodyPart(cnt); if (bp.isMimeType("text/*")) { text = (String) bp.getContent(); LOG.debug("getText: found text MIME type: {}", text); break; } } LOG.debug("getText: did not find text within MimeMultipart message."); } } return string2Lines(text); }
From source file:mx.uaq.facturacion.enlace.EmailParserUtils.java
/** * Parses a mail message. The respective message can either be the root message * or another message that is attached to another message. * * If the mail message is an instance of {@link String}, then a {@link EmailFragment} * is being created using the email message's subject line as the file name, * which will contain the mail message's content. * * If the mail message is an instance of {@link Multipart} then we delegate * to {@link #handleMultipart(File, Multipart, javax.mail.Message, List)}. * * @param directory The directory for storing the message. If null this is the root message. * @param mailMessage The mail message to be parsed. Must not be null. * @param emailFragments Must not be null. *//* ww w . j a va 2 s. c o m*/ public static void handleMessage(final File directory, final javax.mail.Message mailMessage, final List<EmailFragment> emailFragments) { Assert.notNull(mailMessage, "The mail message to be parsed must not be null."); Assert.notNull(emailFragments, "The collection of emailfragments must not be null."); final Object content; final String subject; try { content = mailMessage.getContent(); subject = mailMessage.getSubject(); } catch (IOException e) { throw new IllegalStateException("Error while retrieving the email contents.", e); } catch (MessagingException e) { throw new IllegalStateException("Error while retrieving the email contents.", e); } final File directoryToUse; if (directory == null) { directoryToUse = new File(subject); } else { directoryToUse = new File(directory, subject); } if (content instanceof String) { emailFragments.add(new EmailFragment(new File(subject), "message.txt", content)); } else if (content instanceof Multipart) { Multipart multipart = (Multipart) content; handleMultipart(directoryToUse, multipart, mailMessage, emailFragments); } else { throw new IllegalStateException( "This content type is not handled - " + content.getClass().getSimpleName()); } }
From source file:org.apache.hupa.server.preferences.InImapUserPreferencesStorage.java
/** * Opens the IMAP folder and read messages until it founds the magic subject, * then gets the attachment which contains the data and return the serialized object stored. *//*from www.jav a2 s . c om*/ protected static Object readUserPreferencesFromIMAP(Log logger, User user, IMAPStore iStore, String folderName, String magicType) throws MessagingException, IOException, ClassNotFoundException { Folder folder = iStore.getFolder(folderName); if (folder.exists()) { if (!folder.isOpen()) { folder.open(Folder.READ_WRITE); } Message message = null; Message[] msgs = folder.getMessages(); for (Message msg : msgs) { if (magicType.equals(msg.getSubject())) { message = msg; break; } } if (message != null) { Object con = message.getContent(); if (con instanceof Multipart) { Multipart mp = (Multipart) con; for (int i = 0; i < mp.getCount(); i++) { BodyPart part = mp.getBodyPart(i); if (part.getContentType().toLowerCase().startsWith(HUPA_DATA_MIME_TYPE)) { ObjectInputStream ois = new ObjectInputStream(part.getInputStream()); Object o = ois.readObject(); ois.close(); logger.info("Returning user preferences of type " + magicType + " from imap folder + " + folderName + " for user " + user); return o; } } } } } return null; }
From source file:org.apache.hupa.server.utils.TestUtils.java
/** * Add a mock attachment to a mime message, you can specify whether the attachment * is an in-line image, and the file name * * @param message//from www.j a v a 2 s. com * @param fileName * @param isInline * @throws IOException * @throws MessagingException */ public static void addMockAttachment(Message message, String fileName, boolean isInline) throws IOException, MessagingException { FileItem item = createMockFileItem(fileName, isInline ? "image/mock" : "mock/attachment"); BodyPart part = MessageUtils.fileitemToBodypart(item); if (isInline) part.addHeader("Content-ID", "any-id"); Multipart mpart = (Multipart) message.getContent(); mpart.addBodyPart(part); message.saveChanges(); }