ivan
  • ivan
  • 100% (Exalted)
  • Administration Topic Starter
14 years ago
Visual Basic Example

' The following example codes demonstrate sending email message using email queue + database
' To get full sample projects, please download and install EASendMail on your machine.
' To run it correctly, please change SMTP server, user, password, sender, recipient value to yours


' Add EASendMail namespace
Imports EASendMail 

Module Module1 
    Sub Main() 
        Dim oMail As New SmtpMail("TryIt") 
        Dim oSmtp As New SmtpClient() 

        ' Set sender email address, please change it to yours
        oMail.From = "test@emailarchitect.net" 

        ' Set email subject
        oMail.Subject = "test email from VB.NET project" 

        ' Your SMTP server address
        Dim oServer As New SmtpServer("smtp.emailarchitect.net") 

        ' User and password for ESMTP authentication, if your server doesn't require
        ' User authentication, please remove the following codes.
        oServer.User = "test@emailarchitect.net" 
        oServer.Password = "testpassword" 

        ' If your smtp server requires SSL connection, please add this line
        ' oServer.ConnectType = SmtpConnectType.ConnectSSLAuto

        ' If you want EASendMail service to send the email after 10 minutes,
        ' use the following code.
        ' oMail.Date = System.DateTime.Now.AddMinutes( 10 );

        ' EASendMail will use the following connection to connect to the database,
        ' the syntax is same as ADO connection object.
        oMail.Headers.ReplaceHeader("X-Data-Connection", _ 
         "Driver={Microsoft Access Driver (*.mdb)}; " & _ 
         "Dbq=c:\easendmail\easendmail_demo.mdb;Uid=;Pwd=;") 

        ' EASendMail will select the fields by the following sql statement
        ' before sending email,
        ' then pick the recipient address from specified field.
        oMail.Headers.ReplaceHeader("X-Sql-Select", "SELECT id, name, address FROM Recipients") 

        ' pick "name" field as the recipient name and "address" field as
        ' the recipient address.
        ' you can also use {$var_srecord:fieldname} to pick any field
        ' in X-Sql-Select statement and put it to subject, bodytext, then EASendMail will
        ' replace it automatially.

        oMail.Headers.ReplaceHeader("To", """{$var_srecord:name}"" <{$var_srecord:address}>") 
        oMail.Headers.ReplaceHeader("X-Rcpt-To", "{$var_srecord:address}") 

        ' EASendMail service will execute the following sql statement on
        ' every email was sent successfully.
        oMail.Headers.ReplaceHeader("X-Sql-OnSentSuccess", _ 
        "INSERT INTO sentlog ( server, email ) VALUES( '{$var_server}', '{$var_rcptaddr}' )") 

        ' EASendMail service will execute the following sql statement on
        ' every email could not be sent.
        oMail.Headers.ReplaceHeader("X-Sql-OnSentError", _ 
        "INSERT INTO errorlog( email, server, errorcode, errordescription )" & _ 
        " VALUES( '{$var_rcptaddr}', '{$var_server}', '{$var_errcode}', '{$var_errdesc}' )") 

        Dim s As String = "Hi {$var_srecord:name}, " & vbCrLf 
        s += "this sample demonstrates how to send email using email queue." & vbCrLf & vbCrLf 
        ' {$var_srecord:address} will be replaced by EASendMail automatically.
        s += "Your id in database is {$var_srecord:id}." & vbCrLf & vbCrLf 
        s += "No matter how many recipients there are, EASendMail " 
        s += "service will send the email in background." & vbCrLf 

        ' {$var_srecord:id} {$var_srecord:address} {$var_srecord:name} in
        ' body text will
        ' be replaced by EASendMail automatically.
        oMail.TextBody = s 

        Try 

            Console.WriteLine("start to send email ...") 
            oSmtp.SendMailToQueue(oServer, oMail) 
            Console.WriteLine("email was sent queue successfully!") 

        Catch ep As Exception 

            Console.WriteLine("failed to send email with the following error:") 
            Console.WriteLine(ep.Message) 
        End Try 

    End Sub 
End Module 

Click here to read original topic - full version ... 

If you have any comments or questions about above example codes, please add your comments here.

EXPLORE TUTORIALS

© All Rights Reserved, AIFEI Software Limited & AdminSystem Software Limited.