A Massachusetts federal judge on Thursday dismissed a civil defamation lawsuit brought against Bill Cosby by a woman who said he raped her in a hotel room in Detroit in 1974.

Mark G. Mastroianni, the United States District Court judge, said Mr. Cosby was within his rights to defend himself against the accusation made by the woman, Katherine Mae McKee, when one of his representatives in 2014 wrote a widely published letter questioning her credibility.

Ms. McKee filed the defamation suit in December 2015, saying the rebuttal had damaged her reputation. But in his ruling, Judge Mastroianni said the statements were protected by the First Amendment.

“Individuals publicly accused of misconduct cannot be held completely incapable of issuing any statement in response to the allegation, other than ‘no comment,’” he wrote. “They cannot be entirely chilled from navigating, at their own peril, what may be viewed as a web of defamation law to produce a responsive statement that does not subject themselves to liability.”