Example usage for javax.mail URLName getFile

List of usage examples for javax.mail URLName getFile

Introduction

In this page you can find the example usage for javax.mail URLName getFile.

Prototype

public String getFile() 

Source Link

Document

Returns the file name of this URLName.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    URLName server = new URLName("protocol://username:password@host/foldername");

    Session session = Session.getDefaultInstance(new Properties(), null);

    Folder folder = session.getFolder(server);
    if (folder == null) {
        System.out.println("Folder " + server.getFile() + " not found.");
        System.exit(1);/*from   www . j  a v  a  2 s. c om*/
    }
    folder.open(Folder.READ_ONLY);

    // Get the messages from the server
    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
        System.out.println("------------ Message " + (i + 1) + " ------------");
        messages[i].writeTo(System.out);
    }

    folder.close(false);
}

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) {
        System.out.println("Folder " + server.getFile() + " not found.");
        System.exit(1);/*from w  w w . j a va  2  s  . c  o  m*/
    }
    folder.open(Folder.READ_ONLY);

    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
        System.out.println(messages[i].getSize() + " bytes long.");
        System.out.println(messages[i].getLineCount() + " lines.");
    }
    folder.close(false);
}

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) {
        System.out.println("Folder " + server.getFile() + " not found.");
        System.exit(1);//from   www  .ja  va2  s. c om
    }
    folder.open(Folder.READ_ONLY);

    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
        System.out.println("Message " + (i + 1));

        System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
        System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
        String to = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO));
        System.out.println("To: " + to);
        String cc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC));
        System.out.println("Cc: " + cc);
        String bcc = InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC));
        System.out.println("Bcc: " + bcc);
        System.out.println("Subject: " + messages[i].getSubject());
        System.out.println("Sent: " + messages[i].getSentDate());
        System.out.println("Received: " + messages[i].getReceivedDate());
    }
    folder.close(false);
}

From source file:Main.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) {
        System.out.println("Folder " + server.getFile() + " not found.");
        System.exit(1);/*  w  ww.ja  va2  s  .com*/
    }
    folder.open(Folder.READ_ONLY);

    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
        System.out.println(messages[i].getSize() + " bytes long.");
        System.out.println(messages[i].getLineCount() + " lines.");
        String disposition = messages[i].getDisposition();
        if (disposition == null) {
            ; // do nothing
        } else if (disposition.equals(Part.INLINE)) {
            System.out.println("This part should be displayed inline");
        } else if (disposition.equals(Part.ATTACHMENT)) {
            System.out.println("This part is an attachment");
            String fileName = messages[i].getFileName();
            System.out.println("The file name of this attachment is " + fileName);
        }
        String description = messages[i].getDescription();
        if (description != null) {
            System.out.println("The description of this message is " + description);
        }
    }
    folder.close(false);
}

From source file:Main.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) {
        System.out.println("Folder " + server.getFile() + " not found.");
        System.exit(1);//  www . j a va  2  s. c  o  m
    }
    folder.open(Folder.READ_ONLY);
    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
        System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
        System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
        System.out.println(
                "To: " + InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO)));
        System.out.println(
                "Cc: " + InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC)));
        System.out.println(
                "Bcc: " + InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC)));
        System.out.println("Subject: " + messages[i].getSubject());
        System.out.println("Sent: " + messages[i].getSentDate());
        System.out.println("Received: " + messages[i].getReceivedDate());

        if (messages[i].isSet(Flags.Flag.DELETED)) {
            System.out.println("Deleted");
        }
        if (messages[i].isSet(Flags.Flag.ANSWERED)) {
            System.out.println("Answered");
        }
        if (messages[i].isSet(Flags.Flag.DRAFT)) {
            System.out.println("Draft");
        }
        if (messages[i].isSet(Flags.Flag.FLAGGED)) {
            System.out.println("Marked");
        }
        if (messages[i].isSet(Flags.Flag.RECENT)) {
            System.out.println("Recent");
        }
        if (messages[i].isSet(Flags.Flag.SEEN)) {
            System.out.println("Read");
        }
        if (messages[i].isSet(Flags.Flag.USER)) {
            // We don't know what the user flags might be in advance
            // so they're returned as an array of strings
            String[] userFlags = messages[i].getFlags().getUserFlags();
            for (int j = 0; j < userFlags.length; j++) {
                System.out.println("User flag: " + userFlags[j]);
            }
        }
    }
    folder.close(false);
}

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) {
        System.out.println("Folder " + server.getFile() + " not found.");
        System.exit(1);/*  w  w w .ja v a 2  s.c  o m*/
    }
    folder.open(Folder.READ_ONLY);

    // Get the messages from the server
    Message[] messages = folder.getMessages();
    for (int i = 0; i < messages.length; i++) {
        // Get the headers
        System.out.println("From: " + InternetAddress.toString(messages[i].getFrom()));
        System.out.println("Reply-to: " + InternetAddress.toString(messages[i].getReplyTo()));
        System.out.println(
                "To: " + InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.TO)));
        System.out.println(
                "Cc: " + InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.CC)));
        System.out.println(
                "Bcc: " + InternetAddress.toString(messages[i].getRecipients(Message.RecipientType.BCC)));
        System.out.println("Subject: " + messages[i].getSubject());

        System.out.println("Sent: " + messages[i].getSentDate());
        System.out.println("Received: " + messages[i].getReceivedDate());

        if (messages[i].isSet(Flags.Flag.DELETED)) {
            System.out.println("Deleted");
        }
        if (messages[i].isSet(Flags.Flag.ANSWERED)) {
            System.out.println("Answered");
        }
        if (messages[i].isSet(Flags.Flag.DRAFT)) {
            System.out.println("Draft");
        }
        if (messages[i].isSet(Flags.Flag.FLAGGED)) {
            System.out.println("Marked");
        }
        if (messages[i].isSet(Flags.Flag.RECENT)) {
            System.out.println("Recent");
        }
        if (messages[i].isSet(Flags.Flag.SEEN)) {
            System.out.println("Read");
        }
        if (messages[i].isSet(Flags.Flag.USER)) {
            // We don't know what the user flags might be in advance
            // so they're returned as an array of strings
            String[] userFlags = messages[i].getFlags().getUserFlags();
            for (int j = 0; j < userFlags.length; j++) {
                System.out.println("User flag: " + userFlags[j]);
            }
        }
    }
    folder.close(false);
}

From source file:populate.java

public static void main(String argv[]) {
    String srcURL = null;/*from w w  w.  j  a va2  s.  c o  m*/
    String dstURL = null;
    boolean debug = false;

    int optind;

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-s")) {
            srcURL = argv[++optind];
        } else if (argv[optind].equals("-d")) {
            dstURL = argv[++optind];
        } else if (argv[optind].equals("-D")) {
            debug = true;
        } else if (argv[optind].equals("-f")) {
            force = true;
        } else if (argv[optind].equals("-S")) {
            skipSpecial = true;
        } else if (argv[optind].equals("-c")) {
            clear = true;
        } else if (argv[optind].equals("-P")) {
            dontPreserveFlags = true;
        } else if (argv[optind].equals("-W")) {
            warn = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            printUsage();
            System.exit(1);
        } else {
            break;
        }
    }

    try {

        if (srcURL == null || dstURL == null) {
            printUsage();
            System.exit(1);
        }

        Session session = Session.getInstance(System.getProperties(), null);
        session.setDebug(debug);

        // Get source folder
        URLName srcURLName = new URLName(srcURL);
        Folder srcFolder;
        // Check if the source URL has a folder specified. If
        // not, we use the default folder
        if (srcURLName.getFile() == null) {
            Store s = session.getStore(srcURLName);
            s.connect();
            srcFolder = s.getDefaultFolder();
        } else {
            srcFolder = session.getFolder(new URLName(srcURL));
            if (!srcFolder.exists()) {
                System.out.println("source folder does not exist");
                srcFolder.getStore().close();
                System.exit(1);
            }
        }

        // Set up destination folder
        URLName dstURLName = new URLName(dstURL);
        Folder dstFolder;
        // Check if the destination URL has a folder specified. If
        // not, we use the source folder name
        if (dstURLName.getFile() == null) {
            Store s = session.getStore(dstURLName);
            s.connect();
            dstFolder = s.getFolder(srcFolder.getName());
        } else
            dstFolder = session.getFolder(dstURLName);

        if (clear && dstFolder.exists()) {
            if (!dstFolder.delete(true)) {
                System.out.println("couldn't delete " + dstFolder.getFullName());
                return;
            }
        }
        copy(srcFolder, dstFolder);

        // Close the respective stores.
        srcFolder.getStore().close();
        dstFolder.getStore().close();

    } catch (MessagingException mex) {
        System.out.println(mex.getMessage());
        mex.printStackTrace();
    }
}

From source file:populate.java

public static void main(String argv[]) {
    String srcURL = null;//w  w w  .  ja v  a 2s  .c om
    String dstURL = null;
    boolean debug = false;

    int optind;

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-s")) {
            srcURL = argv[++optind];
        } else if (argv[optind].equals("-d")) {
            dstURL = argv[++optind];
        } else if (argv[optind].equals("-D")) {
            debug = true;
        } else if (argv[optind].equals("-f")) {
            force = true;
        } else if (argv[optind].equals("-S")) {
            skipSpecial = true;
        } else if (argv[optind].equals("-c")) {
            clear = true;
        } else if (argv[optind].equals("-P")) {
            dontPreserveFlags = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            printUsage();
            System.exit(1);
        } else {
            break;
        }
    }

    try {

        if (srcURL == null || dstURL == null) {
            printUsage();
            System.exit(1);
        }

        Session session = Session.getInstance(System.getProperties(), null);
        session.setDebug(debug);

        // Get source folder
        URLName srcURLName = new URLName(srcURL);
        Folder srcFolder;
        // Check if the source URL has a folder specified. If
        // not, we use the default folder
        if (srcURLName.getFile() == null) {
            Store s = session.getStore(srcURLName);
            s.connect();
            srcFolder = s.getDefaultFolder();
        } else {
            srcFolder = session.getFolder(new URLName(srcURL));
            if (!srcFolder.exists()) {
                System.out.println("source folder does not exist");
                srcFolder.getStore().close();
                System.exit(1);
            }
        }

        // Set up destination folder
        URLName dstURLName = new URLName(dstURL);
        Folder dstFolder;
        // Check if the destination URL has a folder specified. If
        // not, we use the source folder name
        if (dstURLName.getFile() == null) {
            Store s = session.getStore(dstURLName);
            s.connect();
            dstFolder = s.getFolder(srcFolder.getName());
        } else
            dstFolder = session.getFolder(dstURLName);

        if (clear && dstFolder.exists()) {
            if (!dstFolder.delete(true)) {
                System.out.println("couldn't delete " + dstFolder.getFullName());
                return;
            }
        }
        copy(srcFolder, dstFolder);

        // Close the respective stores.
        srcFolder.getStore().close();
        dstFolder.getStore().close();

    } catch (MessagingException mex) {
        System.out.println(mex.getMessage());
        mex.printStackTrace();
    }
}

From source file:com.adaptris.core.mail.MailHelper.java

static final URLName createURLName(String urlString, String username, String password)
        throws PasswordException {
    URLName url = new URLName(urlString);
    String realPassword = url.getPassword();
    String realUser = url.getUsername();
    if (realUser == null && !isEmpty(username)) {
        realUser = username;/*from   w  w  w  .j  a  v  a2s  .c om*/
    }
    if (url.getPassword() == null && password != null) {
        realPassword = Password.decode(password);
    }
    return new URLName(url.getProtocol(), url.getHost(), url.getPort(), url.getFile(), realUser, realPassword);
}

From source file:com.adaptris.mail.MailReceiverCase.java

protected static final URLName createURLName(String urlString, String uname, String pw)
        throws PasswordException {
    URLName url = new URLName(urlString);
    String password = url.getPassword();
    String username = url.getUsername();
    if (username == null && !isEmpty(uname)) {
        username = uname;/*from  w  ww .  j av a2 s  .com*/
    }
    if (url.getPassword() == null && pw != null) {
        password = Password.decode(pw);
    }
    return new URLName(url.getProtocol(), url.getHost(), url.getPort(), url.getFile(), username, password);
}