Preparation of graded-index optical microcavities based on porous silicon is demonstrated, and chemical modifications for obtaining improved stability under alkaline conditions are studied. Four surface modification methods for stabilizing the samples are examined, and the effects on the optical properties are verified. Two different thermal carbonization treatments resulting in hydrophilic and hydrophobic surfaces are employed. In addition, modification with undecylenic acid is performed on as-prepared and thermally hydrocarbonized porous silicon surfaces. Stability and sensing capabilities of the modified samples are examined by subjecting them to different concentrations of methylamine and trimethylamine vapors. Vapor induced changes in the reflectance spectra are used for evaluating sensitivity and stability. Sensitivity towards ethanol vapor is also measured in order to compare the sensitivity to a normal organic solvent. The results show that the two carbonization treatments and the undecylenic acid functionalization of the hydrocarbonized surface result in greatly improved stability. In contrast, derivatization of as-prepared porous silicon with undecylenic acid does not protect the surface sufficiently against oxidation under the highly basic conditions produced by the amine vapors. Surface chemistry is also shown to have a large effect on sensitivity towards the examined vapors. X-ray photoelectron spectroscopy was used to assess changes in elemental composition of sample surface. The results suggest that thermally promoted addition of undecylenic acid on hydrocarbonized porous silicon is an effective method for producing highly stable optical structures with a carboxyl group functionalization.