Free-electron beams serve as uniquely versatile probes of microscopic structure and composition 1,2 , and have repeatedly revolutionized atomic-scale imaging, from solid-state physics to structural biology [3][4][5] . Over the past decade, the manipulation and interaction of electrons with optical fields has seen significant progress, enabling novel imaging methods 6 , schemes of near-field electron acceleration 7,8 , and culminating in 4D microscopy techniques with both high temporal and spatial resolution 9,10 . However, weak coupling strengths of electron beams to optical excitations 11,12 are a standing issue for existing and emerging applications of optical free-electron control. Here, we demonstrate phase matched near-field coupling of a free-electron beam to optical whispering gallery modes of dielectric microresonators. The cavity-enhanced interaction with these optically excited modes imprints a strong phase modulation on co-propagating electrons, which leads to electron-energy sidebands up to hundreds of photon orders and a spectral broadening of 700 eV. Mapping the near-field interaction with ultrashort electron pulses in space and time, we trace the temporal ring-down of the microresonator following a femtosecond excitation and observe the cavity's resonant spectral response. Resonantly enhancing the coupling of electrons and light via optical cavities, with efficient injection and extraction, can open up novel applications such as continuous-wave acceleration, attosecond structuring, and real-time all-optical electron detection.