title: Lotus Notes Java API. Mail forwarding
body: <p>I would like to forward emails from my Lotus Notes inbox to my gmail account.</p>
  
  <p>Lotus Notes rules and agents are disabled on our server, so I developed external application for that.
  I am using document.send method and mail successfully arrives to my gmail box.
  The only problem is that often the email also duplicated in my Lotus Notes inbox.
  I just found that the reason of that is "CC" and "BCC" fields, which I don't clean up,
  however, I am looking for the way to forward email as it is - which means keep original CC and BCC and TO fields - exactly on the same way as it is done by forwarding agent.</p>
  
  <p>I am using "IBM Notes 9" on Windows 7 64 bit.</p>
  
  <p>I've prepared a code sample that demonstrates what I am doing.</p>
  
  <pre><code>package com.example;
  
  import lotus.domino.*;
  
  public class TestMailForwarder {
      public static void main(String[] args) throws NotesException {
          NotesThread.sinitThread();
          try {
              Session notesSession = NotesFactory.createSession(
                      (String) null, (String) null, Consts.NOTES_PASSWORD);
              DbDirectory dir = notesSession.getDbDirectory(Consts.NOTES_SERVER);
              Database mailDb = dir.openDatabaseByReplicaID(Consts.MAILDB_REPLICA_ID);
              forwardAllEmails(mailDb);
          } finally {
              NotesThread.stermThread();
          }
      }
  
      private static void forwardAllEmails(Database mailDb) throws NotesException {
          View inbox = mailDb.getView("$Inbox");
          //noinspection LoopStatementThatDoesntLoop
          for (Document document = inbox.getFirstDocument(); 
                   null != document; 
                   document = inbox.getNextDocument(document)) {
              document.send(Consts.GMAIL_ADDRESS);
              break;
          }
      }
  }
  </code></pre>