After trying many solutions from Google and StackOverflow. I came to know that my code is correct the issue was with SMTP credentials. I have used other SMTP credentials and it worked.