Ejemplo n.º 1
0
 public static void copyMessage(Message message, Folder destination) {
   boolean sourceOpened = false;
   boolean destinationOpened = false;
   Folder source = message.getFolder();
   try {
     if (!source.isOpen()) {
       source.open(Folder.READ_ONLY);
       sourceOpened = true;
     }
     if (!destination.isOpen()) {
       destination.open(Folder.READ_WRITE);
       destinationOpened = true;
     }
     try {
       source.copyMessages(new Message[] {message}, destination);
     } catch (MessagingException e) {
       destination.appendMessages(new Message[] {message});
     }
   } catch (MessagingException ex) {
     throw new RuntimeException(
         "Copying message "
             + toString(message)
             + " from "
             + source.getName()
             + " to "
             + destination.getName()
             + " failed.",
         ex);
   } finally {
     if (sourceOpened) closeFolder(source, false);
     if (destinationOpened) closeFolder(destination, false);
   }
 }
Ejemplo n.º 2
0
  /** добавить сообщение в папку "отправленные" */
  private void appendEmailToSent(Message message) {
    try {
      Properties properties = new Properties();

      properties.put("mail.store.protocol", "imaps");
      MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
      socketFactory.setTrustAllHosts(true);
      properties.put("mail.imaps.ssl.socketFactory", socketFactory);
      properties.put("mail.imaps.ssl.trust", "*");
      properties.put("mail.imaps.ssl.checkserveridentity", "false");
      properties.put("mail.imaps.host", HOST);
      properties.put("mail.imaps.port", IMAP_PORT);
      // присоединиться к IMAP
      Session session = Session.getDefaultInstance(properties, null);
      Store store = session.getStore("imaps");
      store.connect(HOST, IMAP_PORT, POST_LOGIN, POST_PASSWORD);
      // добавить сообщение в папку
      Folder inbox = store.getFolder("Sent");
      inbox.open(Folder.READ_WRITE);
      Message[] msgs = {message};
      inbox.appendMessages(msgs);
      inbox.close(false);
      store.close();
    } catch (Exception e) {
      getActionResult().addMessage("не удалось поместить письмо в папку отправленные");
    }
  }
Ejemplo n.º 3
0
  /**
   * @param buff
   * @throws Exception
   */
  public void appendEmail(byte[] buff) throws Exception {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props);
    ByteArrayInputStream bis = new ByteArrayInputStream(buff);
    MimeMessage msg = new MimeMessage(session, bis);
    msg.setFlag(Flags.Flag.SEEN, true);

    ProtocolFactory factory = new ProtocolFactory(profile, auth, handler);
    ImapProtocolImpl imap = (ImapProtocolImpl) factory.getImap(folder);
    Folder f = imap.getFolder();

    try {
      f.appendMessages(new Message[] {msg});
    } catch (MessagingException e) {
      log.warn("appenging msg to folder : " + folder + " failed.", e);
    } finally {
      bis.close();
    }
  }
Ejemplo n.º 4
0
  private void doit(String[] argv) {
    String to = null,
        subject = null,
        from = null,
        replyTo = null,
        cc = null,
        bcc = null,
        url = null;
    String mailhost = null;
    String mailer = "MsgSend";
    String protocol = null, host = null, user = null, password = null, record = null;
    String filename = null, msg_text = null, inline_filename = null;
    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("-X")) {
        msg_text = 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")) {
        filename = argv[++optind];
      } else if (argv[optind].equals("-i")) {
        inline_filename = argv[++optind];
      } else if (argv[optind].equals("-s")) {
        subject = argv[++optind];
      } else if (argv[optind].equals("-o")) { // originator (from)
        from = argv[++optind];
      } else if (argv[optind].equals("-r")) { // reply-to
        replyTo = 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.err.println(USAGE_TEXT);
        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();

      if (reply_to_list == null && replyTo != null) {
        reply_to_list = new InternetAddress[1];
        reply_to_list[0] = new InternetAddress(replyTo);
        msg.setReplyTo(reply_to_list);
      } else msg.setReplyTo(reply_to_list);

      if (dis_list == null) {
        dis_list = new InternetAddress[1];
        dis_list[0] = new InternetAddress(to);
      }

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

      // in-line file contents if specified
      if (inline_filename != null) {
        msg_text = readFile(inline_filename);
      }

      // create and fill the first message part
      MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setText(msg_text);

      // create the Multipart and add the text part
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(mbp1);

      // create additional message part(s)

      // attach the file or files to the message
      if (filename != null) {
        MimeBodyPart mbp = new MimeBodyPart();
        FileDataSource fds = new FileDataSource(filename);
        mbp.setDataHandler(new DataHandler(fds));
        mbp.setFileName(fds.getName());
        mp.addBodyPart(mbp);
        mbp1.setText(msg_text + "\n\nAttachment: " + filename);
        System.out.println("Added attachment: " + filename);
      }

      if (attachments != null) {
        Iterator i = attachments.iterator();
        StringBuffer list = null;
        while (i.hasNext()) {
          String name = (String) i.next();
          MimeBodyPart mbp = new MimeBodyPart();
          FileDataSource fds = new FileDataSource(name);
          mbp.setDataHandler(new DataHandler(fds));
          mbp.setFileName(fds.getName());
          mp.addBodyPart(mbp);
          if (list == null) {
            list = new StringBuffer(name);
          } else {
            list.append(", " + name);
          }
          System.out.println("Added attachment: " + name);
          mbp1.setText(msg_text + "\nAttachment(s): " + list);
        }
      }

      // add the Multipart to the message
      msg.setContent(mp);

      msg.setSubject(subject);

      // jgfrun collect(in, msg);

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

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

      System.out.println("Mail 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) {
      System.err.println("Could not MsgSend.doit");
      e.printStackTrace();
    }
  } // doit
  protected void createInitialIMAPTestdata(
      final Properties props,
      final String user,
      final String password,
      final int count,
      final boolean deleteall)
      throws MessagingException {
    final Session session = Session.getInstance(props);
    final Store store = session.getStore();
    store.connect(user, password);
    checkStoreForTestConnection(store);
    final Folder root = store.getDefaultFolder();
    final Folder testroot = root.getFolder("ES-IMAP-RIVER-TESTS");
    final Folder testrootl2 = testroot.getFolder("Level(2!");

    if (deleteall) {

      deleteMailsFromUserMailbox(props, "INBOX", 0, -1, user, password);

      if (testroot.exists()) {
        testroot.delete(true);
      }

      final Folder testrootenamed = root.getFolder("renamed_from_ES-IMAP-RIVER-TESTS");
      if (testrootenamed.exists()) {
        testrootenamed.delete(true);
      }
    }

    if (!testroot.exists()) {

      testroot.create(Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES);
      testroot.open(Folder.READ_WRITE);

      testrootl2.create(Folder.HOLDS_FOLDERS & Folder.HOLDS_MESSAGES);
      testrootl2.open(Folder.READ_WRITE);
    }

    final Folder inbox = root.getFolder("INBOX");
    inbox.open(Folder.READ_WRITE);

    final Message[] msgs = new Message[count];

    for (int i = 0; i < count; i++) {
      final MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(EMAIL_TO));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(EMAIL_USER_ADDRESS));
      message.setSubject(EMAIL_SUBJECT + "::" + i);
      message.setText(EMAIL_TEXT + "::" + SID++);
      message.setSentDate(new Date());
      msgs[i] = message;
    }

    inbox.appendMessages(msgs);
    testroot.appendMessages(msgs);
    testrootl2.appendMessages(msgs);

    IMAPUtils.close(inbox);
    IMAPUtils.close(testrootl2);
    IMAPUtils.close(testroot);
    IMAPUtils.close(store);
  }
  private void prepareMailbox() throws Exception {
    // connect to mailbox
    Mailbox.clearAll();
    JavaMailSender sender = new DefaultJavaMailSender();
    Store store = sender.getSession().getStore("pop3");
    store.connect("localhost", 25, "bill", "secret");
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    folder.expunge();

    long twoDaysAgo = new Date().getTime() - 2 * 24 * 60 * 60 * 1000L;
    long twentyHoursAgo = new Date().getTime() - 1 * 20 * 60 * 60 * 1000L;
    long oneHourAgo = new Date().getTime() - 1 * 1 * 60 * 60 * 1000L;

    // inserts 5 new messages
    Message[] messages = new Message[6];
    messages[0] = new MimeMessage(sender.getSession());
    messages[0].setSubject("Apache Camel rocks");
    messages[0].setText("I like riding the Camel");
    messages[0].setHeader("Message-ID", "0");
    messages[0].setFrom(new InternetAddress("*****@*****.**"));
    messages[0].setSentDate(new Date(twoDaysAgo));

    messages[1] = new MimeMessage(sender.getSession());
    messages[1].setSubject("Order");
    messages[1].setText("Ordering Camel in Action");
    messages[1].setFrom(new InternetAddress("*****@*****.**"));
    messages[1].setHeader("Message-ID", "1");
    messages[1].setSentDate(new Date(twoDaysAgo));

    messages[2] = new MimeMessage(sender.getSession());
    messages[2].setSubject("Order");
    messages[2].setText("Ordering ActiveMQ in Action");
    messages[2].setHeader("Message-ID", "2");
    messages[2].setFrom(new InternetAddress("*****@*****.**"));
    messages[2].setSentDate(new Date(twentyHoursAgo));

    messages[3] = new MimeMessage(sender.getSession());
    messages[3].setSubject("Buy pharmacy");
    messages[3].setText("This is spam");
    messages[3].setHeader("Message-ID", "3");
    messages[3].setFrom(new InternetAddress("*****@*****.**"));
    messages[3].setSentDate(new Date(twentyHoursAgo));

    messages[4] = new MimeMessage(sender.getSession());
    messages[4].setSubject("Beers tonight?");
    messages[4].setText("We meet at 7pm the usual place");
    messages[4].setHeader("Message-ID", "4");
    messages[4].setFrom(new InternetAddress("*****@*****.**"));
    messages[4].setSentDate(new Date(oneHourAgo));

    messages[5] = new MimeMessage(sender.getSession());
    messages[5].setSubject("Spambot attack");
    messages[5].setText("I am attaching you");
    messages[5].setHeader("Message-ID", "5");
    messages[5].setFrom(new InternetAddress("*****@*****.**"));
    messages[5].setSentDate(new Date());
    messages[5].setSentDate(new Date(oneHourAgo));

    folder.appendMessages(messages);
    folder.close(true);
  }