List of usage examples for javax.mail Session getInstance
public static Session getInstance(Properties props, Authenticator authenticator)
From source file:MainClass.java
public static void main(String[] args) { if (args.length != 4) { System.out.println("usage: java msgmultisend <to> <from> <smtp> true|false"); return;/* w w w .j a va2 s . com*/ } String to = args[0]; String from = args[1]; String host = args[2]; boolean debug = Boolean.valueOf(args[3]).booleanValue(); // create some properties and get the default Session Properties props = new Properties(); props.put("mail.smtp.host", host); Session session = Session.getInstance(props, null); session.setDebug(debug); try { // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = { new InternetAddress(to) }; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("JavaMail APIs Multipart Test"); msg.setSentDate(new Date()); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(msgText1); // create and fill the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // Use setText(text, charset), to show it off ! mbp2.setText(msgText2, "us-ascii"); // create the Multipart and its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); // send the message Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); } } }
From source file:namespace.java
public static void main(String argv[]) { int msgnum = -1; int optind;/*from w ww . j ava 2 s .c om*/ 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("-D")) { debug = true; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-p")) { port = Integer.parseInt(argv[++optind]); } else if (argv[optind].equals("-u")) { suser = argv[++optind]; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: namespace [-L url] [-T protocol] [-H host] [-p port] [-U user]"); System.out.println("\t[-P password] [-u other-user] [-D]"); System.exit(1); } else { break; } } try { // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); session.setDebug(debug); // 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, port, user, password); else store.connect(); } printFolders("Personal", store.getPersonalNamespaces()); printFolders("User \"" + suser + "\"", store.getUserNamespaces(suser)); printFolders("Shared", store.getSharedNamespaces()); store.close(); } catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); } System.exit(0); }
From source file:populate.java
public static void main(String argv[]) { String srcURL = null;/*w ww . j a v a2 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:folderlist.java
public static void main(String argv[]) throws Exception { int optind;//from w w w . j a v a2 s . com 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("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-R")) { root = argv[++optind]; } else if (argv[optind].equals("-r")) { recursive = true; } else if (argv[optind].equals("-v")) { verbose = true; } else if (argv[optind].equals("-D")) { debug = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: folderlist [-T protocol] [-H host] [-U user] [-P password] [-L url]"); System.out.println("\t[-R root] [-r] [-v] [-D] [pattern]"); System.exit(1); } else { break; } } if (optind < argv.length) pattern = argv[optind]; // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); session.setDebug(debug); // Get a Store object Store store = null; Folder rf = 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(); } // List namespace if (root != null) rf = store.getFolder(root); else rf = store.getDefaultFolder(); dumpFolder(rf, false, ""); if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) { Folder[] f = rf.list(pattern); for (int i = 0; i < f.length; i++) dumpFolder(f[i], recursive, " "); } store.close(); }
From source file:uidmsgshow.java
public static void main(String argv[]) { long uid = -1; int optind;/*from ww w . ja v a 2 s . c o m*/ 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("-v")) { verbose = true; } else if (argv[optind].equals("-f")) { mbox = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println( "Usage: uidmsgshow [-L url] [-T protocol] [-H host] [-U user] [-P password] [-f mailbox] [uid] [-v]"); System.exit(1); } else { break; } } try { if (optind < argv.length) uid = Long.parseLong(argv[optind]); // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, null); // session.setDebug(true); // 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(); } // Open the Folder Folder folder = store.getDefaultFolder(); if (folder == null) { System.out.println("No default folder"); System.exit(1); } folder = folder.getFolder(mbox); if (!folder.exists()) { System.out.println(mbox + " does not exist"); System.exit(1); } if (!(folder instanceof UIDFolder)) { System.out.println("This Provider or this folder does not support UIDs"); System.exit(1); } UIDFolder ufolder = (UIDFolder) folder; folder.open(Folder.READ_WRITE); int totalMessages = folder.getMessageCount(); if (totalMessages == 0) { System.out.println("Empty folder"); folder.close(false); store.close(); System.exit(1); } if (verbose) { int newMessages = folder.getNewMessageCount(); System.out.println("Total messages = " + totalMessages); System.out.println("New messages = " + newMessages); System.out.println("-------------------------------"); } if (uid == -1) { // Attributes & Flags for ALL messages .. Message[] msgs = ufolder.getMessagesByUID(1, UIDFolder.LASTUID); // Use a suitable FetchProfile FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.FLAGS); fp.add("X-Mailer"); folder.fetch(msgs, fp); for (int i = 0; i < msgs.length; i++) { System.out.println("--------------------------"); System.out.println("MESSAGE UID #" + ufolder.getUID(msgs[i]) + ":"); dumpEnvelope(msgs[i]); // dumpPart(msgs[i]); } } else { System.out.println("Getting message UID: " + uid); Message m = ufolder.getMessageByUID(uid); if (m != null) dumpPart(m); else System.out.println("This Message does not exist on this folder"); } folder.close(false); store.close(); } catch (Exception ex) { System.out.println("Oops, got exception! " + ex.getMessage()); ex.printStackTrace(); } System.exit(1); }
From source file:transport.java
public static void main(String[] args) { Properties props = System.getProperties(); // parse the arguments InternetAddress[] addrs = null;// w ww .j a v a 2 s . c o m InternetAddress from; boolean debug = false; if (args.length != 4) { usage(); return; } else { props.put("mail.smtp.host", args[2]); if (args[3].equals("true")) { debug = true; } else if (args[3].equals("false")) { debug = false; } else { usage(); return; } // parse the destination addresses try { addrs = InternetAddress.parse(args[0], false); from = new InternetAddress(args[1]); } catch (AddressException aex) { System.out.println("Invalid Address"); aex.printStackTrace(); return; } } // create some properties and get a Session Session session = Session.getInstance(props, null); session.setDebug(debug); transport t = new transport(); t.go(session, addrs, from); }
From source file:populate.java
public static void main(String argv[]) { String srcURL = null;//from ww w . j a va 2s . 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("--")) { 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:mover.java
public static void main(String argv[]) { int start = 1; int end = -1; int optind;//w ww . j a v a 2s .com for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { // protocol protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { // host host = argv[++optind]; } else if (argv[optind].equals("-U")) { // user user = argv[++optind]; } else if (argv[optind].equals("-P")) { // password password = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-s")) { // Source mbox src = argv[++optind]; } else if (argv[optind].equals("-d")) { // Destination mbox dest = argv[++optind]; } else if (argv[optind].equals("-x")) { // Expunge ? expunge = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]"); System.out.println("\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]"); System.out.println("\t The -x option => EXPUNGE deleted messages"); System.out.println("\t msgnum1 => start of message-range; msgnum2 => end of message-range"); System.exit(1); } else { break; } } if (optind < argv.length) start = Integer.parseInt(argv[optind++]); // start msg if (optind < argv.length) end = Integer.parseInt(argv[optind++]); // end msg try { // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, 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(); } // Open source Folder Folder folder = store.getFolder(src); if (folder == null || !folder.exists()) { System.out.println("Invalid folder: " + src); System.exit(1); } folder.open(Folder.READ_WRITE); int count = folder.getMessageCount(); if (count == 0) { // No messages in the source folder System.out.println(folder.getName() + " is empty"); // Close folder, store and return folder.close(false); store.close(); return; } // Open destination folder, create if reqd Folder dfolder = store.getFolder(dest); if (!dfolder.exists()) dfolder.create(Folder.HOLDS_MESSAGES); if (end == -1) end = count; // Get the message objects to copy Message[] msgs = folder.getMessages(start, end); System.out.println("Moving " + msgs.length + " messages"); if (msgs.length != 0) { folder.copyMessages(msgs, dfolder); folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); // Dump out the Flags of the moved messages, to insure that // all got deleted for (int i = 0; i < msgs.length; i++) { if (!msgs[i].isSet(Flags.Flag.DELETED)) System.out.println("Message # " + msgs[i] + " not deleted"); } } // Close folders and store folder.close(expunge); store.close(); } catch (MessagingException mex) { Exception ex = mex; do { System.out.println(ex.getMessage()); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } }
From source file:MainClass.java
public static void main(String argv[]) { int start = 1; int end = -1; int optind;//from www .ja va 2s. c om for (optind = 0; optind < argv.length; optind++) { if (argv[optind].equals("-T")) { // protocol protocol = argv[++optind]; } else if (argv[optind].equals("-H")) { // host host = argv[++optind]; } else if (argv[optind].equals("-U")) { // user user = argv[++optind]; } else if (argv[optind].equals("-P")) { // password password = argv[++optind]; } else if (argv[optind].equals("-L")) { url = argv[++optind]; } else if (argv[optind].equals("-s")) { // Source mbox src = argv[++optind]; } else if (argv[optind].equals("-d")) { // Destination mbox dest = argv[++optind]; } else if (argv[optind].equals("-x")) { // Expunge ? expunge = true; } else if (argv[optind].equals("--")) { optind++; break; } else if (argv[optind].startsWith("-")) { System.out.println("Usage: mover [-T protocol] [-H host] [-U user] [-P password] [-L url] [-v]"); System.out.println("\t[-s source mbox] [-d destination mbox] [-x] [msgnum1] [msgnum2]"); System.out.println("\t The -x option => EXPUNGE deleted messages"); System.out.println("\t msgnum1 => start of message-range; msgnum2 => end of message-range"); System.exit(1); } else { break; } } if (optind < argv.length) start = Integer.parseInt(argv[optind++]); // start msg if (optind < argv.length) end = Integer.parseInt(argv[optind++]); // end msg try { // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Session.getInstance(props, 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(); } // Open source Folder Folder folder = store.getFolder(src); if (folder == null || !folder.exists()) { System.out.println("Invalid folder: " + folder.getName()); System.exit(1); } folder.open(Folder.READ_WRITE); int count = folder.getMessageCount(); if (count == 0) { // No messages in the source folder System.out.println(folder.getName() + " is empty"); // Close folder, store and return folder.close(false); store.close(); return; } // Open destination folder, create if reqd Folder dfolder = store.getFolder(dest); if (!dfolder.exists()) dfolder.create(Folder.HOLDS_MESSAGES); if (end == -1) end = count; // Get the message objects to copy Message[] msgs = folder.getMessages(start, end); System.out.println("Moving " + msgs.length + " messages"); if (msgs.length != 0) { folder.copyMessages(msgs, dfolder); folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true); // Dump out the Flags of the moved messages, to insure that // all got deleted for (int i = 0; i < msgs.length; i++) { if (!msgs[i].isSet(Flags.Flag.DELETED)) System.out.println("Message # " + msgs[i] + " not deleted"); } } // Close folders and store folder.close(expunge); store.close(); } catch (MessagingException mex) { Exception ex = mex; do { System.out.println(ex.getMessage()); if (ex instanceof MessagingException) ex = ((MessagingException) ex).getNextException(); else ex = null; } while (ex != null); } }
From source file:transport.java
public static void main(String[] args) { Properties props = new Properties(); // parse the arguments InternetAddress[] addrs = null;//from w w w.ja v a 2 s .c o m InternetAddress from; boolean debug = false; if (args.length != 4) { usage(); return; } else { props.put("mail.smtp.host", args[2]); if (args[3].equals("true")) { debug = true; } else if (args[3].equals("false")) { debug = false; } else { usage(); return; } // parse the destination addresses try { addrs = InternetAddress.parse(args[0], false); from = new InternetAddress(args[1]); } catch (AddressException aex) { System.out.println("Invalid Address"); aex.printStackTrace(); return; } } // create some properties and get a Session Session session = Session.getInstance(props, null); session.setDebug(debug); transport t = new transport(); t.go(session, addrs, from); }