Send email directly (Simulating SMTP server)


In general, we send email via specified SMTP server. How does the specified SMTP server know what address this email should be sent to? The answer is... it queries MX record of recipient's domain via DNS lookup. It then forwards this email to the SMTP server queried from DNS server. If recipient's server doesn't work fine, sender's SMTP server will send a failure-delivery report to the sender telling it failed to send out the email.

How does EASendMail SMTP component work with "Send email Directly"? Firstly, it queries MX record for recipient address from DNS, then sends email to recipient's email server directly. In short, if no SMTP server is specified in the code, EASendMail will send email to recipient directly.

Since querying DNS server consumes CPU time and networking resouce, the performance of "Send email Directly" is lower than sending email with specified SMTP server. Moreover, nowaday more and more SMTP servers block email sent from dynamic IP address, so we don't recommend you to use "Direct Send Email" except you have a static IP address or you encounter problem with your ISP SMTP server.

Every recipient may have different SMTP server, if there are multiple recipients in one message and you want to send email directly, you should send the email to the recipients one by one. To learn more, please refer to the code in Samples.

Remarks

In our solid experience, we don't suggest that you send email directly.

Example

[Visual Basic, C#, C++, JScript.NET] The following example demonstrates how to send email with EASendMail SMTP Component directly, but it doesn't demonstrates the events usage. To get the full samples of EASendMail, please refer to Samples section.

[VB - Send Email - MX Lookup]

Imports EASendMail

Public Sub SendMailWithMxRecord(sender As String,
    recipient As String,
    subject As String,
    bodyText As String)

    Try
        Dim oMail As SmtpMail = New SmtpMail("TryIt")

        oMail.From = New MailAddress(sender)
        ' Only specify an email address in recipient. For multiple addresses, please send it one by one.
        oMail.To = New AddressCollection(recipient)

        oMail.Subject = subject
        oMail.TextBody = bodyText

        ' The email will be sent to recipient server directly if no server address is specified
        Dim oServer As SmtpServer = New SmtpServer("")

        ' To send email to recipient directly (simulating the smtp server),
        ' please add a Received header as SMTP trace time stamp
        Dim gmtDateTime As String = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zz00", 
                                                New System.Globalization.CultureInfo("en-US"))
        Dim receivedHeader As String = String.Format("from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;" & vbCrLf & Chr(9) & " {1}",
                oServer.HeloDomain,
                gmtDateTime)

        oMail.Headers.Insert(0, New HeaderItem("Received", receivedHeader))

        Dim oSmtp As SmtpClient = New SmtpClient()

        ' To generate a log file for SMTP transaction, please use 
        ' oSmtp.LogFileName = "c:\smtp.txt"

        oSmtp.SendMail(oServer, oMail)
        Console.WriteLine("The message was sent to {0} successfully!", oSmtp.CurrentSmtpServer.Server)

    Catch ex As Exception
        Console.WriteLine("Exception: {0}", ex.Message)
    End Try

End Sub


[C# - Send Email - MX Lookup] using System; using System.Collections; using EASendMail; public void SendMailWithMxRecord(string sender, string recipient, string subject, string bodyText) { try { SmtpMail oMail = new SmtpMail("TryIt"); oMail.From = sender; // Only specify an email address in recipient. For multiple addresses, please send it one by one. oMail.To = recipient; oMail.Subject = subject; oMail.TextBody = bodyText; // The email will be sent to recipient server directly if no server address is specified SmtpServer oServer = new SmtpServer(""); // To send email to recipient directly (simulating smtp server), // please add a Received header as SMTP trace time stamp string gmtDateTime = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zz00", new System.Globalization.CultureInfo("en-US")); string recvheader = string.Format("from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;\r\n\t {1}", oServer.HeloDomain, gmtDateTime); oMail.Headers.Insert(0, new HeaderItem("Received", recvheader)); SmtpClient oSmtp = new SmtpClient(); // To generate a log file for SMTP transaction, please use // oSmtp.LogFileName = "c:\\smtp.txt"; oSmtp.SendMail(oServer, oMail); Console.WriteLine("The message was sent to {0} successfully!", oSmtp.CurrentSmtpServer.Server); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } }
[C++/CLI - Send Email - MX Lookup] using namespace System; using namespace System::Collections; using namespace EASendMail; void SendMailWithMxRecord(String ^sender, String ^recipient, String ^subject, String ^bodyText) { try { SmtpMail ^oMail = gcnew SmtpMail("TryIt"); oMail->From = gcnew EASendMail::MailAddress(sender); // Only specify an email address in sTo. For multiple addresses, please send it one by one oMail->To = gcnew EASendMail::AddressCollection(recipient); oMail->Subject = subject; oMail->TextBody = bodyText; // The email will be sent to recipient server directly if no server address is specified SmtpServer ^oServer = gcnew SmtpServer(""); // To send email to recipient directly (simulating the smtp server), // please add a Received header as SMTP trace time stamp String ^gmtDateTime = DateTime::Now.ToString("ddd, dd MMM yyyy HH:mm:ss zz00", gcnew System::Globalization::CultureInfo("en-US")); String ^receivedHeader = String::Format("from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;\r\n\t {1}", oServer->HeloDomain, gmtDateTime); oMail->Headers->Insert(0, gcnew HeaderItem("Received", receivedHeader)); SmtpClient ^oSmtp = gcnew SmtpClient(); // To generate a log file for SMTP transaction, please use // oSmtp->LogFileName = "c:\\smtp.txt"; oSmtp->SendMail(oServer, oMail); Console::WriteLine("The message was sent to {0} successfully!", oSmtp->CurrentSmtpServer->Server); } catch (Exception ^exp) { Console::WriteLine("Exception: {0}", exp->Message); } }

See Also

Using EASendMail SMTP .NET Component
User Authentication and SSL Connection
Enable TLS 1.2 on Windows XP/2003/2008/7/2008 R2
Using Gmail SMTP OAUTH
Using Gmail/GSuite Service Account + SMTP OAUTH Authentication
Using Office365 EWS OAUTH
Using Office365 EWS OAUTH in Background Service
Using Hotmail SMTP OAUTH
From, ReplyTo, Sender and Return-Path
Digital Signature and E-mail Encryption
DomainKeys and DKIM Signature
Work with EASendMail Service (Email Queuing)
Bulk Email Sender Guidelines
Process Bounced Email (Non-Delivery Report) and Email Tracking
EASendMail .NET Namespace References
EASendMail SMTP Component Samples