My sample to Send Email using Indy components Delphi and Gmail account with SSL protocol
Emailx45 - 06/Jun/2020
Emailx45 - 06/Jun/2020
[SHOWTOGROUPS=4,20]
Here my sample for create a app to send email using Indy components on Delphi or maybe in C++Builder too! (I dont know nothing about C++)
ONLY 3 INDY Components:
IMPORTANT NOTE: running your app by IDE (F9 key ) with or without DEBUG, you will receive the error message showing error "Socket Error # 10013 - Access denied"
Как увидеть ссылки? | How to see hidden links?
Solution: Execute your application directly on Explorer Windows (out of your IDE environment)
my simple VCL app create in Delphi - RAD Studio 10.3.3 Arch - nothing advanced ok? Only for test process...
[/SHOWTOGROUPS]
Here my sample for create a app to send email using Indy components on Delphi or maybe in C++Builder too! (I dont know nothing about C++)
ONLY 3 INDY Components:
- IdSSLIOHandlerSocketOpenSSL
- IdMessage
- IdSMTP
- of course, you can create it on-the-fly!!!
IMPORTANT NOTE: running your app by IDE (F9 key ) with or without DEBUG, you will receive the error message showing error "Socket Error # 10013 - Access denied"
Как увидеть ссылки? | How to see hidden links?
Solution: Execute your application directly on Explorer Windows (out of your IDE environment)
Код:
To send emails using your non-standard Google or Other app, you must enable Gmail's 2-step verification.
This way, you will have more security when an application will use your account to send emails.
1) Go to https://myaccount.google.com/
2) On the left side, access the "Security" option
3) Now activate "2-step verification" for your Gmail email account
4) Gmail will ask for confirmation that you really are making this change ... complete the task.
5) Now that your account has "2-step verification" enabled, you will ask GMail to generate an app password for you to use in your app.
6) This password is 16 characters long, and will be used in your app instead of your GMail account password. Thus, GMail guarantees that your password will be preserved from access.
7) This application password has a validity, so read all the information from GMail.
8) Ready! Now you just have to configure your app to use your 16 character account and app password, instead of your real password.
my simple VCL app create in Delphi - RAD Studio 10.3.3 Arch - nothing advanced ok? Only for test process...
Код:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 525
ClientWidth = 699
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 96
TextHeight = 13
object lbEmailBody: TLabel
Left = 8
Top = 251
Width = 55
Height = 13
Caption = 'Email body:'
end
object lbedtEmailTo: TLabeledEdit
Left = 8
Top = 184
Width = 401
Height = 21
EditLabel.Width = 41
EditLabel.Height = 13
EditLabel.Caption = 'Email to:'
TabOrder = 0
Text = 'WHO-ARE-TARGET-EMAIL@gmail.com'
end
object lbedtEmailSubject: TLabeledEdit
Left = 8
Top = 224
Width = 401
Height = 21
EditLabel.Width = 40
EditLabel.Height = 13
EditLabel.Caption = 'Subject:'
TabOrder = 1
Text = 'Testing send email from Delphi using Indy'
end
object mmEmailBody: TMemo
Left = 8
Top = 270
Width = 401
Height = 251
Lines.Strings = (
'To send emails using your non-standard Google or Other app, you ' +
'must enable '
'Gmail'#39's 2-step verification.'
'This way, you will have more security when an application will u' +
'se your account '
'to send emails.'
'1) Go to https://myaccount.google.com/'
'2) On the left side, access the "Security" option'
'3) Now activate "2-step verification" for your Gmail email accou' +
'nt'
'4) Gmail will ask for confirmation that you really are making th' +
'is change ... '
'complete the task.'
'5) Now that your account has "2-step verification" enabled, you ' +
'will ask GMail to '
'generate an app password for you to use in your app.'
'6) This password is 16 characters long, and will be used in your' +
' app instead of '
'your GMail account password. Thus, GMail guarantees that your pa' +
'ssword will be '
'preserved from access.'
'7) This application password has a validity, so read all the inf' +
'ormation from '
'GMail.'
'8) Ready! Now you just have to configure your app to use your 16' +
' character '
'account and app password, instead of your real password.')
TabOrder = 2
end
object btnEmailSend: TButton
Left = 415
Top = 355
Width = 98
Height = 25
Caption = 'Send...'
TabOrder = 3
OnClick = btnEmailSendClick
end
object lbedtEmailFrom: TLabeledEdit
Left = 8
Top = 99
Width = 401
Height = 21
EditLabel.Width = 55
EditLabel.Height = 13
EditLabel.Caption = 'Email From:'
TabOrder = 4
Text = 'WHO-ARE-SEND-THIS-EMAIL@gmail.com'
end
object lbedtEmailHost: TLabeledEdit
Left = 8
Top = 16
Width = 401
Height = 21
EditLabel.Width = 53
EditLabel.Height = 13
EditLabel.Caption = 'Email Host:'
TabOrder = 5
Text = 'smtp.gmail.com'
end
object lbedtHostPort: TLabeledEdit
Left = 416
Top = 16
Width = 73
Height = 21
EditLabel.Width = 24
EditLabel.Height = 13
EditLabel.Caption = 'Port:'
TabOrder = 6
Text = '465'
end
object lbedtHostUserName: TLabeledEdit
Left = 8
Top = 56
Width = 233
Height = 21
EditLabel.Width = 56
EditLabel.Height = 13
EditLabel.Caption = 'User Name:'
TabOrder = 7
Text = 'YOUR-ACCOUNT-ON-GMAIL@gmail.com'
end
object lbedtHostPassword: TLabeledEdit
Left = 312
Top = 56
Width = 313
Height = 21
EditLabel.Width = 75
EditLabel.Height = 13
EditLabel.Caption = 'User Password:'
TabOrder = 8
Text = 'PASSWORD TO APP GENERATE ON MYACCOUNT GMAIL CONSOLE'
end
object lbedtEmailBCCList: TLabeledEdit
Left = 8
Top = 139
Width = 401
Height = 21
EditLabel.Width = 40
EditLabel.Height = 13
EditLabel.Caption = 'BCC list:'
TabOrder = 9
end
object IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL
Destination = ':25'
Intercept = IdLogEvent1
MaxLineAction = maException
Port = 25
DefaultPort = 0
SSLOptions.Mode = sslmUnassigned
SSLOptions.VerifyMode = []
SSLOptions.VerifyDepth = 0
Left = 456
Top = 176
end
object IdMessage1: TIdMessage
AttachmentEncoding = 'UUE'
BccList = <>
CCList = <>
Encoding = meDefault
FromList = <
item
end>
Recipients = <>
ReplyTo = <>
ConvertPreamble = True
Left = 568
Top = 120
end
object IdSMTP1: TIdSMTP
Intercept = IdLogEvent1
IOHandler = IdSSLIOHandlerSocketOpenSSL1
SASLMechanisms = <>
Left = 568
Top = 192
end
object IdLogEvent1: TIdLogEvent
Left = 568
Top = 280
end
object IdLogFile1: TIdLogFile
Intercept = IdLogEvent1
Filename = 'IndyLog.txt'
Left = 576
Top = 360
end
end
Код:
unit uFormMain;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
Vcl.ExtCtrls,
IdBaseComponent,
IdComponent,
IdIOHandler,
IdIOHandlerSocket,
IdIOHandlerStack,
IdSSL,
IdSSLOpenSSL,
IdMessage,
IdTCPConnection,
IdTCPClient,
IdExplicitTLSClientServerBase,
IdMessageClient,
IdSMTPBase,
IdSMTP,
IdLogFile,
IdIntercept,
IdLogBase,
IdLogEvent;
type
TForm1 = class(TForm)
lbedtEmailTo: TLabeledEdit;
lbedtEmailSubject: TLabeledEdit;
mmEmailBody: TMemo;
lbEmailBody: TLabel;
btnEmailSend: TButton;
lbedtEmailFrom: TLabeledEdit;
lbedtEmailHost: TLabeledEdit;
lbedtHostPort: TLabeledEdit;
lbedtHostUserName: TLabeledEdit;
lbedtHostPassword: TLabeledEdit;
lbedtEmailBCCList: TLabeledEdit;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
IdMessage1: TIdMessage;
IdSMTP1: TIdSMTP;
IdLogEvent1: TIdLogEvent;
IdLogFile1: TIdLogFile;
procedure btnEmailSendClick(Sender: TObject);
private
function fncSendEmail(): boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
uses
IdText,
IdEMailAddress;
procedure TForm1.btnEmailSendClick(Sender: TObject);
begin
btnEmailSend.Enabled := false;
//
if fncSendEmail then
ShowMessage('Successfully send!')
else
ShowMessage('It was not possible send email!');
end;
function TForm1.fncSendEmail(): boolean;
var
lMyIdSSLIOHandlerSocket: TIdSSLIOHandlerSocketOpenSSL;
lMyIdText : TIdText;
lMyIdEMailAddressItem : TIdEMailAddressItem;
begin
result := false;
//
lMyIdSSLIOHandlerSocket := nil;
lMyIdText := nil;
//
try
lMyIdSSLIOHandlerSocket := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
lMyIdSSLIOHandlerSocket.SSLOptions.Method := TIdSSLVersion.sslvSSLv23;
lMyIdSSLIOHandlerSocket.SSLOptions.Mode := TIdSSLMode.sslmClient;
//
//
IdMessage1.CharSet := 'utf-8';
IdMessage1.Encoding := TIdMessageEncoding.meMIME;
IdMessage1.From.Name := 'From <<your name for example>>';
IdMessage1.From.Address := lbedtEmailFrom.Text;
IdMessage1.Priority := TIdMessagePriority.mpNormal;
IdMessage1.Subject := lbedtEmailSubject.Text;
//
// referent to the Tartgets
// lMyIdEMailAddressItem :=
IdMessage1.Recipients.Add;
IdMessage1.Recipients.EMailAddresses := lbedtEmailTo.Text;
IdMessage1.CCList.EMailAddresses := lbedtEmailTo.Text;
// IdMessage1.BccList.EMailAddresses := lbedtEmailBCCList.Text;
// IdMessage1.BccList.EMailAddresses := 'others@gmail.com'; // ocult copy!
//
// setting the message's body type
lMyIdText := TIdText.Create(IdMessage1.MessageParts);
// it's means that the email sended, can dont have any HTML format!
// the message will can contain special chars "charset=iso-8859-1"
// else, the chars will have HTML codes!
IdMessage1.ContentType := 'text/html; text/plain; charset=iso-8859-1';
IdMessage1.Body.Add(mmEmailBody.Text);
//
// preparing server...
IdSMTP1.IOHandler := lMyIdSSLIOHandlerSocket;
IdSMTP1.UseTLS := utUseImplicitTLS;
IdSMTP1.AuthType := TIdSMTPAuthenticationType.satDefault;
IdSMTP1.Host := lbedtEmailHost.Text;
IdSMTP1.AuthType := TIdSMTPAuthenticationType.satDefault;
IdSMTP1.Port := StrToInt(lbedtHostPort.Text); // set direct 465 SSL port
IdSMTP1.Username := lbedtHostUserName.Text;
IdSMTP1.Password := lbedtHostPassword.Text;
//
// connecting and authenticating...
try
IdSMTP1.Connect;
IdSMTP1.Authenticate;
//
// if connected and authenticated...
if IdSMTP1.Connected then
begin
try
IdSMTP1.Send(IdMessage1);
//
result := true;
except
on E: Exception do
ShowMessage('Error on sending email' + sLineBreak + E.Message);
end;
end;
except
on E: Exception do
ShowMessage('Error on connecting / authenticating on server' + sLineBreak + E.Message);
end;
finally
if not(lMyIdText = nil) then
lMyIdText.Free;
///
if not(lMyIdSSLIOHandlerSocket = nil) then
lMyIdSSLIOHandlerSocket.Free;
end;
end;
end.




Последнее редактирование: