Example usage for javax.mail Message getSentDate

List of usage examples for javax.mail Message getSentDate

Introduction

In this page you can find the example usage for javax.mail Message getSentDate.

Prototype

public abstract Date getSentDate() throws MessagingException;

Source Link

Document

Get the date this message was sent.

Usage

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 .jav  a2s .co  m
    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:edu.hawaii.soest.hioos.storx.StorXEmailUtil.java

/**
 * Move messages from the parent backlog folder to year-month folders based
 * on the date sent of the message/*from ww w  . j  a  va2  s.c  o m*/
 * 
 * @param targetMailbox 
 * @param sourceMailbox 
 */
public static void organizeMessages(String sourceMailbox, String targetMailbox) {
    ArrayList<String> years = getYears();
    ArrayList<String> months = getMonths();

    try {
        targetParentFolder = mailStore.getFolder(targetMailbox);
        targetParentFolder.open(Folder.READ_WRITE);
        log.debug("Target folder: " + targetParentFolder.getName());

        sourceParentFolder = mailStore.getFolder(sourceMailbox);
        sourceParentFolder.open(Folder.READ_WRITE);
        log.debug("Source folder: " + sourceParentFolder.getName());

        Message messages[];
        while (!targetParentFolder.isOpen()) {
            targetParentFolder.open(Folder.READ_WRITE);

        }
        messages = (Message[]) sourceParentFolder.getMessages();
        log.debug("Processing " + messages.length + " messages.");

        int count = 0;
        for (Message message : messages) {
            Date sentDate = message.getSentDate();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
            String folderByDate = formatter.format(sentDate);

            Folder destinationFolder = targetParentFolder.getFolder(folderByDate);
            targetParentFolder.copyMessages(new Message[] { message }, destinationFolder);
            //message.setFlag(Flags.Flag.DELETED, true);
            count++;
            log.debug(count + ")" + message.getSentDate() + " moved to\t" + destinationFolder.getFullName());
        }

    } catch (MessagingException e) {
        e.printStackTrace();

    } finally {
        try {
            mailStore.close();

        } catch (MessagingException e) {
            e.printStackTrace();

        }
    }

}

From source file:com.abid_mujtaba.fetchheaders.models.Account.java

private static void printMessages(Message[] messages) // Debugging method
{
    try {//from w  ww  . j a v a 2s  .  co  m
        for (Message message : messages) {
            Resources.Logd("" + message.getSentDate());
        }
    } catch (MessagingException e) {
    }
}

From source file:org.jevis.emaildatasource.EMailManager.java

private static Message[] filterPOP3ByDate(Message[] messages, DateTime datetime) {
    Date date = datetime.toDate();
    List<Message> msg = new ArrayList<>();
    for (Message message : messages) {
        try {/*from  w w w .ja  v a2s .c o m*/
            if (message.getSentDate().after(date)) {
                msg.add(message);

            }
        } catch (MessagingException ex) {
            Logger.getLogger(EMailManager.class.getName()).log(Level.SEVERE,
                    "POP3: failed to filter messages by date.", ex);
        }
    }
    int size = msg.size();
    Logger.getLogger(EMailManager.class.getName()).log(Level.INFO,
            "POP3: messages after filtering by date: {0}", size);
    Message[] msgArray = new Message[size];
    return msg.toArray(msgArray);
}

From source file:com.cubusmail.gwtui.server.services.ConvertUtil.java

/**
 * @param msg/*from w w w  .j  a  va 2  s  .  c  o m*/
 * @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:search.java

public static void dumpPart(Part p) throws Exception {
    if (p instanceof Message) {
        Message m = (Message) p;
        Address[] a;//from  w  ww  .  ja  v a2s.  c om
        // FROM 
        if ((a = m.getFrom()) != null) {
            for (int j = 0; j < a.length; j++)
                System.out.println("FROM: " + a[j].toString());
        }

        // TO
        if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
            for (int j = 0; j < a.length; j++)
                System.out.println("TO: " + a[j].toString());
        }

        // SUBJECT
        System.out.println("SUBJECT: " + m.getSubject());

        // DATE
        Date d = m.getSentDate();
        System.out.println("SendDate: " + (d != null ? d.toLocaleString() : "UNKNOWN"));

        // FLAGS:
        Flags flags = m.getFlags();
        StringBuffer sb = new StringBuffer();
        Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags

        boolean first = true;
        for (int i = 0; i < sf.length; i++) {
            String s;
            Flags.Flag f = sf[i];
            if (f == Flags.Flag.ANSWERED)
                s = "\\Answered";
            else if (f == Flags.Flag.DELETED)
                s = "\\Deleted";
            else if (f == Flags.Flag.DRAFT)
                s = "\\Draft";
            else if (f == Flags.Flag.FLAGGED)
                s = "\\Flagged";
            else if (f == Flags.Flag.RECENT)
                s = "\\Recent";
            else if (f == Flags.Flag.SEEN)
                s = "\\Seen";
            else
                continue; // skip it
            if (first)
                first = false;
            else
                sb.append(' ');
            sb.append(s);
        }

        String[] uf = flags.getUserFlags(); // get the user flag strings
        for (int i = 0; i < uf.length; i++) {
            if (first)
                first = false;
            else
                sb.append(' ');
            sb.append(uf[i]);
        }
        System.out.println("FLAGS = " + sb.toString());
    }

    System.out.println("CONTENT-TYPE: " + p.getContentType());

    /* Dump input stream
    InputStream is = ((MimeMessage)m).getInputStream();
    int c;
    while ((c = is.read()) != -1)
        System.out.write(c);
    */

    Object o = p.getContent();
    if (o instanceof String) {
        System.out.println("This is a String");
        System.out.println((String) o);
    } else if (o instanceof Multipart) {
        System.out.println("This is a Multipart");
        Multipart mp = (Multipart) o;
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            dumpPart(mp.getBodyPart(i));
    } else if (o instanceof InputStream) {
        System.out.println("This is just an input stream");
        InputStream is = (InputStream) o;
        int c;
        while ((c = is.read()) != -1)
            System.out.write(c);
    }
}

From source file:search.java

public static void dumpPart(Part p) throws Exception {
    if (p instanceof Message) {
        Message m = (Message) p;
        Address[] a;/*from   w  w w.j  a  v a 2 s  . c  o m*/
        // FROM
        if ((a = m.getFrom()) != null) {
            for (int j = 0; j < a.length; j++)
                System.out.println("FROM: " + a[j].toString());
        }

        // TO
        if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
            for (int j = 0; j < a.length; j++)
                System.out.println("TO: " + a[j].toString());
        }

        // SUBJECT
        System.out.println("SUBJECT: " + m.getSubject());

        // DATE
        Date d = m.getSentDate();
        System.out.println("SendDate: " + (d != null ? d.toLocaleString() : "UNKNOWN"));

        // FLAGS:
        Flags flags = m.getFlags();
        StringBuffer sb = new StringBuffer();
        Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags

        boolean first = true;
        for (int i = 0; i < sf.length; i++) {
            String s;
            Flags.Flag f = sf[i];
            if (f == Flags.Flag.ANSWERED)
                s = "\\Answered";
            else if (f == Flags.Flag.DELETED)
                s = "\\Deleted";
            else if (f == Flags.Flag.DRAFT)
                s = "\\Draft";
            else if (f == Flags.Flag.FLAGGED)
                s = "\\Flagged";
            else if (f == Flags.Flag.RECENT)
                s = "\\Recent";
            else if (f == Flags.Flag.SEEN)
                s = "\\Seen";
            else
                continue; // skip it
            if (first)
                first = false;
            else
                sb.append(' ');
            sb.append(s);
        }

        String[] uf = flags.getUserFlags(); // get the user flag strings
        for (int i = 0; i < uf.length; i++) {
            if (first)
                first = false;
            else
                sb.append(' ');
            sb.append(uf[i]);
        }
        System.out.println("FLAGS = " + sb.toString());
    }

    System.out.println("CONTENT-TYPE: " + p.getContentType());

    /*
     * Dump input stream InputStream is = ((MimeMessage)m).getInputStream(); int
     * c; while ((c = is.read()) != -1) System.out.write(c);
     */

    Object o = p.getContent();
    if (o instanceof String) {
        System.out.println("This is a String");
        System.out.println((String) o);
    } else if (o instanceof Multipart) {
        System.out.println("This is a Multipart");
        Multipart mp = (Multipart) o;
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            dumpPart(mp.getBodyPart(i));
    } else if (o instanceof InputStream) {
        System.out.println("This is just an input stream");
        InputStream is = (InputStream) o;
        int c;
        while ((c = is.read()) != -1)
            System.out.write(c);
    }
}

From source file:uidmsgshow.java

public static void dumpEnvelope(Message m) throws Exception {
    System.out.println("This is the message envelope");
    System.out.println("---------------------------");
    Address[] a;//from w  w w  .ja va2  s .c  om
    // FROM 
    if ((a = m.getFrom()) != null) {
        for (int j = 0; j < a.length; j++)
            System.out.println("FROM: " + a[j].toString());
    }

    // TO
    if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
        for (int j = 0; j < a.length; j++)
            System.out.println("TO: " + a[j].toString());
    }

    // SUBJECT
    System.out.println("SUBJECT: " + m.getSubject());

    // DATE
    Date d = m.getSentDate();
    System.out.println("SendDate: " + (d != null ? d.toString() : "UNKNOWN"));

    // SIZE
    System.out.println("Size: " + m.getSize());

    // FLAGS:
    Flags flags = m.getFlags();
    StringBuffer sb = new StringBuffer();
    Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags

    boolean first = true;
    for (int i = 0; i < sf.length; i++) {
        String s;
        Flags.Flag f = sf[i];
        if (f == Flags.Flag.ANSWERED)
            s = "\\Answered";
        else if (f == Flags.Flag.DELETED)
            s = "\\Deleted";
        else if (f == Flags.Flag.DRAFT)
            s = "\\Draft";
        else if (f == Flags.Flag.FLAGGED)
            s = "\\Flagged";
        else if (f == Flags.Flag.RECENT)
            s = "\\Recent";
        else if (f == Flags.Flag.SEEN)
            s = "\\Seen";
        else
            continue; // skip it
        if (first)
            first = false;
        else
            sb.append(' ');
        sb.append(s);
    }

    String[] uf = flags.getUserFlags(); // get the user flag strings
    for (int i = 0; i < uf.length; i++) {
        if (first)
            first = false;
        else
            sb.append(' ');
        sb.append(uf[i]);
    }
    System.out.println("FLAGS = " + sb.toString());

    // X-MAILER
    String[] hdrs = m.getHeader("X-Mailer");
    if (hdrs != null)
        System.out.println("X-Mailer: " + hdrs[0]);
    else
        System.out.println("X-Mailer NOT available");
}

From source file:uidmsgshow.java

public static void dumpEnvelope(Message m) throws Exception {
    System.out.println("This is the message envelope");
    System.out.println("---------------------------");
    Address[] a;//from   w  w  w. ja  v a2 s. c o  m
    // FROM
    if ((a = m.getFrom()) != null) {
        for (int j = 0; j < a.length; j++)
            System.out.println("FROM: " + a[j].toString());
    }

    // TO
    if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
        for (int j = 0; j < a.length; j++)
            System.out.println("TO: " + a[j].toString());
    }

    // SUBJECT
    System.out.println("SUBJECT: " + m.getSubject());

    // DATE
    Date d = m.getSentDate();
    System.out.println("SendDate: " + (d != null ? d.toString() : "UNKNOWN"));

    // SIZE
    System.out.println("Size: " + m.getSize());

    // FLAGS:
    Flags flags = m.getFlags();
    StringBuffer sb = new StringBuffer();
    Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags

    boolean first = true;
    for (int i = 0; i < sf.length; i++) {
        String s;
        Flags.Flag f = sf[i];
        if (f == Flags.Flag.ANSWERED)
            s = "\\Answered";
        else if (f == Flags.Flag.DELETED)
            s = "\\Deleted";
        else if (f == Flags.Flag.DRAFT)
            s = "\\Draft";
        else if (f == Flags.Flag.FLAGGED)
            s = "\\Flagged";
        else if (f == Flags.Flag.RECENT)
            s = "\\Recent";
        else if (f == Flags.Flag.SEEN)
            s = "\\Seen";
        else
            continue; // skip it
        if (first)
            first = false;
        else
            sb.append(' ');
        sb.append(s);
    }

    String[] uf = flags.getUserFlags(); // get the user flag strings
    for (int i = 0; i < uf.length; i++) {
        if (first)
            first = false;
        else
            sb.append(' ');
        sb.append(uf[i]);
    }
    System.out.println("FLAGS = " + sb.toString());

    // X-MAILER
    String[] hdrs = m.getHeader("X-Mailer");
    if (hdrs != null)
        System.out.println("X-Mailer: " + hdrs[0]);
    else
        System.out.println("X-Mailer NOT available");
}

From source file:com.mycompany.apsdtask.MessageToEmailConverter.java

EMail convert(Message message) {
    EMail eMail = new EMail();
    try {//from   ww w  .j a v  a2  s.c om
        eMail.setAuthor(((InternetAddress) message.getFrom()[0]).getAddress());
        eMail.setDate(message.getSentDate());
        eMail.setSubject(message.getSubject());
        Object contnet = message.getContent();
        eMail.setBody((contnet instanceof MimeMultipart)
                ? ((MimeMultipart) contnet).getBodyPart(0).getContent().toString()
                : contnet.toString());
    } catch (IOException | MessagingException e) {
        throw new RuntimeException(e);
    }
    return eMail;
}