Example usage for javax.mail URLName URLName

List of usage examples for javax.mail URLName URLName

Introduction

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

Prototype

public URLName(String url) 

Source Link

Document

Construct a URLName from the string.

Usage

From source file:edu.stanford.muse.email.MboxEmailStore.java

public Store connect() throws MessagingException {
    // Get a Session object
    // can customize javamail properties here if needed, see e.g. http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/package-summary.html

    Session session = Session.getInstance(mstorProps, null);
    session.setDebug(DEBUG);/*from ww w.ja  v a  2  s .  co  m*/

    // Get a Store object
    Store store = session.getStore(new URLName("mstor:" + Util.devNullPath())); // although "store" is irrelevant with mbox, connect/close may still be attempted on it. thus, use /dev/null rather than leaving it at / or unspecified path (which may trigger file io error).
    store.connect();
    return store;
}

From source file:com.adaptris.util.URLString.java

/**
 * Construct a URLString from the string. Parses out all the possible information (protocol, host,
 * port, file, username, password)./* w ww  .  j  a v a2 s  . c  om*/
 */
public URLString(String url) {
    urlProxy = new URLName(url);
    this.fullURL = urlProxy.toString();
    this.protocol = urlProxy.getProtocol();
    this.host = urlProxy.getHost();
    this.port = urlProxy.getPort();
    this.file = urlProxy.getFile();
    this.ref = urlProxy.getRef();
    this.username = urlProxy.getUsername();
    this.password = urlProxy.getPassword();
}

From source file:org.springframework.integration.mail.config.MailReceiverFactoryBean.java

private void verifyProtocol() {
    if (StringUtils.hasText(this.storeUri)) {
        URLName urlName = new URLName(this.storeUri);
        if (this.protocol == null) {
            this.protocol = urlName.getProtocol();
        } else {/*from ww  w.  j  av a  2s  .  c  o  m*/
            Assert.isTrue(this.protocol.equals(urlName.getProtocol()),
                    "The provided 'protocol' does not match that in the 'storeUri'.");
        }
    } else {
        Assert.hasText(this.protocol, "Either the 'storeUri' or 'protocol' is required.");
    }
    Assert.hasText(this.protocol, "Unable to resolve protocol.");
}

From source file:it.unimi.di.big.mg4j.document.JavamailDocumentCollection.java

/** Builds a document collection corresponding to a given store URL and folder name.
 * //from ww w  . ja v a2  s .c  om
 *  <p><strong>Beware.</strong> This class is not suited for large mbox files!
 * 
 * @param storeUrl the javamail URL of the store.
 * @param folderName the folder name.
 * @param factory the factory that will be used to create documents.
 * @throws MessagingException 
 */
protected JavamailDocumentCollection(final String storeUrl, final String folderName,
        final JavamailDocumentFactory factory) throws MessagingException {
    this.storeUrl = storeUrl;
    this.folderName = folderName;
    this.factory = factory;

    this.store = SESSION.getStore(new URLName(storeUrl));
    store.connect();

    this.folder = store.getDefaultFolder().getFolder(folderName);
    folder.open(Folder.READ_ONLY);

    this.numberOfMessages = folder.getMessageCount();
}

From source file:com.google.code.rptm.mailarchive.DefaultMailingListArchive.java

public void retrieveMessages(String mailingList, YearMonth month, MimeMessageProcessor processor,
        MailingListArchiveEventListener eventListener) throws MailingListArchiveException {
    Session session = Session.getDefaultInstance(new Properties());
    try {/*from  www .  j av a2  s. c  o m*/
        Store store = session.getStore(new URLName("mstor:" + getMboxFile(mailingList, month, eventListener)));
        store.connect();
        try {
            Folder folder = store.getDefaultFolder();
            folder.open(Folder.READ_ONLY);
            for (Message msg : folder.getMessages()) {
                if (!processor.processMessage((MimeMessage) msg)) {
                    break;
                }
            }
        } finally {
            store.close();
        }
    } catch (MessagingException ex) {
        throw new MailingListArchiveException("JavaMail exception: " + ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new MailingListArchiveException("I/O exception: " + ex.getMessage(), ex);
    }
}

From source file:org.xwiki.contrib.mail.internal.AbstractMailStore.java

/**
 * Creates appropriate javamail Store object.
 * //from ww w .j  a v a  2  s  .  c  o  m
 * @param debug
 * @return
 * @throws NoSuchProviderException
 */
protected Store getJavamailStore(boolean debug) throws NoSuchProviderException {
    Properties props = getStoreProperties();
    Session session = Session.getInstance(props);
    if (debug) {
        session.setDebug(true);
    }

    String url = getProvider() + ":" + getMailSource().getLocation();

    return session.getStore(new URLName(url));
}

From source file:sendhtml.java

public sendhtml(String[] argv) {

    String to, subject = null, from = null, cc = null, bcc = null, url = null;
    String mailhost = null;//from   w ww  .  j  a v  a 2  s . co m
    String mailer = "sendhtml";
    String protocol = null, host = null, user = null, password = null;
    String record = null; // name of folder in which to record mail
    boolean debug = false;
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int optind;

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-T")) {
            protocol = argv[++optind];
        } else if (argv[optind].equals("-H")) {
            host = argv[++optind];
        } else if (argv[optind].equals("-U")) {
            user = argv[++optind];
        } else if (argv[optind].equals("-P")) {
            password = argv[++optind];
        } else if (argv[optind].equals("-M")) {
            mailhost = argv[++optind];
        } else if (argv[optind].equals("-f")) {
            record = argv[++optind];
        } else if (argv[optind].equals("-s")) {
            subject = argv[++optind];
        } else if (argv[optind].equals("-o")) { // originator
            from = argv[++optind];
        } else if (argv[optind].equals("-c")) {
            cc = argv[++optind];
        } else if (argv[optind].equals("-b")) {
            bcc = argv[++optind];
        } else if (argv[optind].equals("-L")) {
            url = argv[++optind];
        } else if (argv[optind].equals("-d")) {
            debug = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            System.out.println("Usage: sendhtml [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
            System.out.println("\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
            System.out.println("\t[-f record-mailbox] [-M transport-host] [-d] [address]");
            System.exit(1);
        } else {
            break;
        }
    }

    try {
        if (optind < argv.length) {
            // XXX - concatenate all remaining arguments
            to = argv[optind];
            System.out.println("To: " + to);
        } else {
            System.out.print("To: ");
            System.out.flush();
            to = in.readLine();
        }
        if (subject == null) {
            System.out.print("Subject: ");
            System.out.flush();
            subject = in.readLine();
        } else {
            System.out.println("Subject: " + subject);
        }

        Properties props = System.getProperties();
        // XXX - could use Session.getTransport() and Transport.connect()
        // XXX - assume we're using SMTP
        if (mailhost != null)
            props.put("mail.smtp.host", mailhost);

        // Get a Session object
        Session session = Session.getInstance(props, null);
        if (debug)
            session.setDebug(true);

        // construct the message
        Message msg = new MimeMessage(session);
        if (from != null)
            msg.setFrom(new InternetAddress(from));
        else
            msg.setFrom();

        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        if (cc != null)
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
        if (bcc != null)
            msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));

        msg.setSubject(subject);

        collect(in, msg);

        msg.setHeader("X-Mailer", mailer);
        msg.setSentDate(new Date());

        // send the thing off
        Transport.send(msg);

        System.out.println("\nMail was sent successfully.");

        // Keep a copy, if requested.

        if (record != null) {
            // Get a Store object
            Store store = null;
            if (url != null) {
                URLName urln = new URLName(url);
                store = session.getStore(urln);
                store.connect();
            } else {
                if (protocol != null)
                    store = session.getStore(protocol);
                else
                    store = session.getStore();

                // Connect
                if (host != null || user != null || password != null)
                    store.connect(host, user, password);
                else
                    store.connect();
            }

            // Get record Folder.  Create if it does not exist.
            Folder folder = store.getFolder(record);
            if (folder == null) {
                System.err.println("Can't get record folder.");
                System.exit(1);
            }
            if (!folder.exists())
                folder.create(Folder.HOLDS_MESSAGES);

            Message[] msgs = new Message[1];
            msgs[0] = msg;
            folder.appendMessages(msgs);

            System.out.println("Mail was recorded successfully.");
        }

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

From source file:sendhtml.java

public sendhtml(String[] argv) {

    String to, subject = null, from = null, cc = null, bcc = null, url = null;
    String mailhost = null;/*  w  w  w. ja  v a2  s.com*/
    String mailer = "sendhtml";
    String protocol = null, host = null, user = null, password = null;
    String record = null; // name of folder in which to record mail
    boolean debug = false;
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    int optind;

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-T")) {
            protocol = argv[++optind];
        } else if (argv[optind].equals("-H")) {
            host = argv[++optind];
        } else if (argv[optind].equals("-U")) {
            user = argv[++optind];
        } else if (argv[optind].equals("-P")) {
            password = argv[++optind];
        } else if (argv[optind].equals("-M")) {
            mailhost = argv[++optind];
        } else if (argv[optind].equals("-f")) {
            record = argv[++optind];
        } else if (argv[optind].equals("-s")) {
            subject = argv[++optind];
        } else if (argv[optind].equals("-o")) { // originator
            from = argv[++optind];
        } else if (argv[optind].equals("-c")) {
            cc = argv[++optind];
        } else if (argv[optind].equals("-b")) {
            bcc = argv[++optind];
        } else if (argv[optind].equals("-L")) {
            url = argv[++optind];
        } else if (argv[optind].equals("-d")) {
            debug = true;
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            System.out.println("Usage: sendhtml [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
            System.out.println("\t[-s subject] [-o from-address] [-c cc-addresses] [-b bcc-addresses]");
            System.out.println("\t[-f record-mailbox] [-M transport-host] [-d] [address]");
            System.exit(1);
        } else {
            break;
        }
    }

    try {
        if (optind < argv.length) {
            // XXX - concatenate all remaining arguments
            to = argv[optind];
            System.out.println("To: " + to);
        } else {
            System.out.print("To: ");
            System.out.flush();
            to = in.readLine();
        }
        if (subject == null) {
            System.out.print("Subject: ");
            System.out.flush();
            subject = in.readLine();
        } else {
            System.out.println("Subject: " + subject);
        }

        Properties props = System.getProperties();
        // XXX - could use Session.getTransport() and Transport.connect()
        // XXX - assume we're using SMTP
        if (mailhost != null)
            props.put("mail.smtp.host", mailhost);

        // Get a Session object
        Session session = Session.getInstance(props, null);
        if (debug)
            session.setDebug(true);

        // construct the message
        Message msg = new MimeMessage(session);
        if (from != null)
            msg.setFrom(new InternetAddress(from));
        else
            msg.setFrom();

        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        if (cc != null)
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
        if (bcc != null)
            msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));

        msg.setSubject(subject);

        collect(in, msg);

        msg.setHeader("X-Mailer", mailer);
        msg.setSentDate(new Date());

        // send the thing off
        Transport.send(msg);

        System.out.println("\nMail was sent successfully.");

        // Keep a copy, if requested.

        if (record != null) {
            // Get a Store object
            Store store = null;
            if (url != null) {
                URLName urln = new URLName(url);
                store = session.getStore(urln);
                store.connect();
            } else {
                if (protocol != null)
                    store = session.getStore(protocol);
                else
                    store = session.getStore();

                // Connect
                if (host != null || user != null || password != null)
                    store.connect(host, user, password);
                else
                    store.connect();
            }

            // Get record Folder. Create if it does not exist.
            Folder folder = store.getFolder(record);
            if (folder == null) {
                System.err.println("Can't get record folder.");
                System.exit(1);
            }
            if (!folder.exists())
                folder.create(Folder.HOLDS_MESSAGES);

            Message[] msgs = new Message[1];
            msgs[0] = msg;
            folder.appendMessages(msgs);

            System.out.println("Mail was recorded successfully.");
        }

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

From source file:edu.stanford.muse.email.MboxEmailStore.java

public Folder openFolderWithoutCount(Store s, String fname) throws MessagingException {
    if (fname == null)
        fname = "INBOX";

    // ignore the store coming in, we need a new session and store
    // for each folder

    Session session = Session.getInstance(mstorProps, null);
    session.setDebug(DEBUG);//from w  ww  .jav a2 s.com

    // Get a Store object
    Store store = session.getStore(new URLName("mstor:" + fname));
    store.connect();

    //This is the root folder in the namespace provided
    //see http://docs.oracle.com/javaee/5/api/javax/mail/Store.html#getDefaultFolder%28%29
    Folder folder = store.getDefaultFolder();
    if (folder == null)
        throw new RuntimeException("Invalid folder: " + fname);

    log.info("Opening folder " + Util.blurPath(fname) + " in r/o mode...");
    try {
        folder.open(Folder.READ_ONLY);
    } catch (MessagingException me) {
        folder = null;
    }

    return folder;
}

From source file:org.socraticgrid.displaymaildata.DisplayMailDataHandler.java

/**
 * /* www  .  j  ava 2s  . c  o  m*/
 * @param session
 * @param sslStore
 * @param access
 * @param folderName
 * @return
 * @throws MessagingException
 * @throws NoSuchProviderException
 */
private Folder getImapFolder(Session session, IMAPSSLStore sslStore, String[] access, String folderName)
        throws MessagingException, NoSuchProviderException {
    Folder folder;
    Provider provider = session.getProvider("imap");
    session.setProvider(provider);
    URLName urlName = new URLName(mailUrl);
    sslStore = new IMAPSSLStore(session, urlName);
    sslStore.connect(host, access[0], access[1]);
    folder = sslStore.getFolder(folderName);
    return folder;
}