Free-electron beams are versatile probes of microscopic structure and composition(1,2), and have revolutionized atomic-scale imaging in several fields, from solid-state physics to structural biology(3). Over the past decade, the manipulation and interaction of electrons with optical fields have enabled considerable progress in imaging methods(4), near-field electron acceleration(5,6), and four-dimensional microscopy techniques with high temporal and spatial resolution(7). However, electron beams typically couple only weakly to optical excitations, and emerging applications in electron control and sensing(8-11) require large enhancements using tailored fields and interactions. Here we couple a free-electron beam to a travelling-wave resonant cavity mode. The enhanced interaction with the optical whispering-gallery modes of dielectric microresonators induces a strong phase modulation on co-propagating electrons, which leads to a spectral broadening of 700 electronvolts, corresponding to the absorption and emission of hundreds of photons. By mapping the near-field interaction with ultrashort electron pulses in space and time, we trace the lifetime of the the microresonator following a femtosecond excitation and observe the spectral response of the cavity. The natural matching of free electrons to these quintessential optical modes could enable the application of integrated photonics technology in electron microscopy, with broad implications for attosecond structuring, probing quantum emitters and possible electron-light entanglement.