![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |

Procedure to send a SMTP mail
Example 485. The method to send a mail using the SFXSMTPSender class
class MyClass {
private:
SFXSMTPSender _sender;
CALLBACK_DECLARE_SFXSMTPSENDER(SMTPCallback)
public:
Void Start(Void);
};
Void MyClass::Start(Void)
{
SFCError error;
SFXMailMessage message;
// set From field to mail header
message.SetFromField("fromaddress@example.com");
// add To addresses to mail header
message.AddToField("toaddress1@example.com");
message.AddToField("toaddress2@example.com");
// 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 completion of sending mail
if ((error = _sender.SendMessage(&message,
CALLBACK_FUNCTION(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 completion of sending mail
CALLBACK_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);
}
}
The SFXMailMessage class instance should set the From field and the To field. The Cc and Bcc fileds can be set if neccessary.
The mail can be sent by SFXSMTPSender::SendText function without using 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.
| Constructor/Destructor |
|---|
|
SFXSMTPSender( Void ) Constructor of SFXSMTPSender class.
|
|
~SFXSMTPSender( Void ) Destructor of 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 instance of the SFXSMTP class 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 the QUIT command has been sent or not.
|
| 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 the sender.
|
| SFCError |
SetHostName(
SFXAnsiStringConstRef host
) Set the host name of the 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 Prototype of the callback function that is called when sending a SMTP mail is finished.
|
|
ProgressEnum Constants that show the progress statuses of sending a SMTP mail.
|
[ public, explicit ] SFXSMTPSender(Void);
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.
[ public ] ~SFXSMTPSender(Void);
When sending a SMTP mail is in progress, release this instance after canceling it using the SFXSMTPSender::Cancel function.
[ public ] SFCError AddTo( SFXAnsiStringConstRef to // mail address of recipient );
Mail address of recipient.
Mail address needs to be enclosed by "<>".
Specify the <admin@example.com> string if you want to send a mail to admin@example.com.
To set more than one mail address of recipient, call the SFXSMTPSender::AddTo function multiple times.
It is necessary to use the SFXSMTPSender::AddTo function before calling the SFXSMTPSender::SendText function.
[ public ] Void Cancel(Void);
Cancel the sending of a SMTP mail, and then all the allocated resources will be released.
[ public ] Void ClearTo(Void);
Delete 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 |
|---|---|
Detailed information : SFXSMTPSender::ProgressEnum | |
If sending a SMTP mail failed, call the SFXSMTPSender::GetProgress 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 |
|---|---|
| Detailed information: 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 );
Set the SMTP server using the SFXSMTPSender::SetServer function before sending.
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 the recipients are the To / Cc / Bcc fields included in the mail message header. The Bcc fields are deleted and sent.
![]() |
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 the SFXSMTPSender::SendMessage function. | |
SFXSMTPSender::SetServer | SFXSMTPSender::SetHostName | SFXSMTPSender::SetFrom | SFXSMTPSender::AddTo |
[ public ] SFCError SendText( SFXAnsiStringConstRef text // text to send CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
Send 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 the SFXSMTPSender::SendText 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 SFXSMTPSender::SendText function. | |
SFXSMTPSender::SendMessage | SFXSMTPSender::SetServer | SFXSMTPSender::SetHostName | SFXSMTPSender::SetFrom | SFXSMTPSender::AddTo
[ public ] SFCError SetAuthorization( AuthEnum auth // authorization method SFXAnsiStringConstRef user // username SFXAnsiStringConstRef password // password );
Specify the authorization method.
Related information: SFXSMTPSender::AuthEnum Constants
Specify username.
Specify password.
If the SFXSMTPSender::SetAuthorization 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);
}
[ public ] SFCError SetFrom( SFXAnsiStringConstRef from // sender's mail address );
Mail address of the sender
Mail address must be enclosed by "<>".
Specify <foobar@example.org> character string if you want to send mail to foobar@exapmle.org.
The SFXSMTPSender::SetFrom 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.
The SFXSMTPSender::SetHostName 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, set true to the isSSL parameter.
[ public ] SFCError SetServer( SFXSocketAddressConstRef server // IP address and port number of SMTP server );
IP address and port number of the SMTP server.
One of the following values are available for the SSL trust mode:
![]() |
Note |
|---|---|
| Detailed information: ISSL_NegotiateV in the BREW API Reference | |
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.
Related information: 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)
The SFXSMTPSender::CallbackSPP function is 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.
As 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.
As 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.
Sending a SMTP mail is finished.
|
Copyright (C) 2002 - 2009 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|