The efficient initialization of a quantum system is a prerequisite for quantum technological applications. Here we show that several classes of quantum states of a harmonic oscillator can be efficiently prepared by means of a Jaynes-Cummings interaction with a single two-level system. This is achieved by suitably tailoring external fields which drive the dipole and/or the oscillator. The time-dependent dynamics that leads to the target state is identified by means of Optimal Control Theory (OCT) based on Krotov’s method. Infidelities below 10?4 can be reached for the parameters of the experiment of the ENS group in Paris, where the oscillator is a mode of a high-Q microwave cavity and the dipole is a Rydberg transition of an atom. For this specific situation we analyze the limitations on the fidelity due to parameter fluctuations and identify robust dynamics based on pulses found using ensemble OCT. Our analysis can be extended to quantum-state preparation of continuous-variable systems in other platforms, such as trapped ions and circuit QED.