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 SendMail( sFrom As String, _ 
    sTo As String, _ 
    sSubject As String, _
    sBodyText As String )

    Dim oMail As SmtpMail = New SmtpMail("TryIt")
    Dim oSmtp As SmtpClient = New SmtpClient
    'To generate a log file for SMTP transaction, please use 
    'oSmtp.LogFileName = "c:\smtp.log"

    Dim errStr As String = ""

    Try
        oMail.From = New MailAddress( sFrom )
        'Only specify an email address in sTo. For multiple addresses, please refer to 
        'EASendMail full samples.
        oMail.To = New AddressCollection(sTo) 
      
        oMail.Subject = sSubject
        oMail.TextBody = sBodyText
        'EASendMail will send email 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, 
        'otherwise, many anti-spam filter will mark it as junk email.    
        
        Dim cur As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")
        Dim gmtdate As String = System.DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zz00", cur)
        Dim recvheader As String = String.Format("from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;" & vbCrLf & Chr(9) & " {1}", _
                    oServer.HeloDomain, _
                    gmtdate)
        oMail.Headers.Insert(0, New HeaderItem("Received", recvheader)) 
        
        oSmtp.SendMail(oServer, oMail)
        MessageBox.Show(String.Format("The message was sent to {0} successfully!", _
oSmtp.CurrentSmtpServer.Server))

    Catch exp As SmtpTerminatedException
        errStr = exp.Message
    Catch exp As SmtpServerException
        errStr = String.Format("Exception: Server Respond: {0}", exp.ErrorMessage)
    Catch exp As System.Net.Sockets.SocketException
        errStr = String.Format("Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message)
    Catch exp As System.ComponentModel.Win32Exception
        errStr = String.Format("Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message)
    Catch exp As System.Exception
        errStr = String.Format("Exception: Common: {0}", exp.Message)
    End Try
    If errStr.Length > 0 Then
        MessageBox.Show(errStr)
    End If
End Sub

[C# - Send Email - MX Lookup]
using System;
using System.Collections;
using EASendMail;

public void SendMail( string sFrom, 
    string sTo, 
    string sSubject,
    string sBodyText )
{
    SmtpMail oMail = new SmtpMail("TryIt");
    SmtpClient oSmtp = new SmtpClient();
    //To generate a log file for SMTP transaction, please use 
    //oSmtp.LogFileName = "c:\\smtp.log";
    string err = "";

    try
    {
        oMail.From = sFrom;
        //Only specify an email address in sTo. For multiple addresses, please refer to 
        //EASendMail full samples. 
        oMail.To = sTo;
     
        oMail.Subject = sSubject;
        oMail.TextBody = sBodyText;
        //EASendMail will send email 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, 
        //otherwise, many anti-spam filter will mark it as junk email.
        System.Globalization.CultureInfo cur = new System.Globalization.CultureInfo("en-U");
        string gmtdate = System.DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zz00", cur);
        string recvheader = String.Format( "from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;\r\n\t {1}",
                            oServer.HeloDomain,
                            gmtdate );

        oMail.Headers.Insert( 0, new HeaderItem( "Received", recvheader ));        
        oSmtp.SendMail( oServer, oMail  );
        
        MessageBox.Show( String.Format( "The message was sent to {0} successfully!", 
            oSmtp.CurrentSmtpServer.Server ));

    }
    catch( SmtpTerminatedException exp )
    {
        err = exp.Message;
    }
    catch( SmtpServerException exp )
    {
        err = String.Format( "Exception: Server Respond: {0}", exp.ErrorMessage );
    }
    catch( System.Net.Sockets.SocketException exp )
    {
        err = String.Format( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message );
    }
    catch( System.ComponentModel.Win32Exception exp )
    {
        err = String.Format( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message );          
    }
    catch( System.Exception exp )
    {
        err = String.Format( "Exception: Common: {0}", exp.Message );           
    }
    
    if( err.Length > 0  )
    {
        MessageBox.Show( err );
    }
}

[C++/CLI - Send Email - MX Lookup]
using namespace System;
using namespace System::Collections;
using namespace EASendMail; 

System::Void SendMail( System::String ^sFrom, 
    System::String ^sTo, 
    System::String ^sSubject,
    System::String ^sBodyText )
{
    SmtpMail ^oMail = gcnew SmtpMail("TryIt");
    SmtpClient ^oSmtp = gcnew SmtpClient();
    //To generate a log file for SMTP transaction, please use 
    //oSmtp->LogFileName = "c:\\smtp.log";

    System::String ^err = "";

    try
    {
        oMail->From = gcnew EASendMail::MailAddress(sFrom);
        //Only specify an email address in sTo. For multiple addresses, please refer to 
        //EASendMail full samples.
        oMail->To =  gcnew EASendMail::AddressCollection(sTo);
        oMail->Subject = sSubject;
        oMail->TextBody = sBodyText;
        //EASendMail will send email 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, 
        //otherwise, many anti-spam filter will mark it as junk email.
        System::Globalization::CultureInfo ^cur = gcnew System::Globalization::CultureInfo("en-U");
        System::DateTime now = System::DateTime::Now;
        System::String ^gmtdate = now.ToString("ddd, dd MMM yyyy HH:mm:ss zz00", cur);
        System::String ^recvheader = String::Format( "from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;\r\n\t {1}",
            oServer->HeloDomain,
            gmtdate );

        oMail->Headers->Insert( 0, gcnew HeaderItem( "Received", recvheader ));
                           
        oSmtp->SendMail( oServer, oMail );
    
        MessageBox::Show( String::Format( "The message was sent to {0} successfully!", 
            oSmtp->CurrentSmtpServer->Server ));

    }
    catch( EASendMail::SmtpTerminatedException ^exp )
    {
        err = exp->Message;
    }
    catch( EASendMail::SmtpServerException ^exp )
    {
        err = String::Format( "Exception: Server Respond: {0}", exp->ErrorMessage );
    }
    catch( System::Net::Sockets::SocketException ^exp )
    {
        err = String::Format( "Exception: Networking Error: {0} {1}", exp->ErrorCode.ToString("d"), exp->Message );
    }
    catch( System::ComponentModel::Win32Exception ^exp )
    {
        err = String::Format( "Exception: System Error: {0} {1}", exp->ErrorCode.ToString("d"), exp->Message );           
    }
    catch( System::Exception ^exp )
    {
        err = String::Format( "Exception: Common: {0}", exp->Message );            
    }
    
    if( err->Length > 0  )
    {
        MessageBox::Show( err );
    }
}

[JScript.NET - Send Email - MX Lookup]
public function SendMail( sFrom:String, 
    sTo:String, 
    sSubject:String,
    sBodyText:String )
{
    var oMail:SmtpMail =  new SmtpMail("TryIt");
    var oSmtp:SmtpClient = new SmtpClient();
    //To generate a log file for SMTP transaction, please use 
    //oSmtp.LogFileName = "c:\\smtp.log";
        
    var err:String = "";    
    try
    {           
        oMail.From  = new MailAddress(sFrom);
        oMail.Subject = sSubject
        //Only specify an email address in sTo. For multiple addresses, please refer to 
        //EASendMail full samples.
             
        oMail.To = new AddressCollection(sTo);
        oMail.TextBody = sBodyText;
        //EASendMail will send email directly if no server address is specified         
        var oServer:SmtpServer = new SmtpServer("");
        
        //To send email to recipient directly (simulating the smtp server),
        //please add a Received header, 
        //otherwise, many anti-spam filter will mark it as junk email.  
        var cur:System.Globalization.CultureInfo = new System.Globalization.CultureInfo("en-U");
        var gmtdate:String = System.DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zz00", cur);
        var recvheader:String = String.Format( "from {0} ([127.0.0.1]) by {0} ([127.0.0.1]) with SMTPSVC;\r\n\t {1}",
                            oServer.HeloDomain,
                            gmtdate );

        oMail.Headers.Insert( 0, new HeaderItem( "Received", recvheader ));       
        oSmtp.SendMail( oServer, oMail );
        MessageBox.Show( String.Format("The message was sent to {0} successfully!", oServer.Server ));
        
    }
    catch( exp:SmtpTerminatedException  )
    {
        err = exp.Message;
    }
    catch( exp:SmtpServerException )
    {
        err = String.Format( "Exception: Server Respond: {0}", exp.ErrorMessage );
    }
    catch( exp:System.Net.Sockets.SocketException )
    {
        err = String.Format( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message );
    }
    catch( exp:System.ComponentModel.Win32Exception )
    {
        err = String.Format( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message );          
    }
    catch( exp:System.Exception )
    {
        err = String.Format( "Exception: Common: {0}", exp.Message );           
    }   
    
    if( err.Length > 0 )
    {
        MesageBox.Show( err );
    }
}

See Also

Using EASendMail SMTP .NET Component
User Authentication and SSL Connection
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