BREW POP / SMTP Mailer - 5 / 6 -
Receiving Mail
The SFXPOP3Receiver class is used to receive mail.
The handler for the Receive button of MainWindow executes mail reception.
After setting the information concerning the account and server, mail is obtained by Receive().
To receive and delete mail at the same time, ReceiveAndDelete() is used.
The POP3Callback function for receiving mail should be set up as the argument for Receive() or ReceiveAndDelete().
HANDLER_IMPLEMENT_VOIDCONTROL(MainWindow, OnButtonControl2, result, control)
{
// Prepare for and start receiving mail
SFCError error;
SFXConfigPtr option = SimpleMailer::GetOption();
// Set account ( user name and password )
_receiver.SetAccount(option->ReadSFXAnsiString(OPTION_USER, ""),
option->ReadSFXAnsiString(OPTION_PASSWORD, "")
,SFXPOP3Receiver::AUTH_ONLY_USERPASS );
//SFXPOP3Receiver::AUTH_ONLY_USERPASS is the password authorization,
//different with the mail servers
//in this case it is the yahoo.co.jp mail server
// Set address and port number of POP3 server
// Domain name is automatically resolved
_receiver.SetServer(SFXSocketAddress(
option->ReadSFXAnsiString(OPTION_POP3SERVER, ""),
option->ReadUInt16(OPTION_POP3PORT, 110)));
// Receive mail & register "POP3Callback" function
// whether to receive and delete mail or not
if (option->ReadBool(OPTION_ERASEMAIL, false)) {
// Receiving mail
error = _receiver.Receive(CALLBACK_FUNCTION(POP3Callback));
}
else {
// Receive and delete
error = _receiver.ReceiveAndDelete(CALLBACK_FUNCTION(POP3Callback));
}
if (error == SFERR_NO_ERROR) { // if no error
// Make dialog
_dialog = ::new SFRMessageDialog(SFXGraphics::GetDeviceRectangle()
.Deflate(20, 100), "Receiving...", "");
}
else {
// Make dialog
_dialog = ::new SFRMessageDialog(SFXGraphics::GetDeviceRectangle()
.Deflate(20, 100), "Reception failed.g", "OK");
}
}
Declare and Define "POP3Callback" function for receiving mail
Declare
class MainWindow : public SFRTitleWindow {
private:
...
CALLBACK_DECLARE_SFXPOP3RECEIVER(POP3Callback)
...
}
Define
Received mail is saved as an Array of pointers to the MailInfo struct.
MailInfo is defined as follows.
struct MailInfo {
UInt32 size;
SFXAnsiString uidl;
SFXAnsiString mail; // heaader and body
};
* The i th message is obtained by mailArray[i]->mail.
CALLBACK_IMPLEMENT_SFXPOP3RECEIVER(MainWindow, POP3Callback, error)
{
SFXWideString message;
SFXPropertyPtr mail;
SInt32 i;
if (_dialog != null) {
// Close "Receiving..." dialog
_dialog->Invoke(SFXEvent(SREVT_RESPONDER_TERMINATE,
SRP16_TERMINATE_INVOKE, true));
}
if (error == SFERR_NO_ERROR) {
message.Set("Successfull reception.");
}
else {
message.Set("Reception failed");
}
// Make dialog
_dialog = ::new SFRMessageDialog(SFXGraphics::GetDeviceRectangle()
.Deflate(20, 100), message, "OK");
if (error == SFERR_NO_ERROR) { // if no error
if (_dialog != null) {
// Registration of dialog handler
_dialog->RegisterHandler(SREVT_DIALOG, HANDLER_BEFORE,
HANDLER_FUNCTION(OnDialog));
}
mail = SimpleMailer::GetMail();
// Received mail is copied.
const SFXArray<SFXPOP3Receiver::MailInfoPtr>& mailArray =
_receiver.GetReceivedMailArray();
for (i = 0; i < mailArray.GetSize(); ++i) {
SFXPOP3Receiver::MailInfoPtr minfo = mailArray[i];
mail->Set(minfo->uidl, minfo->mail);
}
}
// Redrawing
SFRApplication::GetInstance()->Invoke(
SFXEvent(SREVT_RESPONDER_RENDER, SRP16_RENDER_INVOKE, false));
}
Display Received Mail
Received mail is displayed by ReceivedWindow.







