List of usage examples for javax.mail Message getMessageNumber
public int getMessageNumber()
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 {/*ww w . j a v 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:Main.java
public static List<Message> getMessagesWithPage(Folder folder, int pageNum, int pageSize) throws MessagingException { Message[] messages = folder.getMessages(); Arrays.sort(messages, new Comparator<Message>() { public int compare(Message o1, Message o2) { Message m1 = (Message) o1; Message m2 = (Message) o2;/*from ww w.ja v a 2s .c o m*/ if (m1.getMessageNumber() > m2.getMessageNumber()) { return -1; } return 1; } }); List<Message> list = Arrays.asList(messages); int start = (pageNum - 1) * pageSize; int end = pageNum * pageSize; return list.subList(start, end); }
From source file:com.cubusmail.gwtui.server.services.ConvertUtil.java
/** * @param msg// w w w.j a v a2 s . com * @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:ru.codemine.ccms.mail.EmailAttachmentExtractor.java
public void saveAllAttachment() { String protocol = ssl ? "imaps" : "imap"; Properties props = new Properties(); props.put("mail.store.protocol", protocol); try {//from w w w. java 2 s . com Session session = Session.getInstance(props); Store store = session.getStore(); store.connect(host, port, username, password); Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_WRITE); for (Message msg : inbox.getMessages()) { if (!msg.isSet(Flags.Flag.SEEN)) { String fileNamePrefix = settingsService.getStorageEmailPath() + "msg-" + msg.getMessageNumber(); Multipart multipart = (Multipart) msg.getContent(); for (int i = 0; i < multipart.getCount(); i++) { BodyPart bodyPart = multipart.getBodyPart(i); if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) && StringUtils.isNotEmpty(bodyPart.getFileName())) { MimeBodyPart mimimi = (MimeBodyPart) bodyPart; // =(^.^)= mimimi.saveFile(fileNamePrefix + MimeUtility.decodeText(bodyPart.getFileName())); msg.setFlag(Flags.Flag.SEEN, true); } } } } } catch (IOException | MessagingException e) { log.error("? ? ?, : " + e.getMessage()); } }
From source file:com.jaeksoft.searchlib.crawler.mailbox.crawler.MailboxAbstractCrawler.java
final public void readMessage(IndexDocument crawlIndexDocument, IndexDocument parserIndexDocument, Folder folder, Message message, String id) throws Exception { crawlIndexDocument.addString(MailboxFieldEnum.message_id.name(), id); crawlIndexDocument.addString(MailboxFieldEnum.message_number.name(), Integer.toString(message.getMessageNumber())); if (message instanceof MimeMessage) crawlIndexDocument.addString(MailboxFieldEnum.content_id.name(), ((MimeMessage) message).getContentID()); crawlIndexDocument.addString(MailboxFieldEnum.subject.name(), message.getSubject()); putAddresses(crawlIndexDocument, message.getFrom(), MailboxFieldEnum.from_address.name(), MailboxFieldEnum.from_personal.name()); putAddresses(crawlIndexDocument, message.getReplyTo(), MailboxFieldEnum.reply_to_address.name(), MailboxFieldEnum.reply_to_personal.name()); putAddresses(crawlIndexDocument, message.getRecipients(RecipientType.TO), MailboxFieldEnum.recipient_to_address.name(), MailboxFieldEnum.recipient_to_personal.name()); putAddresses(crawlIndexDocument, message.getRecipients(RecipientType.CC), MailboxFieldEnum.recipient_cc_address.name(), MailboxFieldEnum.recipient_cc_personal.name()); putAddresses(crawlIndexDocument, message.getRecipients(RecipientType.BCC), MailboxFieldEnum.recipient_bcc_address.name(), MailboxFieldEnum.recipient_bcc_personal.name()); Date dt = message.getSentDate(); if (dt != null) crawlIndexDocument.addString(MailboxFieldEnum.send_date.name(), dt.toString()); dt = message.getReceivedDate();/* w w w . ja v a2s. c o m*/ if (dt != null) crawlIndexDocument.addString(MailboxFieldEnum.received_date.name(), dt.toString()); if (message.isSet(Flag.ANSWERED)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "ANSWERED"); if (message.isSet(Flag.DELETED)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "DELETED"); if (message.isSet(Flag.DRAFT)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "DRAFT"); if (message.isSet(Flag.FLAGGED)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "FLAGGED"); if (message.isSet(Flag.SEEN)) crawlIndexDocument.addString(MailboxFieldEnum.flags.name(), "SEEN"); if (message instanceof MimeMessage) { MimeMessageParser mimeMessageParser = new MimeMessageParser((MimeMessage) message).parse(); crawlIndexDocument.addString(MailboxFieldEnum.html_content.name(), mimeMessageParser.getHtmlContent()); crawlIndexDocument.addString(MailboxFieldEnum.plain_content.name(), mimeMessageParser.getPlainContent()); for (DataSource dataSource : mimeMessageParser.getAttachmentList()) { crawlIndexDocument.addString(MailboxFieldEnum.email_attachment_name.name(), dataSource.getName()); crawlIndexDocument.addString(MailboxFieldEnum.email_attachment_type.name(), dataSource.getContentType()); if (parserSelector == null) continue; Parser attachParser = parserSelector.parseStream(null, dataSource.getName(), dataSource.getContentType(), null, dataSource.getInputStream(), null, null, null); if (attachParser == null) continue; List<ParserResultItem> parserResults = attachParser.getParserResults(); if (parserResults != null) for (ParserResultItem parserResult : parserResults) crawlIndexDocument.addFieldIndexDocument(MailboxFieldEnum.email_attachment_content.name(), parserResult.getParserDocument()); } } }
From source file:de.saly.elasticsearch.imap.AbstractIMAPRiverUnitTest.java
protected void deleteMailsFromUserMailbox(final Properties props, final String folderName, final int start, final int deleteCount, final String user, final String password) throws MessagingException { final Store store = Session.getInstance(props).getStore(); store.connect(user, password);/*from ww w . j av a 2s .c om*/ checkStoreForTestConnection(store); final Folder f = store.getFolder(folderName); f.open(Folder.READ_WRITE); final int msgCount = f.getMessageCount(); final Message[] m = deleteCount == -1 ? f.getMessages() : f.getMessages(start, Math.min(msgCount, deleteCount + start - 1)); int d = 0; for (final Message message : m) { message.setFlag(Flag.DELETED, true); logger.info("Delete msgnum: {} with sid {}", message.getMessageNumber(), message.getSubject()); d++; } f.close(true); logger.info("Deleted " + d + " messages"); store.close(); }
From source file:com.intranet.intr.inbox.EmpControllerInbox.java
public correoNoLeidos ltaRecibidos2(String name, int num) { correoNoLeidos lta = new correoNoLeidos(); Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); try {//from w w w .j av a2 s.c o m users u = usuarioService.getByLogin(name); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.1and1.es", u.getCorreoUsuario(), u.getCorreoContreasenna()); System.out.println("ola" + store); Folder inbox = store.getFolder("Inbox"); inbox.open(Folder.READ_ONLY); Calendar fecha3 = Calendar.getInstance(); fecha3.roll(Calendar.MONTH, false); Message msg[] = inbox.search(new ReceivedDateTerm(ComparisonTerm.GT, fecha3.getTime())); //Message msg[] = inbox.getMessages(); System.out.println("MAILS: " + msg.length); for (Message message : msg) { try { if (num == message.getMessageNumber()) { //message.setFlag(Flags.Flag.SEEN, true); lta.setNum(message.getMessageNumber()); lta.setFecha(message.getSentDate().toString()); lta.setDe(message.getFrom()[0].toString()); lta.setAsunto(message.getSubject().toString()); correoNoLeidos co = new correoNoLeidos(); List<String> arc = new ArrayList<String>(); List<String> rar = new ArrayList<String>(); correoNoLeidos cc = analizaParteDeMensaje2(co, arc, rar, message); lta.setImagenes(cc.getImagenes()); lta.setRar(cc.getRar()); lta.setContenido(cc.getContenido()); String cont = ""; String contenido = ""; //lta.setContenido(analizaParteDeMensaje2(message)); } } catch (Exception e) { System.out.println("No Information"); } } } catch (MessagingException e) { System.out.println(e.toString()); } return lta; }
From source file:com.liferay.mail.imap.IMAPAccessor.java
public boolean hasNewMessages(long folderId) throws PortalException { Folder jxFolder = null;// w ww .j a v a 2 s. c o m try { jxFolder = openFolder(folderId); int messageCount = jxFolder.getMessageCount(); Message jxMessage = getMessage(folderId, jxFolder, false); if (jxMessage == null) { if (messageCount <= 0) { return false; } return true; } if (messageCount == jxMessage.getMessageNumber()) { return false; } return true; } catch (MessagingException me) { throw new MailException(me); } finally { closeFolder(jxFolder, false); } }
From source file:com.naryx.tagfusion.cfm.mail.cfPOP3.java
private void populateMessage(cfSession _Session, Message thisMessage, cfQueryResultData popData, boolean GetAll, File attachmentDir, Folder _parent) throws Exception { popData.addRow(1);/*w ww .j av a2s . com*/ int Row = popData.getNoRows(); Date date = thisMessage.getSentDate(); if (date != null) { cfDateData cfdate = new cfDateData(date); cfdate.setPOPDate(); popData.setCell(Row, 1, cfdate); } else { popData.setCell(Row, 1, new cfStringData("")); } popData.setCell(Row, 2, new cfStringData(formatAddress(thisMessage.getFrom()))); popData.setCell(Row, 3, new cfNumberData(thisMessage.getMessageNumber())); popData.setCell(Row, 4, new cfStringData(formatAddress(thisMessage.getReplyTo()))); popData.setCell(Row, 5, new cfStringData(thisMessage.getSubject())); popData.setCell(Row, 6, new cfStringData(formatAddress(thisMessage.getRecipients(Message.RecipientType.CC)))); popData.setCell(Row, 7, new cfStringData(formatAddress(thisMessage.getRecipients(Message.RecipientType.TO)))); String[] msgid = thisMessage.getHeader("Message-ID"); popData.setCell(Row, 8, new cfStringData(msgid != null ? msgid[0] : "")); popData.setCell(Row, 9, new cfStringData(getMessageUID(_parent, thisMessage))); popData.setCell(Row, 10, new cfStringData(formatHeader(thisMessage))); if (GetAll) { retrieveBody(_Session, thisMessage, popData, Row, attachmentDir); } }
From source file:com.liferay.mail.imap.IMAPAccessor.java
public void storeEnvelopes(long folderId, boolean allMessages) throws PortalException { Folder jxFolder = null;/* ww w .j a va2s . co m*/ try { com.liferay.mail.model.Folder folder = FolderLocalServiceUtil.fetchFolder(folderId); if (folder == null) { return; } jxFolder = openFolder(folderId); int messageCount = jxFolder.getMessageCount(); FolderLocalServiceUtil.updateFolder(folderId, folder.getFullName(), folder.getDisplayName(), messageCount); if (messageCount == 0) { return; } Message oldestJxMessage = getMessage(folderId, jxFolder, true); Message newestJxMessage = getMessage(folderId, jxFolder, false); Message[] jxMessages = new Message[0]; if (allMessages) { if (_log.isDebugEnabled()) { _log.debug("Downloading all messages from folder " + jxFolder.getFullName()); } jxMessages = jxFolder.getMessages(); } else if ((oldestJxMessage == null) && (newestJxMessage == null)) { if (_log.isDebugEnabled()) { _log.debug( "Downloading messages from folder " + jxFolder.getFullName() + " for the first time"); } int startingMessageNumber = messageCount - PortletPropsValues.MESSAGES_SYNC_COUNT; if (startingMessageNumber < 1) { startingMessageNumber = 1; } jxMessages = jxFolder.getMessages(startingMessageNumber, messageCount); } else { int oldestMessageNumber = oldestJxMessage.getMessageNumber(); int newestMessageNumber = newestJxMessage.getMessageNumber(); if (newestMessageNumber != messageCount) { if (_log.isDebugEnabled()) { _log.debug("Downloading new messages from folder " + jxFolder.getFullName()); } jxMessages = jxFolder.getMessages(newestMessageNumber + 1, messageCount); } else if (oldestMessageNumber != 1) { if (_log.isDebugEnabled()) { _log.debug("Downloading old messages from folder " + jxFolder.getFullName()); } int startingMessageNumber = oldestMessageNumber - PortletPropsValues.MESSAGES_SYNC_COUNT; if (startingMessageNumber < 1) { startingMessageNumber = 1; } jxMessages = jxFolder.getMessages(startingMessageNumber, oldestMessageNumber - 1); } } storeEnvelopes(folderId, jxFolder, jxMessages); } catch (MessagingException me) { throw new MailException(me); } finally { closeFolder(jxFolder, false); } }