Re: SMPT Thread Program !

Giganews Newsgroups
Subject: Re: SMPT Thread Program !
Posted by:  Remy Lebeau (Indy Team) (no.spam@no.spam.com)
Date: Tue, 13 Feb 2007

"Edward. Z. F. M." <edward.z.f…@gmail.com> wrote in message
news:E67BE4BAE91AE340edward.z.f…@gmail.com...

> What i need to do to fix ma my SMPT Thread program ?

Re-write it.  Your code is completely wrong for what you are
attempting.  Try this instead:

    class TSMTPThread : public TThread
    {
    private:
        TIdSMTP *idSmtp;
        TIdMessage *idMess;
    protected:
        void __fastcall Execute();
    public:
        __fastcall TSMTPThread(const String& _FromMail, const String&
_MailSubject, TStrings *_s, const String& _SendDestMail, const String&
_CopySend, const String& _HideSend, const String& _SmtpServer, const
String& _User, const String& _Pass);
        __fastcall ~TSMTPThread();
    };

    __fastcall TSMTPThread::TSMTPThread(const String& _FromMail, const
String& _MailSubject, TStrings *_s, const String& _SendDestMail, const
String& _CopySend, const String& _HideSend, const String& _SmtpServer,
const String& _User, const String& _Pass)
        : TThread(true)
    {
        FreeOnTerminate = true;

        idSmtp = new TIdSMTP(NULL);
        idSmtp->AuthenticationType = atLogin;
        idSmtp->Host = _SmtpServer;
        idSmtp->Username = _User;
        idSmtp->Password = _Pass;

        idMess = new TIdMessage(NULL);
        idMess->From->Text = _FromMail;
        idMess->Body->Assign(_s);
        idMess->Recipients->EMailAddresses = _SendDestMail;
        idMess->BccList->EMailAddresses = _HideSend;
        idMess->CCList->EMailAddresses = _CopySend;
        idMess->Priority = mpNormal;
        idMess->Subject = _MailSubject;

        Resume();
    }

    __fastcall TSMTPThread::~TSMTPThread()
    {
        delete idSmtp;
        delete idMess;
    }

    void __fastcall TSMTPThread::Execute()
    {
        try
        {
            idSmtp->Connect();
            try
            {
                idSmtp->Send(idMess);
            }
            __finally
            {
                idSmtp->Disconnect();
            }
        }
        catch(const Exception &e)
        {
            MessageBox(NULL, e.Message.c_str(), "SMTP Error", MB_OK |
MB_ICONERROR);
        }
    }

    class TFormMain : public TForm
    {
    __published: // IDE-managed Components
        TButton *ButtonSendThread;
        void __fastcall ButtonSendThreadClick(TObject *Sender);
    private: // User declarations
    public:  // User declarations
        __fastcall TFormMain(TComponent* Owner);
    };

    __fastcall TFormMain::TFormMain(TComponent* Owner)
        : TForm(Owner)
    {
    }

    void __fastcall TFormMain::ButtonSendThreadClick(TObject *Sender)
    {
        for(int i = 0; i < RichEditNewletters->Lines->Count; ++i)
        {
            RichEditMsg->Lines->Add(Now());
            EditSubject->Text = Now();

            new TSMTPThread(User, EditSubject->Text,
RichEditMsg->Lines, RichEditNewletters->Lines->Strings[i], "", "",
SmtpServer, User, Senha);

            Label2->Caption = i;
            Application->ProcessMessages();
        }
    }

> What is the max number of therads i can use in SMTP ?

There is no hard-coded limit.  The number of threads is limited only
by available OS resources.

Gambit

Replies

In response to

SMPT Thread Program ! posted by Edward. Z. F. M. on Wed, 14 Feb 2007