From Catholic Online website-
Prayer as an Aid to Chastity
But above all, to acquire the virtue of chastity prayer is necessary: it is necessary to pray, and to pray continually. It has already been said that chastity can neither be acquired nor preserved unless God grant His aid to preserve it; but this aid He gives only to those who ask it.
We should then, says St. Cyprian, instantly resist the first carnal solicitations with which the devil assails us, and not permit the serpent, that is, the temptation, to grow strong. St. Jerome gives this same advice: "You must not permit bad thoughts to grow in your mind; no, kill the enemy when he is small." It is easy to kill a lion when he is small, but not when his has grown to his full size.
Let us guard against reasoning with temptations contrary to chastity: let us endeavor instantly to banish them. And, as the spiritual masters teach, the best means of banishing such temptations is not to combat them directly face to face, by making contrary acts of the will, but to get rid of them indirectly by acts of the love of God, or of contrition, or at least by turning the mind to other things.
But the means in which we should place the greatest confidence is prayer, and recommending ourselves to God. It is useful, as soon as we perceive the first motion of impurity, to renew our purpose to suffer death rather than consent to sin, and immediately after to have recourse to the wounds of Jesus Christ for aid.
It is also very useful to make the sign of the cross on the breast, and to have recourse to our angel guardian and our holy patron. But above all, it is useful to have recourse to Jesus Christ and the divine Mother by instantly invoking their most holy names, and by continuing to invoke them until the temptation is beaten down. Oh! how powerful are the most holy names of Jesus and Mary against the attacks of impurity!
Devotion to the holy Virgin, who is called "the Mother of fair love, and the guardian of virginity," is a most useful devotion for the preservation of chastity. And to recite, at rising in the morning and going to bed at night, three "Hail Mary's," in honor of the purity of Mary, is a devotion that has singular efficacy in obtaining the gift of continence.