To be a heretic, one must publicly deny a doctrine of the faith, or act in some way which denies a doctrine (e.g., baptizing only in the name of Jesus, and not the Trinity), and then be obstinate in the denial after educative reproof by his superiors.
You seem to be implying that a person is only a heretic if they say it publicly. This is incorrect. Per the CCC, "Heresy is the obstinate post-baptismal denial of some truth which must be believed with divine and catholic faith, or it is likewise an obstinate doubt concerning the same." A person is a heretic once they knowingly choose to deny OR DOUBT any matter of defined dogma. With dogmas that have been long defined, there is no need whatsoever for any correction or reproof by their superiors, only that they be aware that the Church teaches such a thing. Anyone who does not believe in transubstantiation is a heretic.
So if this "intention" or "belief" is private ("occult", known only to himself and/or a few others), the Mass will be valid.
But if this "intention" or "belief" is creating public scandal to the point of formal excommunication, the his "mass" is not valid.
You don't seem to be implying the contrary here, but just for clarification, excommunicated persons, even if excommunicated for heresies which do not touch the sacrament in question, can still say valid masses (or whatever sacrament). For example, as far as I know the masses of the Orthodox are valid.