Yes, you can sell the shares on the ex-dividend date and the record date and still receive dividends.