🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #993

Re: SMTP Authentication + Indy 10

#993
From: Remy Lebeau (TeamB)
Date: Fri, 22 Jun 2007 01:59
174 lines
6908 bytes
"JLoz" <jfsierra@excite.com> wrote in message
news:467b0944$1@newsgroups.borland.com...

> How do I go about selecting the right SASL Mechanism?

You don't have to.  TIdSMTP handles that automatically for you.  When
TIdSMTP connects to the server, it queries it to see which SASL
schemes are actually supported, and then checks to see which ones are
contained in the TIdSMTP::SASLMechanisms collection.  In your case,
the server will only support the AUTH LOGIN and AUTH PLAIN commands
(wrapped by the TIdSASLLogin and TIdSASLPlain components,
respectively) as is evident by the AUTH entry in the EHLO reply after
sending the STARTTLS command.

> After seeing that I selected Login and Plain but it says
> "Authentication requiered" when I try to send the message.

That is because you did not send any AUTH command before sending the
MAIL FROM command.

> according to the Knowledge base dropping the SASL Components
> and include them in the SASLMechanisms property should be enough.

It is, provided that you also set the TIdSMTP::AuthType property to
atSASL.  Your code is conditionally setting it to either atSASL or
atNone based on your Header["Authenticate"] value.  I am guessing that
the value is 0, so atNone is being used.

>   IdMessage1->Body = List;

Do not use the Body property if attachments are involved.  The message
text needs to be in its own TIdText object in the MessageParts
collection instead.

>    File = new TIdAttachmentFile*[Header["NoOfFiles"].as_int()];
>    for (int i = 0; i < Header["NoOfFiles"].as_int(); i++) {
>        File[i] = new
TIdAttachmentFile(IdMessage1->MessageParts,Header[dstring(i+1)]);
>    }

Why are you keeping track of the attachments in an array like that?
You are not using the array for anything, so you are just wasting
memory.  TIdMessage owns the attachment objects, so you do not need to
free them manually after sending the message.  Simply Clear() the
message and they will be freed.  Besides, you are allocating the array
with 'new[]', but you are freeing it with 'delete' instead of
'delete[]', so you have a memory leak anyway.

> IdSMTP1->SendCmd("STARTTLS",j);

Do not send that command manually.  TIdSMTP sends that command
automatically for you when an SSL IOHandler is assigned, the UseTLS
property is not set to utNoTLSSupport, and the server supports the
STARTTLS command.

> IdSMTP1->SendCmd("EHLO",j);

Again, do not send that manually.  TIdSMTP's TLS support does that
automatically for you.

>         IdSMTP1->Authenticate();

Send() calls that automatically, so you do not need to call that
manually, either.

With that said, use this code instead:

    void __fastcall TEmail::SendEmail(TIniTable& Header)
    {
        int Count;
        dstring j;

        IdSMTP1->Host = Header["Host"];
        IdSMTP1->Port = Header["Port"].as_int();

        if( Header["Encripted"].as_int() )
        {
            IdSMTP1->IOHandler = IdSSLIOHandlerSocketOpenSSL1;
            IdSSLIOHandlerSocketOpenSSL1->PassThrough = true;
            IdSMTP1->UseTLS = utUseExplicitTLS;

            if( Header["EncriptionMethod"] == "TLSv1" )
                IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Method =
sslvTLSv1;
            else if( Header["EncriptionMethod"] == "SSLv2" )
                IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Method =
sslvSSLv2;
            else if( Header["EncriptionMethod"] == "SSLv23" )
                IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Method =
sslvSSLv23;
            else if( Header["EncriptionMethod"] == "SSLv3" )
                IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Method =
sslvSSLv3;
        }
        else
        {
            IdSMTP1->UseTLS = utNoTLSSupport;
            IdSMTP1->IOHandler = NULL;
        }

        if( Header["Authenticate"].as_int() )
        {
            IdSMTP1->AuthType = Idsmtp::atSASL;
            IdUserPassProvider1->Username = Header["UserName"];
            IdUserPassProvider1->Password = Header["Password"];
            IdSMTP1->Username =  Header["UserName"];
            IdSMTP1->Password = Header["Password"];
        }
        else
            IdSMTP1->AuthType = Idsmtp::atNone;

        IdMessage1->Clear();
        IdMessage1->From->Address = Header["From"];
        IdMessage1->Recipients->EMailAddresses = Header["To"];
        IdMessage1->Subject = Header["Subject"];

        Header.SetSection("$");
        if( Header["Attachments"].as_int() )
        {
            Header.SetSection("Body");
            Count = Header["NoOfLines"].as_int()

            TIdText *pText = new TIdText(IdMessage1, NULL);
            pText->ContentType = "text/plain";

            for(int i = 1; i <= Count; ++i)
                pText->Body->Add(Header[dstring(i)]);

            Header.SetSection("AttachmentList");
            Count = Header["NoOfFiles"].as_int();

            for(int i = 1; i <= Count; ++i)
                new TIdAttachmentFile(IdMessage1->MessageParts,
Header[dstring(i)]);

            IdMessage1->ContentType = "multipart/mixed";
        }
        else
        {
            Header.SetSection("Body");

            Count = Header["NoOfLines"].as_int();
            for(int i = 1; i <= Count; ++i)
                IdMessage1->Body->Add(Header[dstring(i)]);

            IdMessage1->ContentType = "text/plain";
        }

        Header.SetSection("$");
        try
        {
            IdSMTP1->Connect();
            try
            {
                IdSMTP1->Send(IdMessage1);
            }
            __finally
            {
                IdSMTP1->Disconnect(true);
            }
            Header.Set("Result", "1");
        }
        catch (const Exception& E)
        {
            Header.Set("Error", E.Message);
            Header.Set("Result", "0");
        }

        IdMessage1->Clear();
    }


Gambit
Technical Details
Message-ID: <467b2d42$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <467b0944$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,b4680f9741cacd31
X-Google-Attributes: gid11508e,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <467b0944$1@newsgroups.borland.com>
Subject: Re: SMTP Authentication + Indy 10
Date: Thu, 21 Jun 2007 18:59:23 -0700
Lines: 176
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.82.252.126
Message-ID: <467b2d42$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1182477634 75.82.252.126 (21 Jun 2007 19:00:34 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail