Hydroxylated derivatives of vitamin D3 (cholecalciferol) and vitamin D2 (ergocalciferol) are effective for preventing or treating mastitis in animals. The hydroxylated derivatives of vitamins D3 or D2 are administered to the mammary gland of a female animal in an amount effective to inhibit or significantly reduce the growth of bacteria in the animals mammary gland.