First of all, this has nothing at all to do with JSP. Even putting that code in JSP is a bad practice, it should go in a
servlet or a helper class, but your question still isn't about servlets or helper classes, it's about JavaMail. So it belongs in the JavaMail forum.