Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm programming a email sending program in C# and .NET 4.
The user gives his email address, and also the email address from the
reciever.

How can I retrieve the SMTP server from the users email address?

For example:
The user gives "user7712279@gmail.com". And I need to find the SMTP server "smtp.gmail.com". But for Yahoo, Hotmail, and other email services, it must work also. How can I do that?

Thanks in advance.
Posted
Comments
Richard MacCutchan 3-Nov-12 7:52am    
I do not think that information is provided in the email message. Take a look at the source of some messages, or check the SMTP specifications.
TorstenH. 3-Nov-12 8:43am    
WHY do you want that information? That's not unusual.
I expect you to have a false approach on something.
Thomas Daniels 3-Nov-12 9:51am    
I need to send a email with a SmtpClient in C#. But to do that, I need to set the Host property of the SmtpClient. So, I need to retrieve the SMTP host from the given email address.
TorstenH. 3-Nov-12 10:55am    
The host is a basic setting, that one needs BEFORE starting to send messages. You are sending the messages to your own host - not to the foreign host.
I recommend to make some preference dialogs for settings like this. You will also need to know the username and password for retrieving messages.


1 solution

You can't do this. You can lookup the MX record for the domain but that is the inbound mail exchanger and, quite simply, it won't accept mail from your program. If it did, every spammer in the world would do what you are asking to do. Mail servers (like GMail, Yahoo, etc) want things like reverse lookups and properly configured servers before they will consider them legit. Even setting up an SMTP server on your home connection to relay through is still likely to get your messages bounced.

As the others have pointed out in the comments, you must send e-mail through a fully-qualified SMTP server. That server will figure out the remote mail system and will do all of the dirty work to get the message delivered. The server (host), the userID, and password would all be settings the end user would need to provide your application.

Alternately, there are some third party libraries, like the Redemption library, that will use the built-in Outlook (or MS Mail) settings on a Windows machine. This is convenient if you want to send mail as the current user and the user has e-mail setup in Windows. Some of these APIs may have changed in Win 8... I don't know... but I have used the Redemption library through Win 7 and it kicks the crap out of everything else.

And not to be critical, but there are lots of tutorials out there on SMTP describing how it works and how mail, in general, is sent and received. All of the behind-the-scenes stuff you should probably brush up on before going further on this. Unless you follow the rules, your messages will get bounced and you won't know why.
 
Share this answer
 
Comments
Thomas Daniels 3-Nov-12 13:52pm    
Ok, thanks!
TorstenH. 3-Nov-12 18:12pm    
+5!
to add in: please test your code with a local SMTP Server. HMail Server is free and can be driven completely local.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900