After seeing his mother remarry so quickly after his father's death, Hamlet is traumatized to the point where he begins to distrust everyone, especially women. Earlier in the play he treats Gertrude unkindly, complaining that she is weak and lustful. In the following soliloquy goes as far as saying, "Frailty thy name is woman."
This suggests that his sense of trust his hurt, and he feels a deep sense of abandonment. As the previous answerer already mentioned, in Hamlet's mind Ophelia has betrayed him by sharing his private love letters with her father. His reaction, however, is unquestionably disproportional to her action. He treats her very cruel tells her to "get thee to a nunnery."
In his mind this is about more than the letters. He is angry at his mother for his actions, and feels a sense of abandonment after yet another person he loves as done something he did not like.