SendMail Method

Send email to specified SMTP server.

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

Return Values

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 the corresponding events.

Remarks

If ServerAddr property is not assigned, ANSMTP sends email via DNS lookup automatically. We recommend to use SetDnsServer method to specify DNS server on windows95.

Usage Example:

[Visual Baisc]
Private Sub SendEmail()
  Dim oSmtp As AOSMTPLib.Mail
  Set oSmtp = New AOSMTPLib.Mail

  oSmtp.ServerAddr = "mail.adminsystem.net"
  'If you don't have SMTP server, use the following code
  'send email via DNS lookup, ANSMTP lookups SMTP server automatically.
  'oSmtp.ServerAddr = ""  
    
  oSmtp.FromAddr = "test@adminsystem.net"
  oSmtp.AddRecipient "Support Team", "support@adminsystem.net", 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
[C#]
private void SendEmail()
{
  AOSMTPLib.MailClass oSmtp = new AOSMTPLib.MailClass();
  oSmtp.ServerAddr = "mail.adminsystem.net";
  //If you don't have a smtp server, then use the following code
  //send email via dns lookup, ansmtp would lookup smtp server automatically.
  //oSmtp.ServerAddr = "";  
  
  oSmtp.FromAddr = "test@adminsystem.net";
  oSmtp.AddRecipient( "Support Team", "support@adminsystem.net", 0 );

  oSmtp.Subject = "Test";
  oSmtp.BodyText = "Hello, this is a test....";
  
  if( oSmtp.SendMail() == 0 )
    Console.WriteLine( "Message delivered!" );
  else
    Console.WriteLine( oSmtp.GetLastErrDescription());
}
[JScript/WSH]
function SendEmail()
{
  var oSmtp = new ActiveXObject("AOSMTP.Mail");
  oSmtp.ServerAddr = "mail.adminsystem.net";
  //If you don't have SMTP server, use the following code
  //send email via DNS lookup, ANSMTP lookups SMTP server automatically.
  //oSmtp.ServerAddr = "";  
  
  oSmtp.FromAddr = "test@adminsystem.net";
  oSmtp.AddRecipient( "Support Team", "support@adminsystem.net", 0 );

  oSmtp.Subject = "Test";
  oSmtp.BodyText = "Hello, this is a test....";
  
  if( oSmtp.SendMail() == 0 )
    WScript.Echo( "Message delivered!" );
  else
    WScript.Echo( oSmtp.GetLastErrDescription());
}
[Visual C++]
#include "stdafx.h"
#include <comdef.h>
#include <iostream>
#import "C:\Program Files\AdminSystem.NET\ANSMTP\AOSMTP.dll" \
                                  rename_namespace("AOSMTPLib")
using namespace AOSMTPLib;
using namespace std;

void SendEmail()
{
  ::CoInitialize( NULL );
  IMailPtr oSmtp = NULL;
  oSmtp.CreateInstance("AOSMTP.Mail");
	
  oSmtp->ServerAddr = _bstr_t( "mail.adminsystem.net" );
  //If you don't have SMTP server, use the following code
  //send email via DNS lookup, ANSMTP lookups SMTP server automatically.
  //oSmtp->ServerAddr = _bstr_t("");  
    
  oSmtp->FromAddr = _bstr_t( "test@adminsystem.net" );
  oSmtp->AddRecipient( _bstr_t("Support Team"), 
                          _bstr_t("support@adminsystem.net"), 0 );

  oSmtp->Subject = _bstr_t("Test");
  oSmtp->BodyText = _bstr_t("Hello, this is a test....");
  
  if( oSmtp->SendMail() == 0 )
    cout << "Message delivered!" << endl;
  else
    cout << (const char*)(oSmtp->GetLastErrDescription()) << endl;
}

See Also

Asynchronous Property
OnSending Event
Terminate Method
ANSMTP Samples
SaveMailEx Method


2001-2007 © Copyright AdminSystem Software Limited. All rights reserved.