Heat stress and mastitis are major economic issues in dairy production. The objective was to test whether goat’s mammary gland immune response to E. coli lipopolysaccharide (LPS) could be conditioned by heat stress (HS). Changes in milk composition and milk metabolomics were evaluated after the administration of LPS in mammary glands of dairy goats under thermal-neutral (TN; n = 4; 15 to 20 °C; 40 to 45% humidity) or HS (n = 4; 35 °C day, 28 °C night; 40% humidity) conditions. Milk metabolomics were evaluated using 1H nuclear magnetic resonance spectroscopy, and multivariate analyses were carried out. Heat stress reduced feed intake and milk yield by 28 and 21%, respectively. Mammary treatment with LPS resulted in febrile response that was detectable in TN goats, but was masked by elevated body temperature due to heat load in HS goats. Additionally, LPS increased milk protein and decreased milk lactose, with more marked changes in HS goats. The recruitment of somatic cells in milk after LPS treatment was delayed by HS. Milk metabolomics revealed that citrate increased by HS, whereas choline, phosphocholine, N-acetylcarbohydrates, lactate, and ß-hydroxybutyrate could be considered as putative markers of inflammation with different pattern according to the ambient temperature (i.e. TN vs. HS). In conclusion, changes in milk somatic cells and milk metabolomics indicated that heat stress affected the mammary immune response to simulated infection, which could make dairy animals more vulnerable to mastitis.