The S&P 500 has surged to new highs recently, but some of the strongest companies that pay regular dividends are trading well off their highs. This has pushed their dividend yields up and signals they might be undervalued.

Here are two top dividend stocks to buy for the long term.

1. Realty Income

Realty Income (O -0.24%) is a top real estate investment trust (REIT) that has a 55-year record of paying dividends to investors. As a REIT, it is required to pay out at least 90% of its taxable income (excluding capital gains) to investors, and another attractive aspect of the company is that it pays dividends on a monthly schedule.

The first thing that stands out is that half-century-long dividend record. This means Realty Income has profitably operated its business through several recessions in the economy. This record ultimately reflects its diversification and quality tenant portfolio. Its portfolio totals more than 15,400 properties around the world. Its largest clients include industry leaders such as FedEx, Wynn Resorts, Walmart, and Home Depot.

Realty Income makes it a priority to invest in properties where clients are leaders or could become leaders in their industries. Management is clearly interested in sustainable cash flow generation, which helps sustain dividend payments over many years.

The company still sees a lot of opportunities to invest in new properties and grow the value of the business. Management raised its investment volume guidance to $3.5 billion for 2024 and expects to report full-year adjusted funds from operations per share between $4.17 to $4.21, representing an increase of nearly 5% at the midpoint of the range.

Realty Income currently pays a monthly dividend of $0.2635 per share. With the stock trading 28% off its recent highs, the forward yield has increased to 5.41%. This is a great time to consider adding shares. If interest rates stabilize or come down, there will be more demand for high-yield stocks, which could push Realty Income's share price higher over the next year.

2. Hershey

Hershey (HSY -1.98%) is the leading confectionary brand and also has a stable of leading snack foods like Skinny Pop in its arsenal. Brands like Reese's, Kit Kat, Jolly Rancher, and Hershey have been around a long time and aren't going anywhere, but Wall Street has discounted the value of the business significantly over recent weakness in discretionary spending. The stock is currently down about 35% from its previous peak.

Despite weak consumer spending this year, Hershey is on pace to post a full-year revenue increase of 1%, with adjusted earnings expected to be down 2% to $9.40, reflecting a spike in cocoa prices. Out of those earnings, Hershey is currently paying a quarterly dividend of $1.37 per share, bringing the forward yield to 3%.

This is a once-in-a-decade opportunity to add this quality business to your portfolio. Hershey's dividend yield has only reached 3% a few times in the last 20 years.

It's a good bet people are not going to lose their sweet tooth. They will still be buying chocolate decades from now. Statista values the confectionary market at $586 billion and it is estimated to grow at a compound annual rate of 5.4% through 2029.

Inflation has hit Hershey and its customers hard, but it won't always be like this. It's when a top business is wrestling with external issues in its industry or broader economy that investors can invest at a share price that undervalues the company's intrinsic worth. Investors will probably look back in a decade and see Hershey's high dividend yield as a bargain buying opportunity.