There are many causes of infectious pneumonia
..viral, bacterial, and fungal. To get pneumonia you first have to be exposed. Usually this is from infected respiratory droplets from coughing/sneezing. Once you are exposed, whether you get infected or not depends on the amount of exposure, the virulence(ability to cause infection) of the bug, and the strength of your own immune system.