SophiaFramework UNIVERSE 5.3 |
Procedure to send a SMTP mail
Example 835. The method to send a mail using the SFXSMTPSender class
class MyClass { private: SFXSMTPSender _sender; XALLBACK_DECLARE_SFXSMTPSENDER(SMTPCallback) public: Void Start(Void); }; Void MyClass::Start(Void) { SFCError error; SFXMailMessage message; // set From field to mail header message.SetFromField("[email protected]"); // add To addresses to mail header message.AddToField("[email protected]"); message.AddToField("[email protected]"); // set Subject field to mail header message.SetSubjectField("Mail Subject"); // set Body of the mail message message.SetBody("Mail test\r\nThis mail is sent by SFXSMTPSender.\r\n"); // set IP address and port number of SMTP server (domain is automatically resolved) _sender.SetServer(SFXSocketAddress("smtpserver.example.com:25")); // register SMTPCallback as callback function, and then send mail // SMTPCallback function will be notified of the completion of sending mail if ((error = _sender.SendMessage(&message, XALLBACK_INTERNAL(SMTPCallback))) != SFERR_NO_ERROR) { // callback will never be notifiled to SMTPCallback function if error is not SFERR_NO_ERROR // error handling for this case ..... } } // callback function notified of the completion of sending mail XALLBACK_IMPLEMENT_SFXSMTPSENDER(MyClass, SMTPCallback, error) { if (error == SFERR_NO_ERROR) { // succeed to send mail TRACE("Mail is sent."); } else { TRACE("Failed to send mail: %d", error); } }
In the SFXMailMessage instance, the From field and the To field need to be set. The Cc and Bcc fileds can be set if neccessary.
The mail can be sent by the SFXSMTPSender::SendText function without using the SFXMailMessage class. In this case, the mail text must be created from the mail header and mail body. The sender's host name, the sender's mail address, and the recipient's mail address are included in the mail header.
SFXMailMessage | SFXSMTP | SFXSMTPSender::SendText | Mail Sending
Constructor/Destructor |
---|
SFXSMTPSender( Void ) Constructor of the SFXSMTPSender class.
|
~SFXSMTPSender( Void ) Destructor of the SFXSMTPSender class.
|
Public Functions | |
---|---|
SFCError |
AddTo(
SFXAnsiStringConstRef to
) Add the mail address of recipient.
|
Void |
Cancel( Void ) Cancel sending a SMTP mail.
|
Void |
ClearTo( Void ) Delete all the mail addresses of recipients.
|
ProgressEnum |
GetProgress( Void ) Get the progress status of sending a SMTP mail.
|
SFXSMTPRef |
GetSFXSMTP( Void ) Get the SFXSMTP instance that is internally used.
|
Bool |
GetSSLMode( Void ) Get the SSL connection mode.
|
UInt32 |
GetTrustMode( Void ) Get the SSL trust mode.
|
Bool |
IsQuit( Void ) Check whether or not the QUIT command has been sent.
|
SFCError |
SendMessage(
SFXMailMessagePtr message
, CallbackSPP spp
, VoidPtr reference
) Send the mail message.
|
SFCError |
SendText(
SFXAnsiStringConstRef text
, CallbackSPP spp
, VoidPtr reference
) Send the mail text.
|
SFCError |
SetAuthorization(
AuthEnum auth
, SFXAnsiStringConstRef user
, SFXAnsiStringConstRef password
) Set the SMTP authorization method.
|
SFCError |
SetFrom(
SFXAnsiStringConstRef from
) Set the mail address of sender.
|
SFCError |
SetHostName(
SFXAnsiStringConstRef host
) Set the host name of sender.
|
Void |
SetSSLMode(
Bool isSSL
) Set the SSL connection mode.
|
SFCError |
SetServer(
SFXSocketAddressConstRef server
) Set the IP address and port number of the SMTP server.
|
Void |
SetTrustMode(
UInt32 sslTrustMode
) Set the SSL trust mode.
|
Types |
---|
AuthEnum Constants that represent the SMTP authorization methods.
|
CallbackSPP Type that represents the callback function to notify the completion of the SMTP mail sending.
|
ProgressEnum Constants that show the progress statuses of sending a SMTP mail.
|
[ public, explicit ] SFXSMTPSender(Void);
This constructor does the initialization as follows:
Note | |
---|---|
The resources necessary to send a SMTP mail are not allocated immediately after the SFXSMTPSender instance is created by this constructor. When calling the SFXSMTPSender::SendMessage or SFXSMTPSender::SendText function, those resources will be allocated. |
SFXSMTPSender::SetAuthorization | SFXSMTPSender::SetSSLMode | SFXSMTPSender::SetTrustMode | SFXSMTPSender::SendMessage | SFXSMTPSender::SendText
[ public ] ~SFXSMTPSender(Void);
This destructor calls the SFXSMTPSender::Cancel function to cancel the SMTP mail sending.
[ public ] SFCError AddTo( SFXAnsiStringConstRef to // mail address of recipient );
Mail address of recipient.
Mail address needs to be enclosed by "<>".
Specify the <[email protected]> string if you want to send a mail to [email protected].
To set more than one mail address of recipient, call the SFXSMTPSender::AddTo function multiple times.
It is necessary to use this function before calling the SFXSMTPSender::SendText function.
[ public ] Void Cancel(Void);
This function cancels the SMTP mailsending, and then all the allocated resources will be released.
Concretely, the following processings will be done.
Note | |
---|---|
This function is called in the SFXSMTPSender::~SFXSMTPSender destructor internally. |
SFXSMTPSender::SendMessage | SFXSMTPSender::SendText | SFXSMTPSender::ClearTo | SFXSMTPSender::AddTo | SFXSMTPSender::SetFrom | SFXSMTPSender::~SFXSMTPSender | SFXSMTP::Close | SFXSMTPSender::ProgressEnum
[ public ] Void ClearTo(Void);
This function deletes all the mail addresses of recipients set by the SFXSMTPSender::AddTo function.
[ public, const ] ProgressEnum GetProgress(Void);
Return the progress status of sending a SMTP mail.
About the return value of the SFXSMTPSender::GetProgress function | |
---|---|
Reference: SFXSMTPSender::ProgressEnum |
If sending a SMTP mail failed, call this function in the callback function to get the stage where an error occurred.
[ public ] SFXSMTPRef GetSFXSMTP(Void);
[ public, const ] Bool GetSSLMode(Void);
[ public, const ] UInt32 GetTrustMode(Void);
The SSL trust mode can be one of the following values.
Note | |
---|---|
Reference: ISSL_NegotiateV in the BREW API Reference |
[ public ] Bool IsQuit(Void);
[ public ] SFCError SendMessage( SFXMailMessagePtr message // mail message to send CallbackSPP spp // callback function VoidPtr reference // argument to callback function );
It is necessary to set the SMTP server using the SFXSMTPSender::SetServer function before sending the mail message with this function.
Although the hostname of the sender is automatically obtained, it can be set using the SFXSMTPSender::SetHostName function.
The mail address of the sender is the From field included in the mail message header.
The mail addresses of recipients are the To / Cc / Bcc fields included in the mail message header. The Bcc fields will be deleted when sending the mail.
Result of sending a SMTP mail | |
---|---|
The result of sending a SMTP mail is notified to the callback function. It is not reflected in the return value of this function. |
SFXSMTPSender::SetServer | SFXSMTPSender::SetHostName | SFXSMTPSender::SetFrom | SFXSMTPSender::AddTo | SFXSMTPSender::ClearTo | SFXSMTPSender::Cancel | SFXSMTPSender::SendText | SFXSMTPSender::SFXSMTPSender
[ public ] SFCError SendText( SFXAnsiStringConstRef text // text to send CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
This function sends the mail text that consists of a header and a body. The header of the mail text includes the host name and mail address of sender, and the mail address of recipient.
How to use the SFXSMTPSender::SendText function
Before sending a mail, set the SMTP server, the host name of the sender, the mail address of the sender, and the mail address of the recipient using the SFXSMTPSender::SetServer, SFXSMTPSender::SetHostName, SFXSMTPSender::SetFrom, and SFXSMTPSender::AddTo functions respectively.
And then to send a mail, call this function specifiying the mail text including the header as the first argument.
Result of sending a SMTP mail | |
---|---|
The result of sending a SMTP mail is notified to the callback function. It is not reflected in the return value of this function. |
SFXSMTPSender::SetServer | SFXSMTPSender::SetHostName | SFXSMTPSender::SetFrom | SFXSMTPSender::AddTo | SFXSMTPSender::ClearTo | SFXSMTPSender::Cancel | SFXSMTPSender::SendMessage | SFXSMTPSender::SFXSMTPSender
[ public ] SFCError SetAuthorization( AuthEnum auth // authorization method SFXAnsiStringConstRef user // username SFXAnsiStringConstRef password // password );
Specify the authorization method.
Reference: SFXSMTPSender::AuthEnum Constants
Specify username.
Specify password.
If this function is not called, communication will be done without the SMTP authorization.
SFXSMTPSender sender;
SFCError error;
// authenticate SMTP by LOGIN authorization method
error = _sender.SetAuthorization(SFXSMTPSender::AUTH_LOGIN , "user" , "password");
if (error != SFERR_NO_ERROR) {
TRACE("Authorization error : %d", error);
}
SFXSMTPSender::AuthEnum | SFXSMTPSender::SendMessage | SFXSMTPSender::SendText | SFXSMTPSender::SFXSMTPSender
[ public ] SFCError SetFrom( SFXAnsiStringConstRef from // sender's mail address );
Mail address of the sender
Mail address must be enclosed by "<>".
Specify <[email protected]> character string if you want to send mail to [email protected].
This function must be called before the SFXSMTPSender::SendText function.
[ public ] SFCError SetHostName( SFXAnsiStringConstRef host // host name );
Sender's host name.
Empty string is not permitted.
This function must be called before the SFXSMTPSender::SendText function.
The host name set by this function is used as an argument of the EHLO or HELO command of the SMTP protocol.
To use the SSL connection, specifiy true in the isSSL argument.
[ public ] SFCError SetServer( SFXSocketAddressConstRef server // IP address and port number of SMTP server );
IP address and port number of the SMTP server.
SFXSMTPSender::SendMessage | SFXSMTPSender::SendText | SFXSMTPSender::SFXSMTPSender | SFXSocketAddress
One of the following values are available for the SSL trust mode:
Note | |
---|---|
Reference: ISSL_NegotiateV in the BREW API Reference |
SFXSMTPSender::GetTrustMode | SFXSSLSocket::SetTrustMode | SFXSMTPSender::SFXSMTPSender | BREW API ISSL_NegotiateV
enum AuthEnum { AUTH_NONE, AUTH_PLAIN, AUTH_LOGIN, AUTH_CRAM_MD5, AUTH_DIGEST_MD5 };
To set the SMTP authorization method, use the SFXSMTP::SendAuthCommand function.
Reference: SFXSMTP::SendAuthCommand
SMTP authorization is not used.
Represent the PLAIN authorization.
Represent the LOGIN authorization.
Represent the CRAM-MD5 authorization.
Represent the DIGEST-MD5 authorization (in the current version, the DIGEST-MD5 authorization cannot be used)
typedef Void(* SFXSMTPSender::CallbackSPP)(SFCError error, VoidPtr reference)
This function prototype represents the callback function called when sending a SMTP mail is finished.
To register this function, use the SFXSMTPSender::SendMessage or SFXSMTPSender::SendText function.
The result of sending a SMTP mail is obtained in this callback function.
For the 1st argument "error": SFERR_NO_ERROR will be set if sending a SMTP mail succeeds, but an error code other than SFERR_NO_ERROR will be set if it fails.
For the 2nd argument "reference": Same as the argument of the SFXSMTPSender::SendMessage or SFXSMTPSender::SendText function (in general, instance of the SFXSMTPSender class).
If sending a SMTP mail fails, call the SFXSMTPSender::GetProgress function in the callback function to get the stage where an error occurred.
enum ProgressEnum { PROGRESS_NONE, PROGRESS_CONNECT, PROGRESS_EHLO, PROGRESS_HELO, PROGRESS_AUTH, PROGRESS_AUTHRESP1, PROGRESS_AUTHRESP2, PROGRESS_MAIL, PROGRESS_RCPT, PROGRESS_DATA, PROGRESS_DATATEXT, PROGRESS_QUIT, PROGRESS_ERRORQUIT, PROGRESS_DONE };
Constants that show the progress status of sending a SMTP mail with the SFXSMTPSender.
The following values can be obtained using the SFXSMTPSender::GetProgress function.
The connection is not started.
Connection to the SMTP server is in progress.
Waiting for the response of the EHLO command.
Waiting for the response of the HELO command
Waiting for the response of the AUTH command
Waiting for the response regarding the command of the first SMTP authorization.
Waiting for the response regarding the command of the second SMTP authorization.
Waiting for the response of the MAIL command.
Waiting for the response of the RCPT command.
Waiting for the response of the DATA command.
Waiting for the response of the message text following the DATA command.
Waiting for the response of the QUIT command.
An error occurs in sending a SMTP mail.
Sending a SMTP mail is finished.
Copyright(c) 2002 - 2024 Sophia Cradle Incorporated All Rights Reserved. |