It should be:
He said, "I did not write a letter".
(note position of space)
But in a narrative often this style would be used:
He said, "I did not write a letter."
(note position of full stop).
Personally I see no logical justification for the comma, but traditionally you see it used like this.