Catalysis of the reaction: S-adenosyl-L-methionine + DNA containing CpN = S-adenosyl-L-homocysteine + DNA containing 5-MeCpN.