If you you change date and time settings , read and write permissions in some folders guest , shared these problems will appear .
Open mail app - click on window on the top menu bar - connection doctor , click on red dot in front for IMAP effected account it could be your gmail services or iCloud , it will prompt to enter the mail address and password .
suppose it doesn't work straight away delete both accounts from system preferences - internet accounts and then configure it once again see this article Add or remove email accounts in Mail on Mac - Apple Support
But when you again set up iCloud account in the upcoming steps iCloud verification code will be required , so if that is not in records generate a new one as it will help to set up iCloud Keychain .
See this method .
( a ) Open system preferences > iCloud > the box for keychain is checked , click on options , click on change security code .
( b ) Click on security code > enter new code in the fields and click on next , you have to enter the new code for the second time and click on next .
In the next window the country code is written , enter your trusted mobile number …….and click on done .
Again enter your Apple ID password to update your account settings and click on ok , hence the code is changed , so write down this code in a safe place .
A second method that is more effective In which each mail is saved in V6 folder used in Mac OS Mojave 10.14 , for Mac OS High Sierra it is V5 folder .
See the steps -
1 ) Enter in user library ( click on finder take cursor on top menu bar > Go then Hold option key and click on library ) > select mail and right click on copy mail and then right click on empty desktop and click on paste item , the V5 folder will be saved on the desktop .
( 2 ) Open mail application and take cursor on top menu bar > file > import mail boxes > import data > select apple mail > click on continue > select desktop > click on continue > the data will start importing and the bar will start loading and let it be completed .
( 3 ) Go to system preferences > internet accounts > select the web mail account and click on minus sign it will be deleted .
( 4 ) You can see the mail data is transferred in the bottom mail side bar but everything is empty there are no mails in sent , inbox or archive they all are empty but a folder " import " is visible click on drop down arrows of each folders .
( 5 ) And if you plan to switch from yahoo to google or even change the internet servicer provider the folder " import " is still saved in the empty mail application .
( 6 ) When you configure the mail account by taking cursor on top menu bar > click on add account > choose a mail provider and select the box and click on continue > add new web mail address > click on next > and type the password > and click on done you will be joined to the new server say yahoo ( that is still empty for your mail account as you recently joined this new server and no data is saved on their server , but still the mail folder " import " is in the mail application .
You can scatter the mails in the desired mail boxes .
Note : While performing this step you will notice the fans will start running and the system will heat up but this is normal as the the data is transferred , and it will take time to stop the fans and system will cool down .