How can we become immune to an infectious disease?
The first time a white blood cell meets a pathogen, it takes time to make antibodies to destroy it. The next time that pathogen enters, antibodies can be made fast so we don't get ill.
When a white blood cell meets the dead/inactive pathogen it reacts as if it is harmful, and produces antibodies. These antibodies can then be made quickly in future.
Edward Jenner developed the smallpox vaccine when he noticed that milkmaids who had the mild infection of cowpox never got the dangerous smallpox infection.
Very successful as long as enough people are vaccinated. Smallpox has been completely eradicated due to vaccination. The last death from smallpox was 1978.