• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2327
  • Last Modified:

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

0
Stef Merlijn
Asked:
Stef Merlijn
  • 5
  • 3
2 Solutions
 
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

0
 
Stef MerlijnDeveloperAuthor Commented:
Hi,

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

Now it gives an errormessage: "Could not load SSL library."
0
 
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
mmm, I don't use any ssl library, and it works both on x64 and x86
0
 
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.
0
 
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.
0
 
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.
0
 
Stef MerlijnDeveloperAuthor Commented:
I got it working on a 32bit computer with OpenSSL 096.
Thanks for the input.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now