Human milk oligosaccharides could prevent pathogenic bacterial infections in neonates; however, direct in vivo antiinfection evidence was still lacking. Here, we systematically evaluated the effects of 2′-fucosyllactose (2′-FL) and 3′-sialyllactose (3′-SL) on the structural development and functional maturation in neonates and their defense against enteroaggregative Escherichia coli infection. It was found that supplementation with 2′-FL and 3′-SL improved the resistance of weaned mice to enteroaggregative E. coli. The mechanism related to the promotion of 2′-FL and 3′-SL in the maturation of intestinal mucosal immunity by promoting stem cell differentiation, mucus layer integrity, and tight junction formation. 2′-FL and 3′-SL significantly increased the ratio of Th1 and Treg cells in the lamina propria, contents of short-chain fatty acids, as well as the serum content of IgA. This study lays a theoretical basis for the application of 2′-FL and 3′-SL in infant formula, as well as the development of intestinal health products.