We present a general, semimicroscopic, self-consistent treatment of spontaneous emission for a two-level atom in a dielectric microcavity with arbitrary shape. Both lossless and absorbing media are considered. Our approach is based on linear-response theory and the coupled-dipole method. We illustrate our method on spherical and cubic cavities. We discuss the Purcell effect, the frequency shift, the local-field effect, and the dipole-dipole coupling experienced by a source placed in a microcavity.