function SendToRaw(const MailFrom, MailTo, SMTPHost: string;
const MailData: TStrings; const Username, Password: string): Boolean;
var
SMTP: TSMTPSend;
s, t, hos, por, ssl: string;
nps, nss: integer;
begin
// modified by Patyi
// SMTPHost parameter is expanded with TLS/SSL enable part, example :
// for GMail host parameter: 'smtp.gmail.com:465&SSL'
// where ':' is port separator as before, '&' is the new for TLS/SSL enabling separator.
Result := False;
SMTP := TSMTPSend.Create;
try
s := Trim(SMTPHost); // string represent host, port and TLS/SSL option
nps := Pos(':', s); // ':' port separator position
nss := Pos('&', s); // '&' TLS/SSL separator position
if (nps = 0) and (nss = 0) then begin // only host is given, no port, no TLS/SSL
hos := s;
por := ''; // default port is 25
ssl := '';
end else if (nps > 0) and (nss = 0) then begin // host and port is given, no TLS/SSL
hos := Trim(Copy(s, 1, nps-1));
por := Trim(Copy(s, nps+1, 10)); // users port
ssl := '';
end else if (nps = 0) and (nss > 0) then begin // host and TLS/SSL is given, no port
hos := Trim(Copy(s, 1, nss-1));
por := '587'; // if TLS then default port is 587
ssl := Trim(Copy(s, nss+1, 10)); // TLS or SSL
if Pos('SSL', ssl) > 0 then
por := '465'; // if SSL then default port is 465
end else if (nps > 0) and (nss > 0) then begin // host, port and TLS/SSL given
hos := Trim(Copy(s, 1, nps-1));
por := Trim(Copy(s, nps+1, nss-nps-1)); // users port
ssl := Trim(Copy(s, nss+1, 10)); // TLS or SSL for enabling TLS/SSL
end;
// if you need SOCKS5 support, uncomment next lines:
// SMTP.Sock.SocksIP := '127.0.0.1';
// SMTP.Sock.SocksPort := '1080';
if Pos('TLS', ssl) > 0 then // if TLS is enabled
SMTP.AutoTLS := True;
if Pos('SSL', ssl) > 0 then // if SSL is enabled
SMTP.FullSSL := True;
SMTP.TargetHost := hos;
if por > '' then // if different port is given
SMTP.TargetPort := por;
SMTP.Username := Username;
SMTP.Password := Password;
if SMTP.Login then begin
if SMTP.MailFrom(GetEmailAddr(MailFrom),
Length(MailData.Text)) then begin
s := MailTo;
repeat
t := GetEmailAddr(Trim(FetchEx(s, ',', '"')));
if t <> '' then
Result := SMTP.MailTo(t);
if not Result then
Break;
until s = '';
if Result then
Result := SMTP.MailData(MailData);
end;
SMTP.Logout;
end;
finally
SMTP.Free;
end;
end;