14

I am using INDY to send Email using the SMTP client on port 25 with no problem.

Now I need to send an Email using a Gmail account and for that I need to use TLS.

Can anyone provide a simple sample on how to do that.

Thanks

DRokie
  • 705
  • 2
  • 9
  • 20
  • 1
    top result on google for "indy+smtp+gmail" reveals: [Send mail to GMail using Indy](http://www.marcocantu.com/tips/oct06_gmail.html) – Ondrej Kelle Aug 12 '11 at 10:02
  • 1
    I've used Marco Cantu's guide in the past: http://www.marcocantu.com/tips/oct06_gmail.html – berndvf Aug 12 '11 at 10:03

3 Answers3

8

This code works for GMail:

begin
  IDSMTP1 := TIDSMTP.Create;
  IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create;
  try

    with IDSMTP1 do
    begin
      Host := srvr.Host;
      Port := srvr.Port;
      if (srvr.needAuthentication = 'Y') then
        AuthType := satDefault
      else
        AuthType := satNone;
      IOHandler := IdSSLIOHandlerSocketOpenSSL1;

      if (srvr.secureMode = 'Y') then
        UseTLS := utUseRequireTLS
      else
        UseTLS := utNoTLSSupport;

      Username := srvr.Username;
      Password := srvr.Password;
    end;

    idMBHTML := TIdMessageBuilderHTML.Create;
    Idmessage1 := TIDMessage.Create;

    try
      with idMBHTML do
      begin
        enc := TEncoding.Unicode;
        HTML.LoadFromStream(FEmlMsg.MsgBody, enc);
        for c := 0 to FEmlMsg.Attachmnts.Count - 1 do
          Attachments.Add(FEmlMsg.Attachmnts[c]);
        FillMessage(IDMessage1);
      end;

      with Idmessage1 do
      begin
        Subject := FEmlMsg.MsgSubject;
        From.Address := FEmlMsg.FromAddress;
        From.Name := FEmlMsg.FromName;
        Recipients.EMailAddresses := FEmlMsg.RecipientAddress;
        if FEmlMsg.ReceiptRecipientAddress <> '' then
          ReceiptRecipient.Address := FEmlMsg.ReceiptRecipientAddress;
        if FEmlMsg.ReceiptRecipientName <> '' then
          ReceiptRecipient.Name := FEmlMsg.ReceiptRecipientName;
      end;

      with IDSMTP1 do
      begin
        if not Connected then
          Connect;
        Send(IdMessage1);
      end;

    finally
      Idmessage1.Free;
      idMBHTML.Free;
    end;
  finally
    IDSMTP1.Free;
    IdSSLIOHandlerSocketOpenSSL1.Free;
  end;
end;
Steve F
  • 153
  • 2
  • 9
  • have tested your self ? I get an error on connect: EIdOSSLCouldNotLoadSSLLibrary with message ' Could not load SSL library.' – DRokie Aug 12 '11 at 21:33
  • Make sure you are using the correct version of the OpenSSL DLLs for your version of Indy. Use Indy's `WhichFailedToLoad()` function to determine which portion(s) of OpenSSL failed to load. – Remy Lebeau Aug 13 '11 at 09:49
  • As Remy mentioned above, you have to include the OpenSSL DLL files along with your application. They should be in the same folder as the application executable or should be in the system path (eg System32 folder). You can download the latest files from the Indy website. – Steve F Aug 13 '11 at 12:06
1

You need the SSL dll to make the IdSSLIOHandler work.

There is info at Indy SSL Website which redirect you to Fulgan Download Site

You can download one package suitable for your platform and then include the 2 dlls with your application. I myself are using that 2 dlls with the indy components to send mail through email for over 2 years. The only problem is the sending is quite slow.

Justmade
  • 1,496
  • 11
  • 16
0

You not say what version of the Indy that you are using. For use TSL (last security changes in Hotmail/GMaill) you need to use Indy 10.

See description here.

Actually Y use this configuraction to send mails using GMail and it work correctly:

  • MailServer=smtp.gmail.com
  • PortServer=465
  • FromText=account@gmail.com (same of mail)
  • SSLMethod=sslvTLSv1