8 Replies Latest reply: Aug 20, 2011 4:33 PM by matfud RSS

    how to create exchange 2007 mailbox with jndi

    775730
      hello,

      I would like to create a mailbox on my Exchange 2007 server at the same time as the active directory account creation, I know already create the account active directoy and here is the code I use for the active directory:

      public class AjoutDAO {
           

           public static void creatuser(User user, ActiveD actived) throws NamingException, SQLException, ClassNotFoundException {
                Hashtable ldapEnv = new Hashtable();
                ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
                ldapEnv.put(Context.PROVIDER_URL, "ldap://" + actived.getServerip() + ":389");
                ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
                ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=administrateur" + actived.getBaseName2());
                ldapEnv.put(Context.SECURITY_CREDENTIALS, "p@ssw0rd");
           
           
           

                     try {
                          InitialDirContext ldapContext = new InitialDirContext(ldapEnv);
      String nom_complet = user.getGivenname()+" "+ user.getSurname();
           //Object unite1 = jCB1.getSelectedItem().toString();
                          String distinguishedName = "cn=" + nom_complet + ",ou=" actived.getUnite() actived.getBaseName() ;
                          Attributes newAttributes = new BasicAttributes(true);
                          Attribute oc = new BasicAttribute("objectclass");
                          oc.add("top");
                          oc.add("person");
                          oc.add("organizationalperson");
                          oc.add("user");
                          newAttributes.put(oc);
                          newAttributes.put(new BasicAttribute("sAMAccountName", user.getUsername()));
                     //     newAttributes.put(new BasicAttribute("userPassword", user.getMdp()));
                          newAttributes.put(new BasicAttribute("userPrincipalName", user.getUsername() + "@" + actived.getDomaind()));
                          newAttributes.put(new BasicAttribute("cn", nom_complet/*user.getUsername()*/));
                          newAttributes.put(new BasicAttribute("sn", user.getSurname()));
                          newAttributes.put(new BasicAttribute("givenName", user.getGivenname()));
                          newAttributes.put(new BasicAttribute("displayName", user.getGivenname() + " " + user.getSurname()));
           newAttributes.put(new BasicAttribute("l", user.getVille()));
           newAttributes.put(new BasicAttribute("telephoneNumber", user.getTel()));
           newAttributes.put(new BasicAttribute("company", user.getSociete()));
           newAttributes.put(new BasicAttribute("Department", user.getDept()));
           newAttributes.put(new BasicAttribute("userAccountControl", Integer.toString(0x0200)));
           newAttributes.put(new BasicAttribute("userAccountControl", Integer.toString(0x10000)));
           

           boolean canCreate = false;
                               // on verifie que l'attibut n'est deja pas present dans la base
                               try {
                                    Attributes attrs = ldapContext.getAttributes(distinguishedName);
                               } catch (NameNotFoundException e) {
                                    canCreate = true;
                               }
                               // si pas present
                               if (canCreate){
                                    ldapContext.createSubcontext(distinguishedName, newAttributes);
                                    
                          }

                          else{
                               
                          }
                          ldapContext.close();
                     }
                     catch (Exception e) {
                          System.out.println("create error: " + e);
                          e.printStackTrace();
                          System.exit(-1);
                     }
           }



      what i need to add in my code to create an Exchange mailbox

      thank you in advance
        • 1. Re: how to create exchange 2007 mailbox with jndi
          775730
          no one has ever done this operation ???
          • 2. Re: how to create exchange 2007 mailbox with jndi
            EJP
            No. Is it an LDAP operation? If it is, you clearly know how to program JNDI, it's just a matter of knowing what to create in the LDAP tree ... which is an Exchange question, not a Java question, so you're asking in the wrong place.

            I do want to comment on something else. Your technique of testing for existence first is incorrect, as it is non-atomic and therefore vulnerable to timing window problems. You should just attempt the creation and catch the NameAlreafyBoundException or whatever it is. You have to catch exceptions anyway, why add extra code? In general the best way to test the correctness of an operation is to attempt it, not to try something else first.
            • 3. Re: how to create exchange 2007 mailbox with jndi
              775730
              I have tested this code before posting on this forum, it is true that exception handling must be improved .....

              Returning to my question, I understand the creation of Exchange mailboxes is not possible with JNDI, right what you mean .....
              • 4. Re: how to create exchange 2007 mailbox with jndi
                EJP
                I understand the creation of Exchange mailboxes is not possible with JNDI, right what you mean .....
                That's not what I meant and it's not what I said. I didn't say that. I asked it. Your question assumes it is possible. I don't know. This isn't a Microsoft Exchange forum. You're asking the question in the wrong place. If it can be done via LDAP it can be done via JNDI. Responders here can tell you practically anything you need to know about JNDI, and practically nothing about Exchange.
                • 5. Re: how to create exchange 2007 mailbox with jndi
                  matfud
                  I agree with EjP.

                  If you can not do it via JNDI . Well that sounds strange.

                  Perhaps you should look to the command line utilities that exchange provides for a simpler solution? Call them from Java if you must. That would likely take away some of your suffering.

                  If not have a surf around the net to see if there is a library already there to perform the operations you want. I would be surprised if no one had come across this problem before.

                  matfud
                  • 6. Re: how to create exchange 2007 mailbox with jndi
                    EJP
                    It doesn't sound like anybody here is agreeing with me frankly. I have no idea whether this can be done via LDAP or not. Neither does anybody else as far as I can see. I'm just querying the assumption. If you can do it via LDAP you can do it via JNDI. But it's still just an 'if'.
                    • 7. Re: how to create exchange 2007 mailbox with jndi
                      775730
                      it is true that the exchange 2007 shell provides commands to create mailboxes, but I do not know if I could incorporate these commands in my java code, at least I'll try and get back to you
                      • 8. Re: how to create exchange 2007 mailbox with jndi
                        matfud
                        Again I agree with EjP.
                        I have no idea how exchange works so all I can do is poke questions. If you think this is something that "You must be able to do" then it is likely someone has already solved that problem. However I have come across many of these were nobody has felt the need to explain how they did it. If it is not available via JNDI then you can use the command line utils as I suggested or failing that you can drop to JNI and code in C.

                        It is always possible to do but are you using the correct tool for the job?


                        matfud