开发者

STARTTLS error while sending email using Indy in Delphi XE

开发者 https://www.devze.com 2023-04-12 14:36 出处:网络
I\'m trying to send email from my application using the following code: Var MailMessage : TIdMessage; SMTP: TIdSMTP

I'm trying to send email from my application using the following code:

  Var
  MailMessage : TIdMessage;
  SMTP        : TIdSMTP
   .
   .
   .
 //setup SMTP
 SMTP.Host := 'smtp.gmail.com';
 SMTP.Port := 25;
 //setup mail message
 MailMessage.From.Address := 'fromMe@gmail.com';
 MailMessage.Recipient开发者_如何学运维s.EMailAddresses := 'ToSomeOne@hotmail.com';
 MailMessage.Subject := 'Test';
 MailMessage.Body.Text := 'Hello, It is Just for test';

 SMTP.Connect;
 SMTP.Send(MailMessage);

When i run it, it generates the following error

**ERROR: Must issue a STARTTLS command first. i29sm34080394wbp.22**

How can I solve this?


By putting the answers together you can get the following code. Don't forget as Nathanial Woolls mentioned here to put the libeay32.dll and ssleay32.dll libraries for instance from here to your project folder or to a path from the following places.

uses
  IdMessage, IdSMTP, IdSSLOpenSSL, IdGlobal, IdExplicitTLSClientServerBase;

procedure SendEmail(const Recipients: string; const Subject: string; const Body: string);
var
  SMTP: TIdSMTP;
  Email: TIdMessage;
  SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  SMTP := TIdSMTP.Create(nil);
  Email := TIdMessage.Create(nil);
  SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);

  try
    SSLHandler.MaxLineAction := maException;
    SSLHandler.SSLOptions.Method := sslvTLSv1;
    SSLHandler.SSLOptions.Mode := sslmUnassigned;
    SSLHandler.SSLOptions.VerifyMode := [];
    SSLHandler.SSLOptions.VerifyDepth := 0;

    SMTP.IOHandler := SSLHandler;
    SMTP.Host := 'smtp.gmail.com';
    SMTP.Port := 587;
    SMTP.Username := 'yourusername@gmail.com';
    SMTP.Password := 'yourpassword';
    SMTP.UseTLS := utUseExplicitTLS;

    Email.From.Address := 'yourusername@gmail.com';
    Email.Recipients.EmailAddresses := Recipients;
    Email.Subject := Subject;
    Email.Body.Text := Body;

    SMTP.Connect;
    SMTP.Send(Email);
    SMTP.Disconnect;

  finally
    SMTP.Free;
    Email.Free;
    SSLHandler.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SendEmail('recipient@whatever.com', 'Subject', 'Body');
end;

Hope this help


smtp.gmail.com forces you to use an encrypted connection using STARTTLS.

Indy 9 does not support STARTTLS directly, but indy 10 does.

In Indy 10, before connecting to the server, you have to assign an SSL-enabled IOHandler, such as TIdSSLIOHandlerSocketOpenSSL,

See: http://www.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=TIdSSLIOHandlerSocketOpenSSL.html

Add the following code:

var
...
SSLHandler: TIdSSLIOHandlerSocketOpenSSL;

SSLHandler:= TIdSSLIOHandlerSocketOpenSSL.Create(Form1);
SMTP.IOHandler:= SSLHandler;
SMTP.UseTLS:= utUseExplicitTLS;
SMTP.Username:= 'username';
SMTP.Password:= 'password';
SMTP.Port:= 587; 

If you just SMTP to a non-encrypted SMTP-handler (your ISP's SMTP) you will not suffer this problem.


The error is because Gmail uses SSL and TLS. You're going to need to use Indy's SSL IO handler as well as the proper OpenSSL DLL's for your Indy version.

There a tutorial on getting this working here.

0

精彩评论

暂无评论...
验证码 换一张
取 消