[IMP] mail: Improve code and test case to prevent failure of test case