Correction: Robert E. Lee did not believe secession was needed, and opposed it until Virginia left. He never felt it was unlawful.
Lee was opposed to secession and frequently derided the south for having done so.
Regardless, Robert E. Lee was a traitor to his country. When he took his commission in the army, he swore to protect his COUNTRY—not his state—from all enemies foreign and domestic...
A lot of criminals feel that their actions are not criminal...
From a January 1861 letter to his son Custis:
"Secession is nothing but revolution. The framers of our Constitution never exhausted so much labor, wisdom and forbearance in its formation, and surrounded it with so many guards and securities, if it was intended to be broken by every member of the Confederacy at will. It was intended for 'perpetual union' so expressed in the preamble, and for the establishment of a government, not a compact, which can only be dissolved by revolution, or the consent of all the people in convention assembled. It is idle to talk of secession."
It's pretty clear that Lee did think it was illegal. He may have thought that Virginia had no other choice, but that's not the same as believing the actions were lawful.