Not all 50 states, but actually 49 out of the 50 – and this surprisingly happened twice. The first time happened in the 1972 presidential election between Richard Nixon and George McGovern. Nixon received 520 electoral votes, while McGovern only received 17, which was Massachusetts and Washington D.C.
The second time was in the 1984 presidential election between Ronald Reagan and Walter Mondale. Reagan received 525 electoral votes while Mondale went away with 17 from Minnesota and Washington D.C.
Both times these were Republican candidates and the winners had both previously served their first term as President.