Dogs get heartworm disease via a bite from an infected mosquito. The microfilariae, or baby worms, spread and mature within the dog. Heartworm disease is not contagious, meaning that a dog cannot catch the disease from being near an infected dog. Heartworm disease is only spread through the bite of a mosquito. Inside a dog, a heartworm’s lifespan is 5 to 7 years.
More information can be located at Heartworms in Dogs: Facts and Myths (webmd.com)