![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |
There are three types of classes for sending and receiving mails.
Table 16.3. Classes for Sending and Receiving Mails
| Class Name | Description |
|---|---|
| SFXSMTPSender | Class for sending a SMTP mail. |
| SFXPOP3Receiver | Class for receiving a POP3 mail. |
| SFXMailMessage | Class for processing a mail message. |
The following classes are used to process SMTP and the POP3 protocol at the detailed level.
Table 16.4. SMTP/POP3 protocol classes
| Class Name | Description |
|---|---|
| SFXSMTP | Class for processing the SMTP protocol. |
| SFXPOP3 | Class for processing the POP3 protocol. |
| SFXMailUtility | Utility class for processing a mail message. |
| SFXMailField | Class for encode and decode the mail header. |
![]() |
About MIF File Setting |
|---|---|
Never forget to turn on the Network option in the MIF file setting of privilege level. | |
SFXSMTPSender class is used to send mails.
Example 16.13. Send mails
class MyClass {
private:
SFXSMTPSender _sender;
CALLBACK_DECLARE_SFXSMTPSENDER(SMTPCallback)
public:
Void Start(Void);
};
// precondition: sufficient memory
Void MyClass::Start(Void)
{
SFCError error;
SFXMailMessage message;
// set From Address
message.SetFromField("fromaddress@example.com");
// set To Address
message.AddToField("toaddress1@example.com");
message.AddToField("toaddress2@example.com");
// set title
message.SetSubjectField("Mail Subject");
// set body
message.SetBody("Mail test\r\nThis mail is sent by SFXSMTPSender.\r\n");
// set mail server and port number for mail sending (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) {
// if error occurs, SMTPCallback callback function will not be called
// error handling
...
}
}
// callback function notified of completion of sending mail
CALLBACK_IMPLEMENT_SFXSMTPSENDER(MyClass, SMTPCallback, error)
{
if (error == SFERR_NO_ERROR) {
TRACE("The mail has been sent.");
}
else {
TRACE("Failed to send the mail.%d", error);
}
}
SFXPOP3Receiver is used to receive mails.
Example 16.14. Receive mails
class MyClass {
private:
SFXPOP3Receiver _receiver;
CALLBACK_DECLARE_SFXPOP3RECEIVER(POP3Callback)
public:
Void Start(Void);
};
// precondition: sufficient memory
Void MyClass::Start(Void)
{
// set account
_receiver.SetAccount("user", "password");
// set mail server and portnumber for mail receiving (domain is automatically resolved)
_receiver.SetServer(SFXSocketAddress("pop3server.example.com:110"));
// register POP3Callback as callback function, and then receive mails
// POP3Callback function is notified of completion of receiving mails
if ((error = _receiver.Receive(CALLBACK_FUNCTION(POP3Callback)))
!= SFERR_NO_ERROR) {
// if error occurs, POP3Callback callback function will not be called
// error handling
...
}
}
// callback function notified of completion of receiving mails
CALLBACK_IMPLEMENT_SFXPOP3RECEIVER(MyClass, POP3Callback, error)
{
SInt32 i;
if (error == SFERR_NO_ERROR) {
// succeed to receive mails
// get mails
const SFXArray<SFXPOP3Receiver::MailInfoPtr>& mailArray =
receiver.GetReceivedMailArray();
// display the number of mails
TRACE("received %d mails", mailArray.GetSize());
for (i = 0; i < mailArray.GetSize() ; i++) {
SFXPOP3Receiver::MailInfoPtr minfo = mailArray[i];
// get (from left): mail size, UIDL, mail body including header
TRACE("%d, %s, %s", minfo->size, minfo->uidl.GetCString(), minfo->mail.GetCString());
}
}
}
The only difference between common mail sending and SSL mail sending is that, for SSL mail sending, SetSSLMode(), SetTrustMode(), and SetAuthorization() are used.
Example 16.15. Send mails through SSL
class MyClass {
private:
SFXSMTPSender _sender;
CALLBACK_DECLARE_SFXSMTPSENDER(SMTPCallback)
public:
Void Start(Void);
};
// precondition: sufficient memory
// bold lines are added
Void MyClass::Start(Void)
{
SFCError error;
SFXMailMessage message;
// set From address
message.SetFromField("fromaddress@example.com");
// set To address
message.AddToField("toaddress1@example.com");
message.AddToField("toaddress2@example.com");
// set title
message.SetSubjectField("Mail Subject");
// set mail body
message.SetBody("Mail test\r\nThis mail is sent by SFXSMTPSender.\r\n");
// set mail server and port number for mail sending (domain is automatically resolved)
_sender.SetServer(SFXSocketAddress("smtpserver.example.com:465"));
// SSL mail sending settings
_sender.SetSSLMode(true); // set SSL connect mode
_sender.SetTrustMode(SSL_TRUST_MODE_FAIL); // set certificate verification mode
_sender.SetAuthorization(AUTH_CRAM_MD5,"username","password"); // set SMPT authentication mode
// 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) {
// if error occurs, SMTPCallback callback function will not be called
// error handling
...
}
}
// callback function notified of completion of sending mail
CALLBACK_IMPLEMENT_SFXSMTPSENDER(MyClass, SMTPCallback, error)
{
if (error == SFERR_NO_ERROR) {
TRACE("The mail has been sent.");
}
else {
TRACE("Failed to send the mail.%d", error);
}
}
The only difference between common mail receiving and SSL mail receiving is that, for SSL mail receiving, SetSSLMode() and SetTrustMode() are used.
Example 16.16. Receive mails through SSL
class MyClass {
private:
SFXPOP3Receiver _receiver;
CALLBACK_DECLARE_SFXPOP3RECEIVER(POP3Callback)
public:
Void Start(Void);
};
// precondition: sufficient memory
// bold lines are added
Void MyClass::Start(Void)
{
// set account
_receiver.SetAccount("user", "password");
// set mail server and port number from mail receiving(domain is automically resolved)
_receiver.SetServer(SFXSocketAddress("pop3server.example.com:995"));
// SSL mail receiving settings
_receiver.SetSSLMode(true); // set SSL connect mode
_receiver.SetTrustMode(SSL_TRUST_MODE_FAIL); // set certificate verification mode
// register POP3Callback as callback function, and then receive mails
// POP3Callback function is notified of completion of receiving mails
if ((error = _receiver.Receive(CALLBACK_FUNCTION(POP3Callback)))
!= SFERR_NO_ERROR) {
// if error occurs, POP3Callback callback function will not be called
// error handling
...
}
}
// callback function notified of completion of receiving mails
CALLBACK_IMPLEMENT_SFXPOP3RECEIVER(MyClass, POP3Callback, error)
{
SInt32 i;
if (error == SFERR_NO_ERROR) {
// succeed to receive mails
// get mails
const SFXArray<SFXPOP3Receiver::MailInfoPtr>& mailArray =
receiver.GetReceivedMailArray();
// display the number of mails
TRACE("received %d mails", mailArray.GetSize());
for (i = 0; i < mailArray.GetSize() ; i++) {
SFXPOP3Receiver::MailInfoPtr minfo = mailArray[i];
// get (from left): mail size, UIDL, mail body including header
TRACE("%d, %s, %s", minfo->size, minfo->uidl.GetCString(), minfo->mail.GetCString());
}
}
}
Mail message is the string with header and body and is treated using the SFXMailMessage class.
Using the SFXMailMessage class, you can create the mail message to send and the files to attach, parse the header of received mail and so on.
Example 16.17. Create the mail message
SFXMailMessage message; // set From field of Header message.ReplaceFromField("fromaddress@example.com"); // set To field of Header (this function can be called multiple times) message.AddToField("toaddress1@example.com"); // set Subject field of Header message.ReplaceSubjectField("title"); // set mail body message.SetBody("Mail body"); // send mail message smtpSender.SendMessage(&message, CALLBACK_FUNCTION(SMTPCallback));
Example 16.18. Parse the received mail
// mail message which is received SFXAnsiString receivedString = "From: Albert Einstein <einstein@example.com>\r\n" "To: Isaac Newton <newton@example.com>\r\n" "Subject: untitled\r\n" "Date: Sat, 08 Mar 2008 20:37:58 +0900\r\n" "\r\n" "This is a mail body.\r\n"; SFXMailMessage message; // parse mail message which is received message.Parse(receivedString); // get From field of Header // fromString: "Albert Einstein <einstein@example.com>" SFXAnsiString fromString = message.GetFromField(); // get To field of Header // toString: "Isaac Newton <newton@example.com>" SFXAnsiString toString = message.GetToField(); // get Subject field of Header // subjectString: "untitled" SFXAnsiString subjectString = message.GetSubjectField(); // get Date field of Header // date: "Sat, 08 Mar 2008 20:37:58 +0900" SFXDate date = message.GetDateField(); // get mail body // bodyString: "This is a mail body.\r\n"; SFXAnsiString bodyString = message.GetBody();
|
Copyright (C) 2002 - 2008 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|