Exemple #1
0
  public void mail(String toAddress, String subject, String body) {
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
    props.put("mail.smtp.ssl.enable", true);
    Authenticator authenticator = null;
    if (login) {
      props.put("mail.smtp.auth", true);
      authenticator =
          new Authenticator() {
            private PasswordAuthentication pa = new PasswordAuthentication(username, password);

            @Override
            public PasswordAuthentication getPasswordAuthentication() {
              return pa;
            }
          };
    }

    Session s = Session.getInstance(props, authenticator);
    s.setDebug(debug);
    MimeMessage email = new MimeMessage(s);
    try {
      email.setFrom(new InternetAddress(from));
      email.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
      email.setSubject(subject);
      email.setSentDate(new Date());
      email.setText(body);
      Transport.send(email);
    } catch (MessagingException ex) {
      ex.printStackTrace();
    }
  }
  protected void sendInit() throws Exception {
    // Set smtp properties
    Properties s_props = new Properties();
    s_props.put("mail.smtp.host", mailHost);
    s_props.put("mail.smtp.port", mailPort);
    s_props.put("mail.smtp.connectiontimeout", mailConnTimeout);
    s_props.put("mail.smtp.timeout", mailSmtpTimeout);

    // Creating messaging session
    Session session = Session.getInstance(s_props, null);
    session.setDebug(debug);

    // Creating transport object for smtp
    transport = session.getTransport("smtp");
    transport.connect(mailHost, mailUser, mailPass);
  }
  public static void main(String[] args) throws Throwable {
    String mailbox = null;
    boolean DEBUG = false;

    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-debug")) {
        DEBUG = true;

      } else if (mailbox == null) {
        mailbox = args[i];

      } else {
        usage("Spurious command line: " + args);
      }
    }

    if (mailbox == null) {
      usage("Missing mailbox");
    }

    // Get a connection to the mail server
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(DEBUG);

    Provider[] providers = session.getProviders();
    for (int i = 0; i < providers.length; i++) {
      Provider provider = providers[i];
      out.println(provider);
    }

    Store store = session.getStore("imap");
    Folder folder = store.getFolder(mailbox);
    Message[] messages = folder.getMessages();
    out.println("Folder " + folder.getName() + " with " + messages.length + " messages");
    for (int i = 0; i < messages.length; i++) {
      Message message = messages[i];
      out.println("  " + i + ": " + message.getSubject());
    }

    out.println("");
  }
  public static void sendSSLMessage(
      String recipients[], String subject, String message, String from) throws MessagingException {
    boolean debug = true;

    /*Da errore ma non c'e' da preoccuparsi*/
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

    Properties props = new Properties();
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.smtp.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.port", SMTP_PORT);
    props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.put("mail.smtp.socketFactory.fallback", "false");

    Session session =
        Session.getDefaultInstance(
            props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("*****@*****.**", "magicrestaurant");
              }
            });

    session.setDebug(debug);

    Message msg = new MimeMessage(session);
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
      addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
  }
  public static void ClaimsMsgShow(
      java.lang.String argvs[],
      Folder folder,
      Store store,
      biz.systempartners.claims.ClaimsViewer claimsViewer) {
    if (claimsViewer == null) {}

    int msgnum = -1;
    int optind = 0;
    InputStream msgStream = System.in;

    if (argvs != null) {

      for (optind = 0; optind < argvs.length; optind++) {
        if (argvs[optind].equals("-T")) {
          protocol = argvs[++optind];
        } else if (argvs[optind].equals("-H")) {
          host = argvs[++optind];
        } else if (argvs[optind].equals("-U")) {
          user = argvs[++optind];
        } else if (argvs[optind].equals("-P")) {
          password = argvs[++optind];
        } else if (argvs[optind].equals("-v")) {
          verbose = true;
        } else if (argvs[optind].equals("-D")) {
          debug = true;
        } else if (argvs[optind].equals("-f")) {
          mbox = argvs[++optind];
        } else if (argvs[optind].equals("-L")) {
          url = argvs[++optind];
        } else if (argvs[optind].equals("-p")) {
          port = Integer.parseInt(argvs[++optind]);
        } else if (argvs[optind].equals("-s")) {
          showStructure = true;
        } else if (argvs[optind].equals("-S")) {
          saveAttachments = true;
        } else if (argvs[optind].equals("-m")) {
          showMessage = true;
        } else if (argvs[optind].equals("-a")) {
          showAlert = true;
        } else if (argvs[optind].equals("--")) {
          optind++;
          break;
        } else if (argvs[optind].startsWith("-")) {
          System.out.println("Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]");
          System.out.println("\t[-P password] [-f mailbox] [msgnum] [-v] [-D] [-s] [-S] [-a]");
          System.out.println("or     msgshow -m [-v] [-D] [-s] [-S] < msg");
          System.exit(1);
        } else {
          break;
        }
      }
    }

    try {
      if (optind < argvs.length) msgnum = Integer.parseInt(argvs[optind]);
      //            msgnum = 1;

      // Get a Properties object
      Properties props = System.getProperties();

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

      if (showMessage) {
        MimeMessage msg = new MimeMessage(session, msgStream);
        dumpPart(msg, claimsViewer);
        //	System.exit(0);
      }
      /*
                 // Get a Store object
                 Store store = null;
                 if (url != null) {
                     URLName urln = new URLName(url);
                     store = session.getStore(urln);
                     if (showAlert) {
                         store.addStoreListener(new StoreListener() {
                             public void notification(StoreEvent e) {
                                 String s;
                                 if (e.getMessageType() == StoreEvent.ALERT)
                                     s = "ALERT: ";
                                 else
                                     s = "NOTICE: ";
                                 System.out.println(s + e.getMessage());
                             }
                         });
                     }
                     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();
                 }


                 // 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 == null) {
        System.out.println("Invalid folder");
        System.exit(1);
      }

      // try to open read/write and if that fails try read-only
      try {
        folder.open(Folder.READ_WRITE);
      } catch (MessagingException ex) {
        folder.open(Folder.READ_ONLY);
      }
      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 (msgnum == -1) {
        // Attributes & Flags for all messages ..
        Message[] msgs = folder.getMessages();

        // 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 #" + (i + 1) + ":");
          dumpEnvelope(msgs[i]);
          // dumpPart(msgs[i]);
        }
      } else {
        Message[] msgs = folder.getMessages();
        for (int n = 0; n < msgs.length; n++) {
          System.out.println("Getting message number: " + n + 1);
          Message m = null;

          try {
            m = folder.getMessage(msgnum + 1);
            // m.setDisposition(
            dumpPart(m, claimsViewer);
            //        m.setExpunged(true);
          } catch (IndexOutOfBoundsException iex) {
            System.out.println("Message number out of range");
          }
        }
        folder.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);
      }
      //            folder.expunge();
      folder.close(true);

      store.close();
    } catch (Exception ex) {
      System.out.println("Oops, got exception! " + ex.getMessage());
      ex.printStackTrace();
      //	    System.exit(1);
    }
    //	System.exit(0);
  }
Exemple #6
0
  public static void main(String argv[]) {
    int optind;

    String subject = null;
    String from = null;
    boolean or = false;
    boolean today = false;

    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("-or")) {
        or = true;
      } else if (argv[optind].equals("-D")) {
        debug = true;
      } else if (argv[optind].equals("-f")) {
        mbox = argv[++optind];
      } else if (argv[optind].equals("-L")) {
        url = argv[++optind];
      } else if (argv[optind].equals("-subject")) {
        subject = argv[++optind];
      } else if (argv[optind].equals("-from")) {
        from = argv[++optind];
      } else if (argv[optind].equals("-today")) {
        today = true;
      } else if (argv[optind].equals("--")) {
        optind++;
        break;
      } else if (argv[optind].startsWith("-")) {
        System.out.println(
            "Usage: search [-D] [-L url] [-T protocol] [-H host] "
                + "[-U user] [-P password] [-f mailbox] "
                + "[-subject subject] [-from from] [-or] [-today]");
        System.exit(1);
      } else {
        break;
      }
    }

    try {

      if ((subject == null) && (from == null) && !today) {
        System.out.println("Specify either -subject, -from or -today");
        System.exit(1);
      }

      // Get a Properties object
      Properties props = System.getProperties();

      // Get a Session object
      Session session = Session.getDefaultInstance(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, user, password);
        else store.connect();
      }

      // Open the Folder

      Folder folder = store.getDefaultFolder();
      if (folder == null) {
        System.out.println("Cant find default namespace");
        System.exit(1);
      }

      folder = folder.getFolder(mbox);
      if (folder == null) {
        System.out.println("Invalid folder");
        System.exit(1);
      }

      folder.open(Folder.READ_ONLY);
      SearchTerm term = null;

      if (subject != null) term = new SubjectTerm(subject);
      if (from != null) {
        FromStringTerm fromTerm = new FromStringTerm(from);
        if (term != null) {
          if (or) term = new OrTerm(term, fromTerm);
          else term = new AndTerm(term, fromTerm);
        } else term = fromTerm;
      }
      if (today) {
        ReceivedDateTerm dateTerm = new ReceivedDateTerm(ComparisonTerm.EQ, new Date());
        if (term != null) {
          if (or) term = new OrTerm(term, dateTerm);
          else term = new AndTerm(term, dateTerm);
        } else term = dateTerm;
      }

      Message[] msgs = folder.search(term);
      System.out.println("FOUND " + msgs.length + " MESSAGES");
      if (msgs.length == 0) // no match
      System.exit(1);

      // Use a suitable FetchProfile
      FetchProfile fp = new FetchProfile();
      fp.add(FetchProfile.Item.ENVELOPE);
      folder.fetch(msgs, fp);

      for (int i = 0; i < msgs.length; i++) {
        System.out.println("--------------------------");
        System.out.println("MESSAGE #" + (i + 1) + ":");
        dumpPart(msgs[i]);
      }

      folder.close(false);
      store.close();
    } catch (Exception ex) {
      System.out.println("Oops, got exception! " + ex.getMessage());
      ex.printStackTrace();
    }

    System.exit(1);
  }
  public static boolean sendMail(
      String userName,
      String passWord,
      String host,
      String port,
      String starttls,
      String auth,
      boolean debug,
      String socketFactoryClass,
      String fallback,
      String[] to,
      String[] cc,
      String[] bcc,
      String subject,
      String text,
      String attachmentPath,
      String attachmentName) {

    // Object Instantiation of a properties file.
    Properties props = new Properties();

    props.put("mail.smtp.user", userName);

    props.put("mail.smtp.host", host);

    if (!"".equals(port)) {
      props.put("mail.smtp.port", port);
    }

    if (!"".equals(starttls)) {
      props.put("mail.smtp.starttls.enable", starttls);
      props.put("mail.smtp.auth", auth);
    }

    if (debug) {

      props.put("mail.smtp.debug", "true");

    } else {

      props.put("mail.smtp.debug", "false");
    }

    if (!"".equals(port)) {
      props.put("mail.smtp.socketFactory.port", port);
    }
    if (!"".equals(socketFactoryClass)) {
      props.put("mail.smtp.socketFactory.class", socketFactoryClass);
    }
    if (!"".equals(fallback)) {
      props.put("mail.smtp.socketFactory.fallback", fallback);
    }

    try {

      Session session = Session.getDefaultInstance(props, null);

      session.setDebug(debug);

      MimeMessage msg = new MimeMessage(session);

      msg.setText(text);

      msg.setSubject(subject);

      Multipart multipart = new MimeMultipart();
      MimeBodyPart messageBodyPart = new MimeBodyPart();
      DataSource source = new FileDataSource(attachmentPath);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(attachmentName);
      multipart.addBodyPart(messageBodyPart);

      msg.setContent(multipart);
      msg.setFrom(new InternetAddress(userName));

      for (int i = 0; i < to.length; i++) {
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
      }

      for (int i = 0; i < cc.length; i++) {
        msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
      }

      for (int i = 0; i < bcc.length; i++) {
        msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
      }

      msg.saveChanges();

      Transport transport = session.getTransport("smtp");

      transport.connect(host, userName, passWord);

      transport.sendMessage(msg, msg.getAllRecipients());

      transport.close();

      return true;

    } catch (Exception mex) {
      mex.printStackTrace();
      return false;
    }
  }
Exemple #8
0
  public static synchronized boolean sendMail(
      String userName,
      String passWord,
      String host,
      String port,
      String starttls,
      String auth,
      boolean debug,
      String socketFactoryClass,
      String fallback,
      String[] to,
      String[] cc,
      String[] bcc,
      String subject,
      String text) {
    Properties props = new Properties();
    // Properties props=System.getProperties();
    props.put("mail.smtp.user", userName);
    props.put("mail.smtp.host", host);
    if (!"".equals(port)) {
      props.put("mail.smtp.port", port);
    }
    if (!"".equals(starttls)) {
      props.put("mail.smtp.starttls.enable", starttls);
    }
    props.put("mail.smtp.auth", auth);
    if (debug) {
      props.put("mail.smtp.debug", "true");
    } else {
      props.put("mail.smtp.debug", "false");
    }
    if (!"".equals(port)) {
      props.put("mail.smtp.socketFactory.port", port);
    }
    if (!"".equals(socketFactoryClass)) {
      props.put("mail.smtp.socketFactory.class", socketFactoryClass);
    }
    if (!"".equals(fallback)) {
      props.put("mail.smtp.socketFactory.fallback", fallback);
    }

    try {
      Session session = Session.getDefaultInstance(props, null);
      session.setDebug(debug);
      MimeMessage msg = new MimeMessage(session);
      msg.setText(text);
      msg.setSubject(subject);
      msg.setFrom(new InternetAddress("*****@*****.**"));
      for (int i = 0; i < to.length; i++) {
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
      }
      msg.saveChanges();
      Transport transport = session.getTransport("smtp");
      transport.connect(host, userName, passWord);
      transport.sendMessage(msg, msg.getAllRecipients());
      transport.close();
      return true;
    } catch (Exception mex) {
      mex.printStackTrace();
      return false;
    }
  }