SendMail Method


Send email to specified SMTP server or Exchange Server.

[Syntax]
Visual C++: HRESULT SendMail( long* pVal )
Visual Basic: SendMail( ) As Long

Return Value

In synchronous mode, if this method succeeds, the return value is zero; otherwise the return value is non-zero. You can obtain the last error information via GetLastError/GetLastErrDescription method.
In asynchronous mode, this method return to application immediately no matter all emails are sent or not. If OnError event isn't fired while sending email, OnClosed event will indicate the task is finished successfully.
To learn more programming skills in asynchronous mode, please refers to Programming with Asynchronous Mode.

Remarks

If ServerAddr property is not assigned, Mail object sends email via DNS lookup automatically.

Example

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

[VB6, VBA - Send Email]
Private Sub SendEmail()
  Dim oSmtp As EASendMailObjLib.Mail
  Set oSmtp = New EASendMailObjLib.Mail
  
  'The license code for EASendMail ActiveX Object, 
  'for evaluation usage, please use "TryIt" as the license code.
  oSmtp.LicenseCode = "TryIt"
 
  oSmtp.ServerAddr = "mail.adminsystem.com"
  
  ' set SMTP user authentication 
  oSmtp.UserName = "test@adminsystem.com"
  oSmtp.Password = "test"
  
  'If your server requires SSL connection 
  'oSmtp.SSL_init 
  'oSmtp.SSL_starttls = 0 
  'oSmtp.ServerPort = 465
  
  ''If your server requires TLS connection 
  'oSmtp.SSL_init 
  'oSmtp.SSL_starttls = 1 
  'oSmtp.ServerPort = 25
  
  oSmtp.FromAddr = "test@adminsystem.com"
  oSmtp.AddRecipient "Support Team", "support@adminsystem.com", 0
 
  oSmtp.Subject = "Test"
  oSmtp.BodyText = "Hello, this is a test...."
 
  If oSmtp.SendMail() = 0 Then
    MsgBox "Message delivered!"
  Else
    MsgBox oSmtp.GetLastErrDescription()
  End If
End Sub


[ASP, VBScript - Send Email]
  Dim oSmtp
  Set oSmtp = Server.CreateObject("EASendMailObj.Mail")
  
  'The license code for EASendMail ActiveX Object, 
  'for evaluation usage, please use "TryIt" as the license code.
  oSmtp.LicenseCode = "TryIt"
 
  oSmtp.ServerAddr = "mail.adminsystem.com" 
  
  ' set SMTP user authentication 
  'oSmtp.UserName = "test@adminsystem.com"
  'oSmtp.Password = "test"
  
  'If your server requires SSL connection 
  'oSmtp.SSL_init 
  'oSmtp.SSL_starttls = 0 
  'oSmtp.ServerPort = 465
  
  ''If your server requires TLS connection 
  'oSmtp.SSL_init 
  'oSmtp.SSL_starttls = 1 
  'oSmtp.ServerPort = 25
  
  oSmtp.FromAddr = "test@adminsystem.com"
  oSmtp.AddRecipient "Support Team", "support@adminsystem.com", 0
 
  oSmtp.Subject = "Test email sent from VB6, VBA"
  oSmtp.BodyText = "Hello, this is a test...."
 
  If oSmtp.SendMail() = 0 Then
    Response.Write "Message delivered!"
  Else
    Response.Write oSmtp.GetLastErrDescription()
  End If


[JScript/WSH - Send Email]
function SendEmail()
{
  var oSmtp = new ActiveXObject("EASendMailObj.Mail");
  //The license code for EASendMail ActiveX Object, 
  //for evaluation usage, please use "TryIt" as the license code.
  oSmtp.LicenseCode = "TryIt";  
  
  oSmtp.ServerAddr = "mail.adminsystem.com";
  
  // set SMTP user authentication
  oSmtp.UserName = "test@adminsystem.com"; 
  oSmtp.Password = "test";
  
  //If your server requires SSL connection 
  //oSmtp.SSL_init(); 
  //oSmtp.SSL_starttls = 0; 
  //oSmtp.ServerPort = 465;
  
  //If your server requires TLS connection 
  //oSmtp.SSL_init(); 
  //oSmtp.SSL_starttls = 1; 
  //oSmtp.ServerPort = 25;
    
  oSmtp.FromAddr = "test@adminsystem.com";
  oSmtp.AddRecipient( "Support Team", "support@adminsystem.com", 0 );
 
  oSmtp.Subject = "Test email sent from ASP, VBScript";
  oSmtp.BodyText = "Hello, this is a test ....";
  
  if( oSmtp.SendMail() == 0 )
    WScript.Echo( "Message delivered!" );
  else
    WScript.Echo( oSmtp.GetLastErrDescription());
}

[Visual C++ - Send Email]
#include "stdafx.h"
#include <comdef.h>
#include <iostream>

#include "easendmailobj.tlh"
using namespace EASendMailObjLib;

using namespace std;
 
void SendEmail()
{
  ::CoInitialize( NULL );
  IMailPtr oSmtp = NULL;
  oSmtp.CreateInstance(__uuidof(EASendMailObjLib::Mail));
	
  //The license code for EASendMail ActiveX Object,
  //for evaluation usage, please use "TryIt" as the license code.
  oSmtp->LicenseCode = _T("TryIt");
	
  oSmtp->ServerAddr = _T( "mail.adminsystem.com" );
  
  // set user authentication 
  oSmtp->UserName = _T("test@adminsystem.com"); 
  oSmtp->Password = _T("test");
  
  //If your server requires SSL connection 
  //oSmtp->SSL_init();
  //oSmtp->SSL_starttls = 0; 
  //oSmtp->ServerPort = 465;
  
  //If your server requires TLS connection 
  //oSmtp->SSL_init();
  //oSmtp->SSL_starttls = 1; 
  //oSmtp->ServerPort = 25;  
  
  oSmtp->FromAddr = _T( "test@adminsystem.com" );
  oSmtp->AddRecipient( _T("Support Team"), 
                          _T("support@adminsystem.com"), 0 );
 
  oSmtp->Subject = _T("Test");
  oSmtp->BodyText = _T("Hello, this is a test email from VC++ ....");
  
  if( oSmtp->SendMail() == 0 )
    cout << _T("Message delivered!") << endl;
  else
    cout << (const TCHAR*)(oSmtp->GetLastErrDescription()) << endl;
}

[Delphi - Send Email]
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;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  oSmtp : TMail;
begin
  oSmtp := TMail.Create(Application);
  oSmtp.LicenseCode := 'TryIt';

  // 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 email body
  oSmtp.BodyText := 'this is a test email sent from Delphi project, do not reply';

  // Your SMTP server address
  oSmtp.ServerAddr := 'smtp.emailarchitect.net';

  // User and password for ESMTP authentication
  oSmtp.UserName := 'test@emailarchitect.net';
  oSmtp.Password := 'testpassword';

  // If your SMTP server requires SSL connection, please add this line
  // oSmtp.SSL_init();

  ShowMessage( 'start to send email ...' );

  if oSmtp.SendMail() = 0 then
    ShowMessage( 'email was sent successfully!' )
  else
    ShowMessage( 'failed to send email with the following error: '
    + oSmtp.GetLastErrDescription());

end;

end.

See Also

Programming with Asynchronous Mode
Work with EASendMail Service(Mail Queuing)
SendMailToQueue Method
SendMailToQueueEx Method
EASendMail Samples

Online Tutorials

Send Email in VB 6.0 - Tutorial
Send Email in C# - Tutorial
Send Email in VB.NET - Tutorial
Send Email in Visual C++ - Tutorial
Send Email in Managed C++/CLI - Tutorial
Send Email in Delphi - Tutorial
Send Email in MS SQL stored procedure - Tutorial