An environmentally friendly and facile method for the preparation of 1,8-dioxooctahydroxanthene between aldehydes with dimedone or cyclohexane-1,3-dione is presented in the presence of trichloromelamine (TCM) under solvent-free conditions. Also, for the first time the corrosion inhibitive performance of some 1,8-dioxooctahydroxanthene derivatives on mild steel in 0.1 M HCl was investigated using AC and DC electrochemical techniques and a quantum chemical method. An efficiency of more than 97% was obtained with 1 mmol L−1 1,8-dioxooctahydroxanthene. Density functional calculations showed that the lone pair electrons of oxygen in the structure of 1,8-dioxooctahydroxanthene are suitable sites to interact with the metal. Finally in the presence of 1,8-dioxooctahydroxanthene, a decrease in surface roughness and corrosion attacks were demonstrated by microscopy examination.