Send mail via Gmail

Hi,

I'm trying to send emails via Gmail, but the application hangs during the sending process.

I'm using Delphi 2007 and Indy 10
Added two SSL DLL's to my application's folder:
libeay32.dll   and   ssleay32.dll

I've tried sending through ports 465 and 587.
And     SSLOptions.Method     sslvSSLv3   and   sslvSSLv2   and   sslvSSLv1


What is wrong with this code?
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdSMTPBase,
  IdSMTP, IdMessage, DB, ADODB, DBCtrls, IdIOHandler, IdIOHandlerSocket,
  IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdGlobal;

type
  TForm1 = class(TForm)
    IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
    IdSMTP1: TIdSMTP;
    IdMessage1: TIdMessage;
    Button1: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure IdSMTP1Status(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: string);
    procedure IdSSLIOHandlerSocketOpenSSL1Status(ASender: TObject;
      const AStatus: TIdStatus; const AStatusText: string);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  idsmtp1.Connect;
  idsmtp1.Send(idmessage1);
  idsmtp1.Disconnect;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  with IdSSLIOHandlerSocketOpenSSL1 do
  begin
    Destination            := 'smtp.gmail.com' + ':' + IntToStr(465);
    Host                   := 'smtp.gmail.com';
    MaxLineAction          := maException;
    Port                   := 465;
    SSLOptions.Method      := sslvTLSv1;
    SSLOptions.Mode        := sslmUnassigned;
    SSLOptions.VerifyMode  := [];
    SSLOptions.VerifyDepth := 0;
  end;
  with IdSMTP1 do
  begin
    IOHandler          := IdSSLIOHandlerSocketOpenSSL1;
    Host               := 'smtp.gmail.com';
    Port               := 465;
    AuthType           := atDefault;
    Password           := 'mypassword';
    Username           := 'mymail@gmail.com';
    UseTLS             := utUseRequireTLS;
  end;
  with IdMessage1 do
  begin
    From.Address              := 'mymail@gmail.com';
    Recipients.EMailAddresses := 'yourmail@gmail.com';
    Subject                   := 'Test';
    Body.Text                 := Memo1.Lines.Text;
  end;
end;

procedure TForm1.IdSMTP1Status(ASender: TObject; const AStatus: TIdStatus;
  const AStatusText: string);
begin
  Memo2.Lines.Add('IdSMTP: ' + AStatusText);
end;

procedure TForm1.IdSSLIOHandlerSocketOpenSSL1Status(ASender: TObject;
  const AStatus: TIdStatus; const AStatusText: string);
begin
  Memo2.Lines.Add('IdSSL: ' + AStatusText);
end;

end.

Open in new window

object Form1: TForm1
  Left = 192
  Top = 114
  Caption = 'Form1'
  ClientHeight = 684
  ClientWidth = 1027
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 345
    Top = 302
    Width = 153
    Height = 25
    Caption = 'Send via Gmail'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Memo1: TMemo
    Left = 8
    Top = 53
    Width = 490
    Height = 243
    Lines.Strings = (
      'Dear,'
      ''
      'My body of my mail.'
      ''
      'Greetings')
    ScrollBars = ssBoth
    TabOrder = 1
  end
  object Memo2: TMemo
    Left = 536
    Top = 0
    Width = 491
    Height = 684
    Align = alRight
    ScrollBars = ssVertical
    TabOrder = 2
  end
  object IdSMTP1: TIdSMTP
    OnStatus = IdSMTP1Status
    IOHandler = IdSSLIOHandlerSocketOpenSSL1
    HeloName = 'csd'
    Host = 'smtp.gmail.com'
    Password = 'mypass'
    Port = 587
    SASLMechanisms = <>
    UseTLS = utUseRequireTLS
    Username = 'sender@gmail.com'
    Left = 80
    Top = 24
  end
  object IdMessage1: TIdMessage
    AttachmentEncoding = 'UUE'
    Body.Strings = (
      'ceva')
    BccList = <>
    CCList = <>
    Encoding = meDefault
    FromList = <
      item
        Address = 'sender@gmail.com'
        Text = 'sender@gmail.com'
        Domain = 'gmail.com'
        User = 'sender'
      end>
    From.Address = 'sender@gmail.com'
    From.Text = 'sender@gmail.com'
    From.Domain = 'gmail.com'
    From.User = 'sender'
    ReceiptRecipient.Address = 'receiver@yahoo.com'
    ReceiptRecipient.Text = 'receiver@yahoo.com'
    ReceiptRecipient.Domain = 'yahoo.com'
    ReceiptRecipient.User = 'receiver'
    Recipients = <>
    ReplyTo = <>
    Sender.Address = 'sender@gmail.com'
    Sender.Text = 'sender@gmail.com'
    Sender.Domain = 'gmail.com'
    Sender.User = 'sender'
    ConvertPreamble = True
    Left = 120
    Top = 24
  end
  object IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL
    OnStatus = IdSSLIOHandlerSocketOpenSSL1Status
    Destination = 'smtp.gmail.com:587'
    Host = 'smtp.gmail.com'
    MaxLineAction = maException
    Port = 587
    DefaultPort = 0
    SSLOptions.Method = sslvSSLv2
    SSLOptions.Mode = sslmUnassigned
    SSLOptions.VerifyMode = []
    SSLOptions.VerifyDepth = 0
    Left = 160
    Top = 24
  end
end

Open in new window

Stef MerlijnDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
chenge IDSmtp1.UseTLS   to utUseRequireTLS;

This works for me with gmail and 465

My code just to see how it works for me
IdMsgSend := TIdMessage.Create(nil);
  try
    with IdMsgSend do
    begin
      ContentType := 'multipart/mixed';
      Body.AddStrings(Memo1.lines);
      From.Text := lblemail.Text;
      ReplyTo.EMailAddresses := lblemail.Text;
      Recipients.EMailAddresses := lblto.Text;
      Subject := lblsubject.Text;
      if OpenDialog1.Execute then
      begin
        TIdAttachmentFile.Create(IdMsgSend.MessageParts, OpenDialog1.FileName);
      end;
      s := TMemoryStream.Create;
      savetostream(s, false);
      ProgressBar.Max := s.Size;
      s.Free;
    end;
    SMTP := TIdSMTP.Create(nil);
    try
      SMTP.Username := lblemail.Text; // gmail account
      SMTP.Password := lblpwd.Text;
      SMTP.Host := 'smtp.gmail.com';
      SMTP.Port := 465;
      SMTP.OnWork := IdSMTPWork;
      SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
      SMTP.IOHandler := SSLHandler;
      SSLHandler.SSLOptions.Method := sslvTLSV1;
      SMTP.AuthType := satDefault;
      SMTP.UseTLS := utUseImplicitTLS;
      SMTP.Connect;
      try
        SMTP.Send(IdMsgSend);
      finally
        SMTP.Disconnect;
      end;
    finally
      FreeAndNil(SSLHandler);
      FreeAndNil(SMTP);
    end;
  finally
    FreeAndNil(IdMsgSend);
  end;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stef MerlijnDeveloperAuthor Commented:
Hi,

I've changed it to:
      SMTP.UseTLS := utUseImplicitTLS;

Now it gives an errormessage: "Could not load SSL library."
Stef MerlijnDeveloperAuthor Commented:
In the mean time I tried several different versions of the SSL library? Not one works.
I need to redistribute them on x64 and x86 systems.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
mmm, I don't use any ssl library, and it works both on x64 and x86
Stef MerlijnDeveloperAuthor Commented:
Which version of INDY do you use?
I use the one that is installed with Delphi 2007, so not the latest snapshot.
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Indy 10.
Anyway, surfing a ltl the net, you'll discover that many people can use gmail account with Indy without dlls.
Stef MerlijnDeveloperAuthor Commented:
Maybe you can point those sites out to me, as I searched a lot on this subject and didn't find a working solution yet.
Stef MerlijnDeveloperAuthor Commented:
I got it working on a 32bit computer with OpenSSL 096.
Thanks for the input.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.