List of usage examples for javax.mail Message getSubject
public abstract String getSubject() throws MessagingException;
From source file:com.jlgranda.fede.ejb.mail.reader.FacturaElectronicaMailReader.java
public static void main(String[] args) throws MessagingException, IOException, Exception { FacturaElectronicaMailReader famr = new FacturaElectronicaMailReader(); String server = "jlgranda.com"; String username = "1104499049@jlgranda.com"; String password = "FKR5oznrtVwnEirkrbl4rmeba0mFCmYh"; String proto = "TLS"; IMAPClient client = new IMAPClient(server, username, password); String contentType = null;/*from w w w .j a v a 2 s . c om*/ StringReader reader = null; Address[] fromAddress = null; String messageContent = ""; String attachFiles = ""; org.apache.james.mime4j.dom.Multipart multiPart = null; MimeBodyPart part = null; int i = 0; String partContentType = null; String partName = null; String from = ""; String subject = ""; String sentDate = ""; int contadorFacturasLeidas = 0; List<FacturaReader> result = new ArrayList<>(); //Guardar enlaces a factura si es el caso boolean facturaEncontrada = false; int index = 0; int numberOfParts = 0; String[] token = null; MessageBuilder builder = new DefaultMessageBuilder(); ByteArrayOutputStream os = null; EmailHelper emailHelper = new EmailHelper(); for (Message message : client.getMessages("inbox", false)) { //System.out.println("Message #" + email.fullMail(message) + ":"); //attachFiles = ""; fromAddress = message.getFrom(); from = fromAddress[0].toString(); subject = message.getSubject(); sentDate = message.getSentDate() != null ? message.getSentDate().toString() : ""; if (subject.contains( "Fwd: Ghost - Doc. Electrnico: 2201201601180145025300120010320000336391234567819")) { System.out.println("--------------------------------------" + (index++) + "-----------------------------------------"); System.out.println("From: " + fromAddress); System.out.println("Subject: " + subject); try { org.apache.james.mime4j.dom.Message mime4jMessage = builder .parseMessage(new ByteArrayInputStream(emailHelper.fullMail(message).getBytes())); result.addAll(famr.handleMessage(mime4jMessage)); } catch (org.apache.james.mime4j.MimeIOException mioe) { mioe.printStackTrace(); } catch (org.apache.james.mime4j.MimeException me) { me.printStackTrace(); } System.out .println("-------------------------------------------------------------------------------"); } } System.err.println("Facturas encontradas>> " + result.size()); client.close(); }
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 w w w .jav a 2 s. co m 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.wso2.esb.integration.common.utils.servers.GreenMailServer.java
/** * Check whether email received by reading the emails. * * @param protocol to connect to the store * @param user whose mail store should be connected * @param subject the subject of the mail to search * @return// w w w.j a v a 2 s . c o m * @throws MessagingException when unable to connect to the store */ public static boolean isMailReceived(String protocol, GreenMailUser user, String subject) throws MessagingException { Store store = getConnection(user, protocol); Folder folder = store.getFolder(EMAIL_INBOX); folder.open(Folder.READ_ONLY); boolean isReceived = false; Message[] messages = folder.getMessages(); for (Message message : messages) { if (message.getSubject().contains(subject)) { log.info("Found the Email with Subject : " + subject); isReceived = true; break; } } return isReceived; }
From source file:org.wso2.esb.integration.common.utils.servers.GreenMailServer.java
/** * Check mail folder for an email using subject. * * @param emailSubject Email subject/*from w ww . ja v a 2 s . c o m*/ * @param folder mail folder to check for an email * @param protocol protocol used to connect to the server * @return whether mail received or not * @throws MessagingException if we're unable to connect to the store */ private static boolean isMailReceivedBySubject(String emailSubject, String folder, String protocol, GreenMailUser user) throws MessagingException { boolean emailReceived = false; Folder mailFolder; Store store = getConnection(user, protocol); try { mailFolder = store.getFolder(folder); mailFolder.open(Folder.READ_WRITE); SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject)); Message[] messages = mailFolder.search(searchTerm); for (Message message : messages) { if (message.getSubject().contains(emailSubject)) { log.info("Found the Email with Subject : " + emailSubject); emailReceived = true; break; } } } finally { if (store != null) { store.close(); } } return emailReceived; }
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); }// w w w . j a va 2s . c om } } Thread.sleep(MAIL_WAIT_TIME); } return null; }
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. */// w ww .ja va2 s . co m 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.preferences.InImapUserPreferencesStorage.java
/** * Opens the IMAP folder, deletes all messages which match the magic subject and * creates a new message with an attachment which contains the object serialized *//*www . ja v a2 s . co m*/ protected static void saveUserPreferencesInIMAP(Log logger, User user, Session session, IMAPStore iStore, String folderName, String subject, Object object) throws MessagingException, IOException, InterruptedException { IMAPFolder folder = (IMAPFolder) iStore.getFolder(folderName); if (folder.exists() || folder.create(IMAPFolder.HOLDS_MESSAGES)) { if (!folder.isOpen()) { folder.open(Folder.READ_WRITE); } // Serialize the object ByteArrayOutputStream fout = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(object); oos.close(); ByteArrayInputStream is = new ByteArrayInputStream(fout.toByteArray()); // Create a new message with an attachment which has the serialized object MimeMessage message = new MimeMessage(session); message.setSubject(subject); Multipart multipart = new MimeMultipart(); MimeBodyPart txtPart = new MimeBodyPart(); txtPart.setContent("This message contains configuration used by Hupa, do not delete it", "text/plain"); multipart.addBodyPart(txtPart); FileItem item = createPreferencesFileItem(is, subject, HUPA_DATA_MIME_TYPE); multipart.addBodyPart(MessageUtils.fileitemToBodypart(item)); message.setContent(multipart); message.saveChanges(); // It seems it's not possible to modify the content of an existing message using the API // So I delete the previous message storing the preferences and I create a new one Message[] msgs = folder.getMessages(); for (Message msg : msgs) { if (subject.equals(msg.getSubject())) { msg.setFlag(Flag.DELETED, true); } } // It is necessary to copy the message before saving it (the same problem in AbstractSendMessageHandler) message = new MimeMessage((MimeMessage) message); message.setFlag(Flag.SEEN, true); folder.appendMessages(new Message[] { message }); folder.close(true); logger.info("Saved preferences " + subject + " in imap folder " + folderName + " for user " + user); } else { logger.error("Unable to save preferences " + subject + " in imap folder " + folderName + " for user " + user); } }
From source file:com.cubusmail.gwtui.server.services.ConvertUtil.java
/** * @param msg/* w w w. jav a 2 s . c om*/ * @return * @throws MessagingException */ public static GWTMessage convert(Message msg) throws MessagingException { GWTMessage result = new GWTMessage(); result.setId(msg.getMessageNumber()); result.setSubject(msg.getSubject()); result.setFrom(MessageUtils.getMailAdressString(msg.getFrom(), AddressStringType.COMPLETE)); result.setTo(MessageUtils.getMailAdressString(msg.getRecipients(Message.RecipientType.TO), AddressStringType.COMPLETE)); result.setCc(MessageUtils.getMailAdressString(msg.getRecipients(Message.RecipientType.CC), AddressStringType.COMPLETE)); result.setDate(msg.getSentDate()); result.setSize(MessageUtils.calculateAttachmentSize(msg.getSize())); return result; }
From source file:org.wso2.esb.integration.common.utils.MailToTransportUtil.java
/** * Check a particular email has received to a given email folder by email subject. * * @param emailSubject - Email emailSubject to find email is in inbox or not * @return - found the email or not/*from w ww . j a v a 2 s . c om*/ * @throws ESBMailTransportIntegrationTestException - Is thrown if an error occurred while reading the emails */ public static boolean isMailReceivedBySubject(String emailSubject, String folder) throws ESBMailTransportIntegrationTestException { boolean emailReceived = false; Folder mailFolder; Store store = getConnection(); try { mailFolder = store.getFolder(folder); mailFolder.open(Folder.READ_WRITE); SearchTerm searchTerm = new AndTerm(new SubjectTerm(emailSubject), new BodyTerm(emailSubject)); Message[] messages = mailFolder.search(searchTerm); for (Message message : messages) { if (message.getSubject().contains(emailSubject)) { log.info("Found the email emailSubject : " + emailSubject); emailReceived = true; break; } } return emailReceived; } catch (MessagingException ex) { log.error("Error when getting mail count ", ex); throw new ESBMailTransportIntegrationTestException("Error when getting mail count ", ex); } finally { if (store != null) { try { store.close(); } catch (MessagingException e) { log.warn("Error when closing the store ", e); } } } }
From source file:com.glaf.mail.util.MailUtils.java
public static String getSubject(Message message) { String mailSubject = ""; try {/* w w w .ja v a2 s .c o m*/ mailSubject = message.getSubject(); mailSubject = convertString(message.getSubject()); } catch (Exception ex) { ex.printStackTrace(); } return mailSubject; }