Send email without SMTP Server (DNS Lookup)
This tutorial shows the algorithm of DNS lookup in Mail.dll email component,
which enables your application to send email without using your SMTP server.
In general, you should send email via specified SMTP server. You may ask: how does the specified SMTP server know what address this email should be sent to? The answer is simple: it queries MX record of recipient’s domain using DNS lookup. It then forwards this email to the SMTP server queried from DNS server.
If the recipient’s server doesn’t work fine or rejects the message, your SMTP server will send you an email to tell there was a failure in sending this email.
You can however remove the first step and just ask DNS MX record for the recipient’s SMTP server yourself.
You do this by using DnsHelper helper class:
// C# using Limilabs.Client.DNS; using Limilabs.Client.SMTP; using Limilabs.Mail; using Fluent = Limilabs.Mail.Fluent; string receipientSMTP = new DnsHelper().GetSmtpServer("pat@recipient.com"); using(Smtp client = new Smtp()) { client.Connect(receipientSMTP, 25); IMail email = Fluent.Mail.Text("Hello") .From("john@your_domain.com") .To("pat@recipient.com") .Create(); client.SendMessage(email); client.Close(); }
' VB.NET Imports Limilabs.Client.DNS Imports Limilabs.Client.SMTP Imports Limilabs.Mail Imports Fluent = Limilabs.Mail.Fluent Dim receipientSMTP As String = New DnsHelper().GetSmtpServer("pat@recipient.com") Using client As New Smtp() client.Connect(receipientSMTP, 25) Dim email As IMail = Fluent.Mail.Text("Hello") _ .From("john@your_domain.com") _ .[To]("pat@recipient.com") _ .Create() client.SendMessage(email) client.Close() End Using
Notice that we are using port 25. SMTP in plain text mode usually works on two ports: 587 – client STMP port, the one that is used by parameter-less Connect method and port 25 – used by servers (previously also used by clients).
Client port (587) may be disabled on servers advertised by MX DNS records – those must use port 25 and are not required to allow clients to connect.
There are two important things you should be also aware:
- This procedure is not what is popularly known as relaying.
- Most likely your IP address (the one from which you connect) does not designate a permitted sender for the sender’s domain (your domain, and yours domain MX record). This means that recipient’s SMTP server may refuse the message or can treat such message as spam.