Because it has cannabinoids (resin). Many plants produce resin, but only cannabis produces cannabinoids - aren't we lucky?
The resin serves two purposes, one is that it is sticky so when the male pollenates, the female will catch it and two once it has seeded, it acts to protect the seed from predators.