In millimeter-wave (MMW) multiple-input multiple-output (MIMO) communications, users and their corresponding base station (BS) have to align their beam during both initial access and data transmissions to compensate for the high propagation loss. The beam alignment (BA) procedure specified for 5th Generation (5G) New Radio (NR) has been designed to be fast and precise in the presence of non-malicious interference and noise. A smart jammer might exploit this weakness and may launch an attack during the BA phase in order to degrade the accuracy of beam selection and, thus, adversely impacting the end-to-end performance and quality-of-service experienced by the users. In this paper, we study the effects of a jamming attack at MMW frequencies during the BA procedure used to perform initial access for idle users and adaptation/recovery for connected users. We show that the BA procedure adopted in 5G NR is extremely vulnerable to a smart jamming attack and, consequently, we propose a countermeasure based on the idea of randomized probing, which consists of randomly corrupting the probing sequence transmitted by the BS in order to reject the jamming signal at the UE via a subspace-based technique based on orthogonal projections and jamming cancellation. Numerical results corroborate our theoretical findings and show the very satisfactory accuracy of the proposed anti-jamming approach.