Searches for dark matter decaying into photons constrain its lifetime to be many orders of magnitude larger than the age of the Universe. A corollary statement is that the abundance of any particle that can decay into photons over cosmological timescales is constrained to be much smaller than the cold dark-matter density. We show that an irreducible freeze-in contribution to the relic density of axions is in violation of that statement in a large portion of the parameter space. This allows us to set stringent constraints on axions in the mass range 100 eV-100 MeV. At 10 keV our constraint on a photophilic axion is gaγγ ≲8.1×10-14 GeV-1, almost 3 orders of magnitude stronger than the bounds established using horizontal branch stars; at 100 keV our constraint on a photophobic axion coupled to electrons is gaee≲8.0×10-15, almost 4 orders of magnitude stronger than the present results. Although we focus on axions, our argument is more general and can be extended to, for instance, sterile neutrinos.