We introduce a Hamiltonian dynamics for the description of long-range interacting systems in contact with a thermal bath (i.e., in the canonical ensemble). The dynamics confirms statistical mechanics equilibrium predictions for the Hamiltonian mean field model and the equilibrium ensemble equivalence. We find that long-lasting quasistationary states persist in the presence of the interaction with the environment. Our results indicate that quasistationary states are indeed reproducible in real physical experiments.