# Sending Messages

Messages can be sent to a counter party with the static Session::sendToTarget methods. This method has several signatures. They are:

See this code in C#, VB.NET, PYTHON, RUBY

        // send a message that already contains a BeginString, SenderCompID, and a TargetCompID
        static bool sendToTarget( Message&, const std::string& qualifier = "" )
            throw(SessionNotFound&);

        // send a message based on the sessionID, convenient for use
        // in fromApp since it provides a session ID for incoming
        // messages
        static bool sendToTarget( Message&, const SessionID& )
          throw(SessionNotFound&);

        // append a SenderCompID and TargetCompID before sending
        static bool sendToTarget( Message&, const SenderCompID&, const TargetCompID&, const std::string& qualifier = "" )
          throw(SessionNotFound&);

        // pass SenderCompID and TargetCompID in as strings
        static bool sendToTarget( Message&, const std::string&, const std::string&, const std::string& qualifier = "" )
          throw(SessionNotFound&);

# Creating Messages

As for creating messages, just like reading them, there are several levels of type safety. And just like before, the recommended usage is the type safe method.

## Least Type Safe

Once again. This should only be used for low level interface to other languages and middleware. do not use this for writing applications.

See this code in C#, VB.NET, PYTHON, RUBY

        void sendOrderCancelRequest()
        {
          FIX::Message message;
          // BeginString
          message.getHeader().setField(8, "FIX.4.2");
          // SenderCompID
          message.getHeader().setField(49, "TW");
          // TargetCompID, with enumeration
          message.getHeader().setField(FIX::FIELD::TargetCompID, "TARGET");
          // MsgType
          message.getHeader().setField(35, 'F');
          // OrigClOrdID
          message.setField(41, "123");
          // ClOrdID
          message.setField(11, "321");
          // Symbol
          message.setField(55, "LNUX");
          // Side, with value enumeration
          message.setField(54, FIX::Side_BUY);
          // Text
          message.setField(58, "Cancel My Order!");

          FIX::Session::sendToTarget(message);
        }

## More Type Safe

And here, by using field classes, we can clearify our code and add some type safety. Once again, this is something you usually use for code that needs to work with multiple messages types or multiple FIX versions.

See this code in C#, VB.NET, PYTHON, RUBY

        void sendOrderCancelRequest()
        {
          FIX::Message message;
          FIX::Header header& = message.getHeader();

          header.setField(FIX::BeginString("FIX.4.2"));
          header.setField(FIX::SenderCompID(TW));
          header.setField(FIX::TargetCompID("TARGET"));
          header.setField(FIX::MsgType(FIX::MsgType_OrderCancelRequest));
          message.setField(FIX::OrigClOrdID("123"));
          message.setField(FIX::ClOrdID("321"));
          message.setField(FIX::Symbol("LNUX"));
          message.setField(FIX::Side(FIX::Side_BUY));
          message.setField(FIX::Text("Cancel My Order!"));

          FIX::Session::sendToTarget(message);
        }

## Most Type Safe... DO THIS!

Finally, the highly recommended method is to use the type safe message classes. This should typically be the only way you should ever have to create messages. Here the constructor takes in all the required fields and adds the correct MsgType and BeginString for you. What's more, by using the set method instead of setField, the compiler will not let you add a field that is not a part of a OrderCancelRequest based on the FIX4.1 specs. Keep in mind you can still use setField if you want to force any field you want into the message.

See this code in C#, VB.NET, PYTHON(not supported), RUBY(not supported)

        void sendOrderCancelRequest()
        {
          FIX41::OrderCancelRequest message(
            FIX::OrigClOrdID("123"),
            FIX::ClOrdID("321"),
            FIX::Symbol("LNUX"),
            FIX::Side(FIX::Side_BUY));

          message.set(FIX::Text("Cancel My Order!"));

          FIX::Session::sendToTarget(message, SenderCompID("TW"), TargetCompID("TARGET"));
        }