What is the purpose of using the word "why" in "why, thank you"?

Solution 1:

It is definitely expressing mild surprise. Imagine you're giving a dinner party. You turn to the person sitting at the table next to you and ask them to pass the salt. They do. You say "Thank you." You wouldn't say "Why, thank you.", because there's no surprise, you were fully expecting them to pass it to you. But if someone says "I must compliment you, this stew is delicious!", you might well say "Why, thank you!", because you weren't expecting the compliment, it was a bit of a surprise. You could still say just "Thank you." in this case, but you wouldn't really ever say "Why, thank you." in the first case.

Solution 2:

Why is used here as an interjection. According to Merriam-Webster:

—used to express mild surprise, hesitation, approval, disapproval, or impatience <why, here's what I was looking for>

In my experience, the extra why in Why, thank you is used mainly to avoid appearing too abrupt in one's thankfulness.