This property specifies the operation mode of current object.
Data Type: Long
Remarks
If the value of asynchronous property is zero, Mail object works in synchronous mode. If the value of property is 1, Mail object works in asynchronous mode. In synchronous mode, all events won't be fired. The default value is zero.
It is more easier to develop application in synchronous mode. However, running in asynchronous mode could provide higher performance.
In synchronous mode, once SendMail method is called, it returns to application after the method is complete. Therefore, if the runtime is long, your application cannot do anything before this method ends, which results in lower efficiency. In contrast, in asynchronous mode, as SendMail method works in background, this methods return to application immediately no matter the running method is complete or not. The return value will pass to application via fired event.
To learn more about asynchronous operating mode, please refer to regarding events. You may also download EASendMail in which many samples are embedded. Those samples demonstrate how to use asynchronous operating mode to raise your application's efficiency and flexibility.
Examples
[VB, VC++, Delphi] To get the full samples of EASendMail, please refer to Samples section.
[VB, VBA - Send Email Asynchronously] Const ConnectNormal = 0 Const ConnectSSLAuto = 1 Const ConnectSTARTTLS = 2 Const ConnectDirectSSL = 3 Const ConnectTryTLS = 4 Private WithEvents oSmtp As EASendMailObjLib.Mail Private m_bError As Boolean Private m_bFinished As Boolean Private Sub oSmtp_OnAuthenticated() Label1.Caption = "Authenticated" End Sub Private Sub oSmtp_OnClosed() If Not m_bError Then Label1.Caption = "email was sent successfully!" End If m_bFinished = True End Sub Private Sub oSmtp_OnConnected() Label1.Caption = "Connected" End Sub Private Sub oSmtp_OnError(ByVal lError As Long, ByVal ErrDescription As String) Label1.Caption = "failed to send email with error: " & ErrDescription m_bError = True m_bFinished = True End Sub Private Sub oSmtp_OnSending(ByVal lSent As Long, ByVal lTotal As Long) Label1.Caption = "Sending " & lSent & "/" & lTotal End Sub Private Sub Command1_Click() If oSmtp Is Nothing Then Set oSmtp = New EASendMailObjLib.Mail oSmtp.LicenseCode = "TryIt" End If m_bError = False m_bFinished = False ' Your SMTP server address oSmtp.ServerAddr = "smtp.emailarchitect.net" ' User and password for ESMTP authentication oSmtp.UserName = "test@emailarchitect.net" oSmtp.Password = "testpassword" ' If server supports SSL/TLS connection, SSL/TLS is used automatically. oSmtp.ConnectType = ConnectTryTLS ' Set your sender email address oSmtp.FromAddr = "test@emailarchitect.net" ' Add recipient email address oSmtp.AddRecipientEx "support@emailarchitect.net", 0 ' Set email subject oSmtp.Subject = "test email from VB 6.0 in asynchronous mode" ' Set email body oSmtp.BodyText = "this is a test email sent from VB 6.0 project with asynchronous mode" ' Set asynchronous mode oSmtp.Asynchronous = 1 Command1.Enabled = False Label1.Caption = "start to send email ..." oSmtp.SendMail Do While Not m_bFinished ' Wait for the email sending, you can do other thing here DoEvents Loop MsgBox Label1.Caption Command1.Enabled = True End Sub
[Delphi - Send Email Asynchronously] unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, EASendMailObjLib_TLB; // add EASendMail unit type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; procedure Button1Click(Sender: TObject); // Event handler procedure procedure OnAuthenticated(ASender: TObject); procedure OnConnected(ASender: TObject); procedure OnClosed(ASender: TObject); procedure OnError(ASender: TObject; lError: Integer; const ErrDescription: WideString); procedure OnSending(ASender: TObject; lSent: Integer; lTotal: Integer); private { Private declarations } public { Public declarations } end; const ConnectNormal = 0; ConnectSSLAuto = 1; ConnectSTARTTLS = 2; ConnectDirectSSL = 3; ConnectTryTLS = 4; var Form1: TForm1; m_bFinished : Boolean; m_bError : Boolean; implementation {$R *.dfm} procedure TForm1.OnAuthenticated(ASender: TObject); begin Label1.Caption := 'Authenticated'; end; procedure TForm1.OnConnected(ASender: TObject); begin Label1.Caption := 'Connected'; end; procedure TForm1.OnClosed(ASender: TObject); begin if not m_bError then Label1.Caption := 'email was sent successfully'; m_bFinished := true; end; procedure TForm1.OnError(ASender: TObject; lError: Integer; const ErrDescription: WideString); begin Label1.Caption := 'Failed to send email with error: ' + ErrDescription; m_bError := true; m_bFinished := true; end; procedure TForm1.OnSending(ASender: TObject; lSent: Integer; lTotal: Integer); begin Label1.Caption := Format('Sending %d/%d ...', [lSent, lTotal]); end; procedure TForm1.Button1Click(Sender: TObject); var oSmtp : TMail; begin oSmtp := TMail.Create(Application); oSmtp.LicenseCode := 'TryIt'; // Your SMTP server address oSmtp.ServerAddr := 'smtp.emailarchitect.net'; // User and password for ESMTP authentication, oSmtp.UserName := 'test@emailarchitect.net'; oSmtp.Password := 'testpassword'; // ConnectTryTLS means if server supports SSL/TLS connection, SSL/TLS is used automatically oSmtp.ConnectType := ConnectTryTLS; // If your server uses 587 port // oSmtp.ServerPort := 587; // If your server uses 465 port with SSL/TLS // oSmtp.ConnectType := ConnectSSLAuto; // oSmtp.ServerPort := 465; // If your server uses 25/587 port with SSL/TLS // oSmtp.ConnectType := ConnectSSLAuto; // oSmtp.ServerPort := 587; // Set your sender email address oSmtp.FromAddr := 'test@emailarchitect.net'; // Add recipient email address oSmtp.AddRecipientEx('support@emailarchitect.net', 0); // Set email subject oSmtp.Subject := 'simple email from Delphi project'; // Set body text oSmtp.BodyText := 'this is a test email sent from delphi'; m_bError := false; m_bFinished := false; // Set Asynchronous mode oSmtp.Asynchronous := 1; // Add event handler oSmtp.OnConnected := OnConnected; oSmtp.OnClosed := OnClosed; oSmtp.OnError := OnError; oSmtp.OnSending := OnSending; oSmtp.OnAuthenticated := OnAuthenticated; Button1.Enabled := false; Label1.Caption := 'start to send email ...'; oSmtp.SendMail(); // Wait for the result while not m_bFinished do Application.ProcessMessages(); ShowMessage(Label1.Caption); Button1.Enabled := true; end; end.
[VC++ - Send Email Asynchronously] #include "stdafx.h" #include <tchar.h> #include <Windows.h> #include "EASendMailObj.tlh" using namespace EASendMailObjLib; const int ConnectNormal = 0; const int ConnectSSLAuto = 1; const int ConnectSTARTTLS = 2; const int ConnectDirectSSL = 3; const int ConnectTryTLS = 4; #define IDC_SRCIMAIL 1 static _ATL_FUNC_INFO OnClosed = {CC_STDCALL, VT_EMPTY, 0}; static _ATL_FUNC_INFO OnSending = {CC_STDCALL, VT_EMPTY, 2, {VT_I4, VT_I4}}; static _ATL_FUNC_INFO OnError = {CC_STDCALL, VT_EMPTY, 2, {VT_I4, VT_BSTR}}; static _ATL_FUNC_INFO OnConnected = {CC_STDCALL, VT_EMPTY, 0}; static _ATL_FUNC_INFO OnAuthenticated = {CC_STDCALL, VT_EMPTY, 0}; class CMailEvents:public IDispEventSimpleImpl<IDC_SRCIMAIL, CMailEvents, &__uuidof(_IMailEvents)> { public: CMailEvents(){}; BEGIN_SINK_MAP(CMailEvents) SINK_ENTRY_INFO(IDC_SRCIMAIL, __uuidof(_IMailEvents), 1, &CMailEvents::OnClosedHandler, &OnClosed) SINK_ENTRY_INFO(IDC_SRCIMAIL, __uuidof(_IMailEvents), 2, &CMailEvents::OnSendingHandler, &OnSending) SINK_ENTRY_INFO(IDC_SRCIMAIL, __uuidof(_IMailEvents), 3, &CMailEvents::OnErrorHandler, &OnError) SINK_ENTRY_INFO(IDC_SRCIMAIL, __uuidof(_IMailEvents), 4, &CMailEvents::OnConnectedHandler, &OnConnected) SINK_ENTRY_INFO(IDC_SRCIMAIL, __uuidof(_IMailEvents), 5, &CMailEvents::OnAuthenticatedHandler, &OnAuthenticated) END_SINK_MAP() public: BOOL m_bError; BOOL m_bFinished; _bstr_t m_lastError; protected: HRESULT __stdcall OnClosedHandler() { m_bFinished = TRUE; return S_OK; } HRESULT __stdcall OnSendingHandler(long nSent, long nTotalSize) { _tprintf(_T("Sending %d/%d ...\r\n"), nSent, nTotalSize); return S_OK; } HRESULT __stdcall OnErrorHandler(long nErrorCode, BSTR ErrorMessage) { m_bFinished = TRUE; m_bError = TRUE; m_lastError = ErrorMessage; return S_OK; } HRESULT __stdcall OnConnectedHandler() { _tprintf(_T("Connected\r\n")); return S_OK; } HRESULT __stdcall OnAuthenticatedHandler() { _tprintf(_T("Authenticated\r\n")); return S_OK; } }; int _tmain(int argc, _TCHAR* argv[]) { ::CoInitialize(NULL); IMailPtr oSmtp = NULL; oSmtp.CreateInstance(__uuidof(EASendMailObjLib::Mail)); oSmtp->LicenseCode = _T("TryIt"); // Your SMTP server address oSmtp->ServerAddr = _T("smtp.emailarchitect.net"); // User and password for ESMTP authentication oSmtp->UserName = _T("test@emailarchitect.net"); oSmtp->Password = _T("test"); // ConnectTryTLS means if server supports SSL/TLS connection, SSL/TLS is used automatically oSmtp->ConnectType = ConnectTryTLS; // If your server uses 587 port // oSmtp->ServerPort = 587; // If your server uses 465 port with SSL/TLS // oSmtp->ConnectType = ConnectSSLAuto; // oSmtp->ServerPort = 465; // If your server uses 25/587 port with SSL/TLS // oSmtp->ConnectType = ConnectSSLAuto; // oSmtp->ServerPort = 587; // Set your sender email address oSmtp->FromAddr = _T("test@emailarchitect.net"); // Add recipient email address oSmtp->AddRecipientEx(_T("support@emailarchitect.net"), 0); // Set email subject oSmtp->Subject = _T("email from Visual C++ project in asynchronous mode "); // Set email body oSmtp->BodyText = _T("this is a test email sent from Visual C++ asynchronously"); _tprintf(_T("Start to send email ...\r\n")); // Attach event connection pointer CMailEvents oEvents; oEvents.DispEventAdvise(oSmtp.GetInterfacePtr()); oEvents.m_bFinished = FALSE; oEvents.m_bError = FALSE; // Set asynchronous mode oSmtp->Asynchronous = 1; oSmtp->SendMail(); // Waiting for email sending ... while(!oEvents.m_bFinished) { MSG msg; while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if(msg.message == WM_QUIT) return 0; TranslateMessage(&msg); DispatchMessage(&msg); } // You can do other thing here ::Sleep(10); } // Detach event connection pointer oEvents.DispEventUnadvise(oSmtp.GetInterfacePtr()); if(!oEvents.m_bError) { _tprintf(_T("email was sent successfully!\r\n")); } else { _tprintf(_T("failed to send email with the following error: %s\r\n"), (const TCHAR*)oEvents.m_lastError); } return 0; }
See Also
SendMail Method
Terminate Method
EASendMail Samples
Programming with Asynchronous Mode
Work with EASendMail Service(Mail Queuing)
Online Tutorials
Send Email
in VB 6.0 - Tutorial
Send Email
in Visual C++ - Tutorial
Send
Email in Delphi - Tutorial
Send
Email in MS SQL stored procedure - Tutorial