Introduction
One of the background parts of most systems I write involves messaging of some kind ... more often than not, the default is email notification of one thing or another. When I need to carry out large-scale mail-outs I always use one of the major Azure services, but when I only need ad-hoc mail operations or low volume, I use a combination of an SMTP Client and a DNS MX Lookup. MX what? ..,. Lookup! he said, Lookup! :)

Quick explanation: Normally, our message gets sent from the sender to the sender's mail server, and it takes care of everything ... this is from A -> B -> C -> D above. Our alternative is to dispose of the intermediate server at 'B', and go straight to 'C' ourselves.
side-note 1: if you are short on time and don't want the background to understand whats happening, just download the attached code; it's reasonably self-explanatory.
side-note 2: while I use this code on Azure, it should also work on other cloud providers and also standard Windows applications as well.
Background
When we send an email to say our friend '[email protected]', we generally hand it over to an SMTP server to send onward for us. But how does it actually get sent on and what happens to it along the way? ... Many years ago, I had the serious fun writing a custom MailServer - this involved creating both a POP3 and an SMTP server. In the process of building them, I learned about the protocols used in mail exchange and learned about the MX or 'Mail Exchange' record that is stored in a DNS server. When you open your browser and enter 'CodeProject.com' as the address to go to, one of the things your system does is go to its local DNS (domain name server) and asks 'where is the actual IP address of this server/resource that the website CodeProject.com resides? ..... Depending on what the system asks for, it will return simply the IP address of the web-server, or other information as well. The DNS stores not only information about web-server services, but also about all kinds of other things for DNS based network communication. A DNS record can have many entries, here are some examples of different types and their general use:
- 'A' record .. returns a 32-bit IPv4 address
- 'CNAME' .. returns an alias to another record
- 'MX' .. returns a list of 1:n servers that specifically handle mail
(if you want to poke around, here is a list of different DNS record types)
The reason we are interested in the MX or Mail Exchange record is that it will allow us to bypass the need for an SMTP server to send our message and go directly to the receiving target email server. To understand how we can do this, lets first quickly look at the protocol used for sending an email and then how this is used by an SMTP server to manage mail transport.
MX Record lookup
As a mail-server, when we have a message we want to send, we need to find out where to send it - just because someone has an 'outlook.com' address does not mean you send it directly to the server that hosts the main domain. To lookup the MX record, we need to send the root domain in as a query to a DNS server and extract the MX record from the answer. In the case of most large mail hosts, there will be multiple mail servers returned. The list you receive may not be consistent, and the order of the servers listed may also change - this assists with load balancing among other things.
For a visual look at how this works, I will use the DNS query tool at the 'Network Tools' website, in particular, the NSLOOKUP service: http://network-tools.com/nslook/
Enter a query for CodeProject.com as follow.

Now, let's execute and see the first result we get, take note, in particular, the first entry, the other alternatives are given, and the order in which they are listed...

Now, let's do exactly the same thing again and notice the changes... I have marked the *previous* order/positions in red.

The general process that happens is a mail server carries out an MX lookup, gets a list of results back (like above), then it iterates through the list, connecting to each listed mail-server in turn until one successfully accepts the message.
The important part we are interested in here is the 'exchange' part of the MX record. This tells us where the server is that will accept a message for the root domain we are trying to connect to (in this case, CodeProject.com).
SMTP Protocol
Once we know the mail-server we need to connect to, we then need to have a consistent method to connect to the server and send our message - we do this using an SMTP message protocol.
SMTP stands for 'Simple mail transfer protocol'. It was first developed back in the prehistoric ages of 1982 - you can read it in its glory on the IEFT Webpage for RFC 821. Mail servers use SMTP to transmit and receive messages, whereas email clients typically use SMTP to forward mail to the server, and then use either POP3 or IMAP to receive messages from a server. The SMTP protocol is a 'chat' between two endpoints, it goes kind of like this...
Client: 'Hey, I have a message for someone on your server....'
Server: 'Great, let me have it!'
Client: 'Here you go....<sends message>'
Server: 'Got it thanks, I will deliver it asap, good-bye and thanks for calling!'
Client: 'Cool, Cya!'
The protocol has an agreed standard for connecting the two endpoints, for passing over the message, and closing the connection again. You can actually send an email message from the command-line using the inbuilt windows application 'telnet' if you wish! ... let's take a little side-trip and look at that, just because it's interesting!
Telnet is a command-line utility that allows you to connect to a remote computer. Its generally turned off by default in Windows - to turn it on, open your control panel, then add/remove programs and 'turn windows features on or off' - in here looking for the 'telnet client' - confirm the checkbox is ticked and close the modal window - it should install and you are good to go.

To use telnet and connect to a mail-server, open a command window. At the prompt, enter telnet, followed by the 'exchange' name you are trying to contact, followed by the port number of the mail server. This is usually port 25.
telnet aspmx3.googlemail.com 25
When connected, you will see something like the following,

We are connected to the mail server and it is awaiting our first command. We tell it who we are by saying 'Helo' (no, it's not a typo!), and announcing who we are...





Robo TeachPosted May 6, 2019, 5:43 AM
Hello, I hope you're doing fine. I've tried your code but it seems I can't send a message. I'm getting this error System.Net.Sockets.SocketException: 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 121.54.24.218:25'
Tushar DikshitPosted Jun 13, 2018, 2:28 PM
Nice, thanks for sharing.
Sagar Pandurang KapPosted Feb 5, 2018, 11:19 PM
Awesome stuff..Keep sharing....