| xHarbour Reference Documentation > Class Reference (textmode) |
![]() |
![]() |
![]() |
Creates a new TIpClientSmtp object.
TIpClientSmtp():new( <cUrl>, [<lTrace>] ) --> oTIpClientSmtp
smtp://<mailAccount>:<password>@<mail.server.com>
Alternatively, a TUrl() object can be passed that is initialized with the SMTP mail server URL.
The function returns a new TIpClientSmtp object and method :new() initializes the object.
Objects of the TIpClientSmtp() class inherit from the generic internet client class TIpClient(). TIpClientSmtp objects are used to communicate with an SMTP mail server. They use the Simple Mail Transfer Protocol (SMTP, RFC0821.TXT) for sending, or writing, eMails. The address of the mail server must be provided as a URL string with method :new(). The URL is maintained by a TUrl() object, which is stored in the :oUrl instance variable.
The internet connection to the mail server must be established with the :open() method.
Once the internet connection is established (opened), eMails can be sent using the :sendMail() method, which accepts an eMail in form of a TIpMail() object.
When all eMails are sent, the internet connection must be closed with the :close() method.
Note: methods of the TIpClientSmtp class are divided into "High level" and "Low level" methods. "High level" methods are designed to process TIpMail() objects, while "Low level" methods communicate with an SMTP server directly using commands of the SMTP protocol.
| See also: | TIpClientFtp(), TIpClientHttp(), TIpClientPop(), TIpMail(), TUrl() |
| Category: | Internet functions , Object functions , xHarbour extensions |
| Source: | tip\smtpcln.prg |
| LIB: | lib\xhb.lib |
| DLL: | dll\xhb.dll |
// The example outlines the steps and data required to send an eMail
// including file attachment to an SMTP mail server (it forwards the eMail)
PROCEDURE Main
LOCAL oSmtp, oEMail
LOCAL cSmtpUrl
LOCAL cSubject, cFrom, cTo, cBody, cFile
// preparing data for eMail
cSmtpUrl := "smtp://mailaccount:password@smtp.server.com"
cSubject := "Testing eMail"
cFrom := "MyName@Mail.server.com"
cTo := "YourName@another.server.com"
cFile := "File_Attachment.zip"
cBody := "This is a test mail sent at: " + DtoC(date()) + " " + Time()
// preparing eMail object
oEMail := TIpMail():new()
oEMail:setHeader( cSubject, cFrom, cTo )
oEMail:setBody( cBody )
oEMail:attachFile( cFile )
// preparing SMTP object
oSmtp := TIpClientSmtp():new( cSmtpUrl )
// sending data via internet connection
IF oSmtp:open()
oSmtp:sendMail( oEMail )
oSmtp:close()
? "Mail sent"
ELSE
? "Error:", oSmtp:lastErrorMessage()
ENDIF
RETURN
http://www.xHarbour.com