unit UnitSendEmail;
interface
uses
IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
IdMessageClient, IdSMTPBase, IdSMTP, IdBaseComponent, IdMessage, IdIOHandler,
IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdSASLLogin,
IdSASL_CRAM_SHA1, IdSASL, IdSASLUserPass, IdSASL_CRAMBase, IdSASL_CRAM_MD5,
IdSASLSKey, IdSASLPlain, IdSASLOTP, IdSASLExternal, IdSASLDigest,
IdSASLAnonymous, IdUserPassProvider, System.Classes;
type
TSSLEmail =
class
constructor Create;
destructor Destroy;
override;
private
{ Private declarations }
public
{ Public declarations }
edSMTPServer:
string;
edSMTPPort: integer;
edUserName:
string;
edPassword:
string;
edSSLConnection: boolean;
edSenderEmail:
string;
edSenderName:
string;
edToEmail:
string;
edCCEmail:
string;
edBCCEmail:
string;
edSubject:
string;
edPriority: TIdMessagePriority;
edBody: TStrings;
Procedure Init;
procedure SendEmail;
end;
Var
SSLEmail1: TSSLEmail;
implementation
constructor TSSLEmail.Create;
begin
inherited;
edBody := TStringList.Create;
end;
destructor TSSLEmail.Destroy;
begin
edBody.Free;
inherited;
end;
procedure TSSLEmail.Init;
begin
edSMTPServer := '
';
edSMTPPort := 0;
edUserName := '
';
edPassword := '
';
edSSLConnection := True;
edSenderEmail := '
';
edSenderName := '
';
edToEmail := '
';
edCCEmail := '
';
edBCCEmail := '
';
edSubject := '
';
edPriority := TIdMessagePriority.mpNormal;
edBody.Clear;
end;
procedure TSSLEmail.SendEmail;
var
IdMessage: TIdMessage;
// enthält Eigenschaften der Email
SMTP: TIdSMTP;
// enthält Eigenschaften des SMTP Servers
SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
// alles weitere ist für die SSL Verschlüsselung
IdUserPassProvider: TIdUserPassProvider;
IdSASLCRAMMD5: TIdSASLCRAMMD5;
IdSASLCRAMSHA1: TIdSASLCRAMSHA1;
IdSASLPlain: TIdSASLPlain;
IdSASLLogin: TIdSASLLogin;
IdSASLSKey: TIdSASLSKey;
IdSASLOTP: TIdSASLOTP;
IdSASLAnonymous: TIdSASLAnonymous;
IdSASLExternal: TIdSASLExternal;
begin
IdMessage := TIdMessage.Create(
nil);
try
// Eigenschaften der email setzen
IdMessage.ContentType := '
text/plain';
IdMessage.Charset := '
UTF-8';
IdMessage.Body.Assign(edBody);
IdMessage.Sender.Text := edSenderEMail;
IdMessage.From.
Name := edSenderName;
IdMessage.From.Address := edSenderEMail;
IdMessage.ReplyTo.EMailAddresses := edSenderEmail;
IdMessage.Recipients.EMailAddresses := edToEmail;
IdMessage.Subject := edSubject;
IdMessage.Priority := edPriority;
IdMessage.CCList.EMailAddresses := edCCEMail;
IdMessage.ReceiptRecipient.Text := '
';
IdMessage.BccList.EMailAddresses := edBCCEMail;
SMTP := TIdSMTP.Create(
nil);
try
if edSSLConnection
then begin
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(
SMTP);
// SSLHandler.MaxLineAction := maException;
SSLHandler.SSLOptions.Method := sslvSSLv23;
// SSL/TLS handshake determines the highest available SSL/TLS version dynamically
SSLHandler.SSLOptions.Mode := sslmClient;
SSLHandler.SSLOptions.VerifyMode := [];
SSLHandler.SSLOptions.VerifyDepth := 0;
SMTP.IOHandler := SSLHandler;
if edSMTPPort=587
then
SMTP.UseTLS := utUseExplicitTLS
else
SMTP.UseTLS := utUseImplicitTLS;
// ImplicitTLS insbesondere auch bei Port 465
end;
if (edUserName<>'
')
or (edPassword<>'
')
then begin
SMTP.AuthType := satSASL;
// Username und Passwort verschlüsselt bereitstellen
IdUserPassProvider := TIdUserPassProvider.Create(
SMTP);
IdUserPassProvider.Username := edUserName;
IdUserPassProvider.Password:= edPassword;
// SASL-Komponenten für SSL Verschlüsselung
IdSASLCRAMSHA1 := TIdSASLCRAMSHA1.Create(
SMTP);
IdSASLCRAMSHA1.UserPassProvider := IdUserPassProvider;
IdSASLCRAMMD5 := TIdSASLCRAMMD5.Create(
SMTP);
IdSASLCRAMMD5.UserPassProvider := IdUserPassProvider;
IdSASLSKey := TIdSASLSKey.Create(
SMTP);
IdSASLSKey.UserPassProvider := IdUserPassProvider;
IdSASLOTP := TIdSASLOTP.Create(
SMTP);
IdSASLOTP.UserPassProvider := IdUserPassProvider;
IdSASLAnonymous := TIdSASLAnonymous.Create(
SMTP);
IdSASLExternal := TIdSASLExternal.Create(
SMTP);
IdSASLLogin := TIdSASLLogin.Create(
SMTP);
IdSASLLogin.UserPassProvider := IdUserPassProvider;
IdSASLPlain := TIdSASLPlain.Create(
SMTP);
IdSASLPlain.UserPassProvider := IdUserPassProvider;
SMTP.SASLMechanisms.Add.SASL := IdSASLCRAMSHA1;
SMTP.SASLMechanisms.Add.SASL := IdSASLCRAMMD5;
SMTP.SASLMechanisms.Add.SASL := IdSASLSKey;
SMTP.SASLMechanisms.Add.SASL := IdSASLOTP;
SMTP.SASLMechanisms.Add.SASL := IdSASLAnonymous;
SMTP.SASLMechanisms.Add.SASL := IdSASLExternal;
SMTP.SASLMechanisms.Add.SASL := IdSASLLogin;
SMTP.SASLMechanisms.Add.SASL := IdSASLPlain;
end else begin
SMTP.AuthType := satNone;
end;
// Verbindung zum Server herstellen und email senden
SMTP.Host := edSMTPServer;
SMTP.Port := edSMTPPort;
SMTP.ConnectTimeout := 30000;
SMTP.UseEHLO := True;
SMTP.Connect;
// verbinden
try
SMTP.Send(IdMessage);
// senden
finally
SMTP.Disconnect;
// Verbindung lösen
end;
finally
SMTP.Free;
end;
finally
IdMessage.Free;
end;
end;
end.