You are here:

Delphi/Sending email through Delphi code

Advertisement


Question
Hello!
I'm studying Delphi and I'm trying to make this example work, it's taken from "Teach Yourself Delphi 4 in 21 Days"
For the host I tried smtp.gmail.com, I also tried port
465, I tried imtp... the program connects to the server, but when I execute the code to send an email a messagedlg box pops up saying "incomplete header". I tried it in Delphi 4 as well as in 7, I can't find the way of making this work, it's very tiring going through a book and speding months trying things that never work...
Could you please help me out? I include the code I'm trying below. I also have installed Delphi 2010 if you wish to provide examples for 2010 version.
Thank!

SMTP.Host := `mail';
SMTP.Connect;

procedure TMainForm.SMTPConnect(Sender: TObject);
begin
 with SMTP.PostMessage do begin
   FromAddress := `';
   ToAddress.Add(`');
   Subject := `Test';
   Body.Add(`This is a test');
 end;
 SMTP.SendMail;
end;

Answer
Gmail requires SSL and using the stock components can certainly be tricky.  I personally use Clever Internet Suite which handles the SSL internally.  Using the stock Indy 10 components, I have been able to connect and send an email message.  

Delphi Versions may affect the code a little, but the most important factor is downloading the correct OpenSSL libraries.  I found one compatible to Delphi 2007 here: https://web.archive.org/web/20070201000000*/http://indy.fulgan.com/SSL/indy_OpenSSL096m.zip
Newer versions of Delphi will be able to use OpenSSL from here: http://indy.fulgan.com/SSL/

Here is the code and the form:

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdBaseComponent, IdComponent, IdTCPConnection,
 IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdSMTPBase, IdSMTP, StdCtrls, IdMessage, IdIntercept,
 IdInterceptSimLog, IdLogBase, IdLogStream;

type
 TForm1 = class(TForm)
   Button1: TButton;
   SMTP: TIdSMTP;
   IOHandlerSSL: TIdSSLIOHandlerSocketOpenSSL;
   EmailMessage: TIdMessage;
   Memo1: TMemo;
   IdLogStream: TIdLogStream;
   Button2: TButton;
   procedure Button1Click(Sender: TObject);
   procedure IOHandlerSSLStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string);
   procedure IdLogStreamConnect(ASender: TIdConnectionIntercept);
   procedure IdLogStreamReceive(ASender: TIdConnectionIntercept; var ABuffer: TBytes);
   procedure IdLogStreamSend(ASender: TIdConnectionIntercept; var ABuffer: TBytes);
   procedure Button2Click(Sender: TObject);
 private
 public
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 EmailMessage.Clear;
 EmailMessage.From.Address := 'youremailhere@gmail.com';
 EmailMessage.Recipients.Add.Address := 'recipient@domain.com';
 EmailMessage.Subject := 'This is a test';
 EmailMessage.Body.Add('This is the body of my test');

 SMTP.Host := 'smtp.Gmail.com';
 SMTP.UseTLS := utUseImplicitTLS;
 SMTP.Connect;

 SMTP.Send(EmailMessage);
 Memo1.Lines.Add('Sent');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 SMTP.Disconnect;
end;

procedure TForm1.IdLogStreamConnect(ASender: TIdConnectionIntercept);
begin
 Memo1.Lines.Add('Connect');
end;

procedure TForm1.IdLogStreamReceive(ASender: TIdConnectionIntercept; var ABuffer: TBytes);
var
 AString: string;
begin
 SetLength(AString, Length(ABuffer));
 Move(ABuffer[0], AString[1], Length(ABuffer));
 Memo1.Lines.Add('Recv: '+AString);
end;

procedure TForm1.IdLogStreamSend(ASender: TIdConnectionIntercept; var ABuffer: TBytes);
var
 AString: string;
begin
 SetLength(AString, Length(ABuffer));
 Move(ABuffer[0], AString[1], Length(ABuffer));
 Memo1.Lines.Add('Send: '+AString);
end;

procedure TForm1.IOHandlerSSLStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string);
begin
 Memo1.Lines.Add(AStatusText);
end;

end.


Unit1.dfm:

object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClientHeight = 300
 ClientWidth = 635
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 PixelsPerInch = 96
 TextHeight = 13
 object Button1: TButton
   Left = 32
   Top = 32
   Width = 75
   Height = 25
   Caption = 'Button1'
   TabOrder = 0
   OnClick = Button1Click
 end
 object Memo1: TMemo
   Left = 113
   Top = 24
   Width = 514
   Height = 268
   Lines.Strings = (
     'Memo1')
   TabOrder = 1
 end
 object Button2: TButton
   Left = 32
   Top = 63
   Width = 75
   Height = 25
   Caption = 'Button2'
   TabOrder = 2
   OnClick = Button2Click
 end
 object SMTP: TIdSMTP
   IOHandler = IOHandlerSSL
   MailAgent = 'Delphi2007'
   PipeLine = False
   Host = 'mail.gmail.com'
   Password = 'password'
   SASLMechanisms = <>
   UseTLS = utUseRequireTLS
   Username = 'youremail@gmail.com'
   Left = 24
   Top = 104
 end
 object IOHandlerSSL: TIdSSLIOHandlerSocketOpenSSL
   OnStatus = IOHandlerSSLStatus
   Destination = 'mail.gmail.com:25'
   Host = 'mail.gmail.com'
   Intercept = IdLogStream
   MaxLineAction = maException
   Port = 25
   DefaultPort = 0
   SSLOptions.Method = sslvSSLv3
   SSLOptions.Mode = sslmUnassigned
   SSLOptions.VerifyMode = []
   SSLOptions.VerifyDepth = 0
   Left = 48
   Top = 104
 end
 object EmailMessage: TIdMessage
   AttachmentEncoding = 'MIME'
   BccList = <>
   CCList = <>
   Encoding = meDefault
   FromList = <
     item
     end>
   Recipients = <>
   ReplyTo = <>
   ConvertPreamble = True
   Left = 96
   Top = 104
 end
 object IdLogStream: TIdLogStream
   OnConnect = IdLogStreamConnect
   OnReceive = IdLogStreamReceive
   OnSend = IdLogStreamSend
   Active = True
   Left = 72
   Top = 104
 end
end


Requiring the correct DLLs for SSL is one of the factors that led me to using Clever Internet Suite as their implementation has its own SSL without even using DLLs.

Hope this helps.

Randy

Delphi

All Answers


Answers by Expert:


Ask Experts

Volunteer


Randy Sill

Expertise

Turbo Pascal and Delphi developer since 1986. Strengths - UI, Windows API, Database, SQL, Internet, Threading, NT Services.

Experience

Turbo Pascal and Delphi developer since 1986.

Education/Credentials
Delphi 5 Certification, Borland

Awards and Honors
2005 Indiana IT/Software Million Dollar Award, Key Technical Contributor

©2016 About.com. All rights reserved.