Send Email with Indy and SSL [Edit]

I must send a mail with Indy 10.0.1.1 and SSL
This is my code:

procedure SendEmail;
var
  IdMessage: TIdMessage;
  SMTP: TIdSMTP;
  SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
  IdUserPassProvider: TIdUserPassProvider;
  IdSASLCRAMMD5: TIdSASLCRAMMD5;
  IdSASLCRAMSHA1: TIdSASLCRAMSHA1;
  IdSASLPlain: TIdSASLPlain;
  IdSASLLogin: TIdSASLLogin;
  IdSASLSKey: TIdSASLSKey;
  IdSASLOTP: TIdSASLOTP;
  IdSASLAnonymous: TIdSASLAnonymous;
  IdSASLExternal: TIdSASLExternal;
begin
  IdMessage := TIdMessage.Create(nil);
  try
    IdMessage.ContentType := 'text/plain';
    IdMessage.Charset := 'UTF-8';
    IdMessage.Body.Text:='Prova di invio tramite SSL';
    IdMessage.Sender.Text := 'giovanellip@gmail.com';
    IdMessage.From.Name := 'Paolo Giovanelli';
    IdMessage.From.Address := 'giovanellip@gmail.com';
    IdMessage.ReplyTo.EMailAddresses := 'giovanellip@gmail.com';
    IdMessage.Recipients.EMailAddresses := 'giovanellip@gmail.com';
    IdMessage.Subject := 'Prova invio SSL';
    IdMessage.Priority := TIdMessagePriority.mpHighest;
    IdMessage.CCList.EMailAddresses := '';
    IdMessage.ReceiptRecipient.Text := '';
    IdMessage.BccList.EMailAddresses := '';

    SMTP := TIdSMTP.Create(nil);
    try
        SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
        SSLHandler.MaxLineAction := maException;
        SSLHandler.SSLOptions.Method := sslvSSLv23;
        SSLHandler.SSLOptions.Mode := sslmClient;
        SSLHandler.SSLOptions.VerifyMode := [];
        SSLHandler.SSLOptions.VerifyDepth := 0;
        SMTP.IOHandler := SSLHandler;
        SMTP.UseTLS := utUseExplicitTLS;

        SMTP.AuthType := satSASL;

        IdUserPassProvider := TIdUserPassProvider.Create(SMTP);
        IdUserPassProvider.Username := 'giovanellip@gmail.com';
        IdUserPassProvider.Password:= myPassword;   // my mail password

        IdSASLCRAMSHA1 := TIdSASLCRAMSHA1.Create(SMTP);
        IdSASLCRAMSHA1.UserPassProvider := IdUserPassProvider;
        IdSASLCRAMMD5 := TIdSASLCRAMMD5.Create(SMTP);
        IdSASLCRAMMD5.UserPassProvider := IdUserPassProvider;
        IdSASLSKey := TIdSASLSKey.Create(SMTP);
        IdSASLSKey.UserPassProvider := IdUserPassProvider;
        IdSASLOTP := TIdSASLOTP.Create(SMTP);
        IdSASLOTP.UserPassProvider := IdUserPassProvider;
        IdSASLAnonymous := TIdSASLAnonymous.Create(SMTP);
        IdSASLExternal := TIdSASLExternal.Create(SMTP);
        IdSASLLogin := TIdSASLLogin.Create(SMTP);
        IdSASLLogin.UserPassProvider := IdUserPassProvider;
        IdSASLPlain := TIdSASLPlain.Create(SMTP);
        IdSASLPlain.UserPassProvider := IdUserPassProvider;

        SMTP.SASLMechanisms.Add.SASL := IdSASLCRAMSHA1;
        SMTP.SASLMechanisms.Add.SASL := IdSASLCRAMMD5;
        SMTP.SASLMechanisms.Add.SASL := IdSASLSKey;
        SMTP.SASLMechanisms.Add.SASL := IdSASLOTP;
        SMTP.SASLMechanisms.Add.SASL := IdSASLAnonymous;
        SMTP.SASLMechanisms.Add.SASL := IdSASLExternal;
        SMTP.SASLMechanisms.Add.SASL := IdSASLLogin;
        SMTP.SASLMechanisms.Add.SASL := IdSASLPlain;

        SMTP.Host := 'smtp.gmail.com';
        SMTP.Port := 587;
        SMTP.ConnectTimeout := 30000;
        SMTP.UseEHLO := True;
        SMTP.Connect;
        try
          SMTP.Send(IdMessage);
        finally
          SMTP.Disconnect;
        end;
    finally
      SMTP.Free;
    end;
  finally
    IdMessage.Free;
  end;
end;

I connect to gmail server but when I send my email I have this excetpion: "Connection closed gracefully" and the email is not sent
Can someone help me?
0
Paolo
12/9/2013 3:06:12 PM
embarcadero.delphi.non-tech 5933 articles. 1 followers. Follow

6 Replies
6387 Views

Similar Articles

[PageSpeed] 54

>      IdMessage.BccList.EMailAddresses := '';
>
>      SMTP := TIdSMTP.Create(nil);
>      try
>          SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
>          SSLHandler.MaxLineAction := maException;
>          SSLHandler.SSLOptions.Method := sslvSSLv23;
>          SSLHandler.SSLOptions.Mode := sslmClient;
>          SSLHandler.SSLOptions.VerifyMode := [];
>          SSLHandler.SSLOptions.VerifyDepth := 0;
>          SMTP.IOHandler := SSLHandler;
>          SMTP.UseTLS := utUseExplicitTLS;


I use these options and it works well:
            sslIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
            SMTP.IOHandler := sslIOHandler;
            SMTP.UseTLS := utUseRequireTLS;

kind regards
    Mike
0
Michael
12/9/2013 3:48:33 PM
I try this change to my code:

// old code version

//        SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
//        SSLHandler.MaxLineAction := maException;
//        SSLHandler.SSLOptions.Method := sslvSSLv23;
//        SSLHandler.SSLOptions.Mode := sslmClient;
//        SSLHandler.SSLOptions.VerifyMode := [];
//        SSLHandler.SSLOptions.VerifyDepth := 0;
//        SMTP.IOHandler := SSLHandler;
//        SMTP.UseTLS := utUseExplicitTLS;

// new code version        
        SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create;
        SMTP.IOHandler := SSLHandler;
        SMTP.UseTLS := utUseRequireTLS;

but it doesn' t run again: always the same exception.
the change is wrong?
0
Paolo
12/9/2013 3:58:33 PM
Paolo wrote:

> SSLHandler.SSLOptions.Method := sslvSSLv23;

Use either sslvSSLv3 or sslvTLSv1 instead.

--
Remy Lebeau (TeamB)
0
Remy
12/9/2013 8:12:48 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Paolo wrote:
> 
> > SSLHandler.SSLOptions.Method := sslvSSLv23;
> 
> Use either sslvSSLv3 or sslvTLSv1 instead.
> 
> --
> Remy Lebeau (TeamB)

I try with both but I have always the same result
Any idea?
Greetings Paolo
0
Paolo
12/10/2013 8:19:17 AM
>
> I try with both but I have always the same result
> Any idea?

Any chance that you first try that with a normal mail client like
Thunderbird?

kind regards
   Mike
0
Michael
12/10/2013 8:47:07 AM
Paolo wrote:

> I must send a mail with Indy 10.0.1.1 and SSL
> This is my code:

Using the latest Indy 10 SVN snapshot, the exact code you showed works as-is 
for me using my GMail account.  The only thing I had to change was the GMail 
username/password and the sender/recipient addresses, nothing else was changed. 
 The email was sent correctly as expected.

Do you have 2-factor authentication enabled on your GMail account?  If so, 
make sure you are using a 2-factor application password and not your main 
GMail password.

--
Remy Lebeau (TeamB)
0
Remy
12/10/2013 6:39:50 PM
Reply:

Web resources about - Send Email with Indy and SSL [Edit] - embarcadero.delphi.non-tech

Edits Quarterly
“Short narratives in film and photography”, by Ian Coyle. Do yourself a favor and view it on a big display in Safari in full-screen mode.

File:Edit-copy purple.svg - Wikipedia, the free encyclopedia
This file contains additional information, probably added from the digital camera or scanner used to create or digitize it.If the file has been ...

Articles with edits awaiting review - Wikipedia, the free encyclopedia
As part of a Request for Comment on pending-changes protection , a third phase considered its use in the short term. In accordance with consensus ...

Facebook Android App Update Includes Privacy Settings Icon, Structured Status Updates, Ability To Edit ...
... Inside Facebook reported that the update also extends the availability of structured status updates , as well as the ability for users to edit ...

Nick Gordon digitally edits late Bobbi Kristina Brown in controversial Christmas card
He may be facing a US$40 million lawsuit for her wrongful death, but that didn't stop Nick Gordon from sharing a controversial Christmas card ...

Kate Middleton to guest-edit the UK section of The Huffington Post
The Duchess of Cambridge will guest-edit the UK section of The Huffington Post to highlight child mental health issues, Kensington Palace says. ...

Livestream’s new Movi camera allows users to edit video while filming live
... iOS app. Thanks to the high-resolution 4K sensor, users can select from up to nine different virtual camera angles. Users can even edit video ...

A legal storm is brewing over a startup backed by Bill Gates that wants to edit people's genes by 2017 ...
A patent fight has been brewing over a new gene-editing technology that could be worth hundreds of millions, and it could throw a wrench in the ...

Duchess Kate will guest-edit HuffPo UK for one day, from Kensington Palace
... to center around children’s issues? Well, Kate is going to do more work on that issue. For one day. In February. She’s going to guest-edit The ...

Gallery: The Daily Edit 12.14.15
Charlotte Observer Gallery: The Daily Edit 12.14.15 Charlotte Observer Real Madrid's Karim Benzema, second right, and Cristiano Ronaldo, second ...

Resources last updated: 1/17/2016 5:41:09 PM