Structural imperfections, heteroatom dopants, and the interconnected pore structure of carbon materials have a huge impact on their electrochemical performance in lithium-ion and sodium-ion batteries due to the specific ion transport and the dominant storage mechanism at surface defect sites. In this work, mesopore-enriched nitrogen-doped carbon (NC) materials were produced with template-assisted chemical vapor deposition using calcium tartrate as the template precursor and acetonitrile as the carbon and nitrogen source. The chemical states of nitrogen, the volume of mesopores, and the specific surface areas of the materials were regulated by adjusting the synthesis temperature. The electrochemical testing of NC materials synthesized at 650, 750, and 850 °C revealed the best performance of the NC-650 sample, which was able to deliver 182 mA·h·g−1 in sodium-ion batteries and 1158 mA·h·g−1 in lithium-ion batteries at a current density of 0.05 A·g−1. Our study shows the role of defect sites, including carbon monovacancies and nitrogen-terminated vacancies, in the binding and accumulation of sodium. The results provide a strategy for managing the carbon structure and nitrogen states to achieve a high alkali-metal-ion storage capacity and long cycling stability, thereby facilitating the electrochemical application of NC materials.