3

Tips i followed is found here.

I do have libeay32.dll and ssleay32.dll in win32 folder.

dfm file:

object tidSMTP: TIdSMTP
    IOHandler = tidSMTP_SSL
    SASLMechanisms = <>
    UseTLS = utUseExplicitTLS
  end
  object tidSMTP_SSL: TIdSSLIOHandlerSocketOpenSSL
    Destination = 'smtp.gmail.com:587'
    Host = 'smtp.gmail.com'
    MaxLineAction = maException
    Port = 587
    DefaultPort = 0
    SSLOptions.Mode = sslmUnassigned
    SSLOptions.VerifyMode = []
    SSLOptions.VerifyDepth = 0
  end

and Send button click event:

procedure TForm1.btnSendClick(Sender: TObject);
var
  mes:TIdMessage;
  fromAddress:TIdEmailAddressItem;
  toAddress:TIdEMailAddressItem;
begin
  tidSMTP.Username := txtUsername.Text;
  tidSMTP.Password := txtPassword.Text;
  tidSMTP.Host := txtSMTPserver.Text;           //smtp.gmail.com
  tidSMTP.Port := StrToInt(txtSMTPport.Text);   //587

  fromAddress := TIdEMailAddressItem.Create;
  fromAddress.Address := txtUsername.Text;

  toAddress := TIdEMailAddressItem.Create;
  toAddress.Address := txtTo.Text;

  mes := TIdMessage.Create;
  mes.ContentType := 'text/plain';
  mes.From := fromAddress;
  mes.ReceiptRecipient := toAddress;
  mes.Subject := txtSubject.Text;

  mes.Body := memoText.Lines;

  tidSMTP.Connect;
  tidSMTP.Send(mes);
  tidSMTP.Disconnect;
end;

Any help would be appreciated!

Peacelyk
  • 1,126
  • 4
  • 24
  • 48

2 Answers2

4

Set you SSL Method to SSL version 3 (tidSMTP_SSL.SSLOptions.Method). I think it defaults to SSL version 2, but GMail does not support that.

SSLOptions.Method := sslvSSLv3;

Edit:

You can log the SSL Status info by assigning an eventhandler to the OnStatusInfo event of your IOHandler:

tidSMTP_SSL.OnStatusInfo := DoOnStatusInfo;

proceudre TForm1.DoOnStatusInfo(Msg: string);
begin
  // when running from IDE, message will appear in 
  // EventLog (Ctrl+Alt+V), otherwise, 
  // use DebugViewer.exe
  OutputDebugString(PChar(Msg)); 
end;

Maybe this will give you a clue about the failing negotation.

PS: I'm on Indy 9.0.0.18, so things may have changed for you.

Edit2:

If above does not help, please check if there is not a firewall / antivirus that is blocking smtp.gmail.com or port 587

The_Fox
  • 6,992
  • 2
  • 43
  • 69
  • Thank you for your reply! Changing SSLOptions.Method doesn't solve that! I had it set to sslvTLSv1. – Peacelyk Apr 11 '11 at 09:38
  • Hey Fox, it turned out that my antivirus program blocked that port. Edit your post and i'll accept it as an answer! – Peacelyk Apr 11 '11 at 13:12
1

I successfully make it worked like this:

procedure TForm1.btn2Click(Sender: TObject);
var
  email      : TIdMessage;
  idSMTPGMail: TIdSMTP;
  idSSLGMail : TIdSSLIOHandlerSocketOpenSSL;
begin
  idSSLGMail                   := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  idSSLGMail.SSLOptions.Method := sslvTLSv1;
  idSSLGMail.SSLOptions.Mode   := sslmUnassigned;

  idSMTPGMail                  := TIdSMTP.Create(nil);
  idSMTPGMail.IOHandler        := idSSLGMail;
  idSMTPGMail.UseTLS           := utUseExplicitTLS;

  email                           := TIdMessage.Create(nil);
  email.From.Address              := txtUsername.Text;
  email.Recipients.EMailAddresses := txtTo.Text;
  email.Subject                   := txtSubject.Text;
  email.Body.Text                 := memoText.Text;

  idSMTPGMail.Host     := 'smtp.gmail.com';
  idSMTPGMail.Port     := 587;
  idSMTPGMail.UserName := txtUsername.Text;
  idSMTPGMail.Password := txtPassword.Text;

  idSMTPGMail.Connect;
  idSMTPGMail.Send(email);
  idSMTPGMail.Disconnect;

  email.Free;
  idSSLGMail.Free;
  idSMTPGMail.Free;
  Beep;
end;

I use the same TEdit, TMemo, but dynamically create the Indy components...

Whiler
  • 7,998
  • 4
  • 32
  • 56