3 Replies Latest reply on Oct 22, 2012 7:19 PM by Alex B

    Programmatic mail-session configuration

    Alex B Newbie

      i currently have the following working

      {code}

          public static void createMailSession(ModelControllerClient client, String sessionJndi) throws IOException {

              ModelNode request = new ModelNode();

              request.get(ClientConstants.OP).set(ClientConstants.ADD);

              request.get(ClientConstants.OP_ADDR).add("subsystem",

                      "mail");

              request.get(ClientConstants.OP_ADDR).add("mail-session",

                      sessionJndi);

       

       

              request.get("jndi-name").set(sessionJndi);

       

       

              ModelNode result = client.execute(new OperationBuilder(request).build());

       

       

              String outcome = result.get(ClientConstants.OUTCOME).asString();

              String description = "";

              if (outcome.equalsIgnoreCase("failed")) {

                  description += " ,Reason: " + result.get(ClientConstants.FAILURE_DESCRIPTION).asString();

              }

              System.out.println("Adding MailSession: " + sessionJndi + " Result: " + outcome + description);

          }

       

      {code}

       

      This creates a

      {code:xml}

       

      <mail-session jndi-name="java:/Mail"/>

      {code}

       

      However, I am not sure how to add a

      {code:xml}

      <smtp-server outbound-socket-binding-ref="mail-smtp">

                          <login name="name" password="pass"/>

                      </smtp-server>

       

      {code}

       

      To that mail-session.

        • 1. Re: Programmatic mail-session configuration
          Tomaz Cerar Master

          Hi,

           

          you are close first you added session, now you also need to add server.

           

          public static void createMailSession(ModelControllerClient client, String sessionJndi) throws IOException {
                  ModelNode requestSession = new ModelNode();
                  requestSession.get(ClientConstants.OP).set(ClientConstants.ADD);
                  requestSession.get(ClientConstants.OP_ADDR).add("subsystem","mail");
                  requestSession.get(ClientConstants.OP_ADDR).add("mail-session",sessionJndi);
                  requestSession.get("jndi-name").set(sessionJndi);
                  
                  ModelNode requestServer = new ModelNode();
                  requestServer.get(ClientConstants.OP).set(ClientConstants.ADD);
                  requestServer.get(ClientConstants.OP_ADDR).add("subsystem","mail");
                  requestServer.get(ClientConstants.OP_ADDR).add("mail-session",sessionJndi);
                  requestServer.get(ClientConstants.OP_ADDR).add("mail-server","smtp");
                  requestServer.get("outbound-socket-binding-ref").set("mail-smtp");
                  requestServer.get("username").set("some-user-name");
                  requestServer.get("password").set("password");
                  
                  ModelNode result = client.execute(new OperationBuilder(requestSession).build());
                  ModelNode result2 = client.execute(new OperationBuilder(requestServer).build());
          
          ...
                  String outcome = result.get(ClientConstants.OUTCOME).asString();
                  String description = "";
                  if (outcome.equalsIgnoreCase("failed")) {
                      description += " ,Reason: " + result.get(ClientConstants.FAILURE_DESCRIPTION).asString();
                  }
                  System.out.println("Adding MailSession: " + sessionJndi + " Result: " + outcome + description);
              }
          

           

          hope this helps,

          tomaz

          • 2. Re: Programmatic mail-session configuration
            Alex B Newbie

            This doesn't seem to be working it  still creates an empty "mail-session"

            • 3. Re: Programmatic mail-session configuration
              Alex B Newbie

              requestServer.get(ClientConstants.OP_ADDR).add("mail-server","smtp");

               

              must be

               

              requestServer.get(ClientConstants.OP_ADDR).add("server","smtp");

               

              then it works

              1 of 1 people found this helpful