I have to declare first that I have never gotten a mental illness, but I can give some thoughts about this topic.
We can see many people having mental problems at any gender and at any age. It's both really sad for them and us.
I am not a medical worker, so I don't really know the reason which caused these problems, but I guess if it's not caused by brain tissues, it is mostly caused by people's mentality.
If people are open minded, kind, friendly and generous, they hardly seem to get mental illness, otherwise, they are easy to.
So, the best way to avoid getting mental illness is to build a strong mental power and be open minded to everything.
Additionally, giving is always happier than taking and I think it's another good method to keep from mental illness.