Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: ISAPI dll to interface with GMail's SMTP and alternatively using MailGun


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 12 - Last Post: May 27, 2015 6:52 PM Last Post By: Remy Lebeau (Te...
Nols Smit

Posts: 7
Registered: 6/10/09
ISAPI dll to interface with GMail's SMTP and alternatively using MailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2015 5:05 AM
Hi,

I'm using Delphi XE4 with the IntraWeb, TMSIntraWeb and Indy components to create web applications. These web applications are hosted under IIS7 on Windows Server 2008, Web edition.

The Web edition does not contain Microsoft's Mail Exchange.

I've successfully created a windows application to send email using the SMTP service of GMail, port 465 but for some unknown reason the same coding does not connect from out an ISAPI dll to GMail's SMTP server. The stand-alone windows executable, in the folder of the ISAPI dll, is able to send email. This windows application is based on the example by Remy Lebeau of 8/13/2012 (http://codeverge.com/embarcadero.delphi.non-tech/using-indy-to-send-a-email-with-s/1086644)

Another problem for me using GMail's SMTP service is the fact it is not intended for sending bulk email.

I had a look at the services of MailGun (https://mailgun.com) and it seems I may be able to use it by interfacing via HTTPS through port 443, I should then be able to use it from out my ISAPI dll.

Which Indy components should I use for this HTTPS interface?

Are there perhaps any Delphi examples using MailGun's SMTP service via HTTPS through port 443?

Regards,

Nols Smit
Matthew Jones

Posts: 337
Registered: 1/25/98
Re: ISAPI dll to interface with GMail's SMTP and alternatively using MailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2015 5:27 AM   in response to: Nols Smit in response to: Nols Smit
Nols Smit wrote:

I've successfully created a windows application to send email using
the SMTP service of GMail, port 465 but for some unknown reason the
same coding does not connect from out an ISAPI dll to GMail's SMTP
server

I think I recall that services on the system account cannot access
network resources, for security? Maybe I'm wrong.

Anyway, if this is for sending system mails, we use Mandrill which has
been very good, and supports multiple methods.
Nols Smit

Posts: 7
Registered: 6/10/09
Re: ISAPI dll to interface with GMail's SMTP and alternatively using MailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2015 6:39 AM   in response to: Matthew Jones in response to: Matthew Jones
Anyway, if this is for sending system mails, we use Mandrill which has
been very good, and supports multiple methods.

Hi Matthew,

Do you perhaps have a Delphi code snippet for me or some guide lines of the Indy components to use?

Regards,

Nols Smit
Matthew Jones

Posts: 337
Registered: 1/25/98
Re: ISAPI dll to interface with GMail's SMTP and alternatively using MailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2015 7:31 AM   in response to: Nols Smit in response to: Nols Smit
Nols Smit wrote:

Do you perhaps have a Delphi code snippet for me or some guide lines
of the Indy components to use?

I just use their SMTP API, having switched from the "localhost" mail
server that wasn't so good at getting mail out in a short time.

Apart from when I first set it up and they verified my account (about
half a day) it has been very good.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: ISAPI dll to interface with GMail's SMTP and alternatively usingMailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2015 9:27 AM   in response to: Matthew Jones in response to: Matthew Jones
Matthew wrote:

I think I recall that services on the system account cannot access
network resources, for security?

That is true for things like named pipes and remote files, but not for TCP.
I use TCP sockets in services running under the SYSTEM account, and they
work jst fine.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: ISAPI dll to interface with GMail's SMTP and alternatively usingMailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2015 9:26 AM   in response to: Nols Smit in response to: Nols Smit
Nols wrote:

I've successfully created a windows application to send email using
the SMTP service of GMail, port 465 but for some unknown reason
the same coding does not connect from out an ISAPI dll to GMail's
SMTP server.

What is the actual problem you are having? Are you getting errors? What
do they say? TCP is TCP whether it is used in a standalone process or an
ISAPI process, so you should not be getting connection errors, unless maybe
you have a firewall that is blocking the ISAPI process from making outbound
connections.

Which Indy components should I use for this HTTPS interface?

TIdHTTP and TIdSSLIOHandlerSocketOpenSSL.

--
Remy Lebeau (TeamB)
Nols Smit

Posts: 7
Registered: 6/10/09
Re: ISAPI dll to interface with GMail's SMTP and alternatively usingMailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 19, 2015 2:21 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
What is the actual problem you are having? Are you getting errors? What
do they say? TCP is TCP whether it is used in a standalone process or an
ISAPI process, so you should not be getting connection errors, unless maybe
you have a firewall that is blocking the ISAPI process from making outbound
connections.

Eventually I got the error message: "Could not load SSL library"

I copied the following two dlls to the folder containing the ISAPI dll: libeay32.dll and ssleay32.dll

Regards,

Nols Smit

Nols Smit

Posts: 7
Registered: 6/10/09
Re: ISAPI dll to interface with GMail's SMTP and alternatively usingMailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 20, 2015 10:00 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
What is the actual problem you are having? Are you getting errors?

I'm now pretty sure it is IIS 7 blocking port 465. I created a self-signed certificate and enabled SSL. I followed the instructions stated in this posting: http://weblogs.asp.net/scottgu/tip-trick-enabling-ssl-on-iis7-using-self-signed-certificates. Now I have to open any web application on my server by starting with https://.... and I recon this is the proof of a successful enabling of SSL in IIS 7.

However, if I try to send email from out a web application, I still get "Could not load SSL library".

Do you have any suggestions what I should try to resolve this matter?

Regards,

Nols Smit

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: ISAPI dll to interface with GMail's SMTP and alternativelyusingMailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 20, 2015 11:04 AM   in response to: Nols Smit in response to: Nols Smit
Nols wrote:

However, if I try to send email from out a web application, I
still get "Could not load SSL library".

That is an Indy error message. You can use Indy's WhichFailedToLoad() function
in the IdSSLOpenSSLHeaders unit to find out why it is not able to load OpenSSL.
Either it cannot find/load the OpenSSL DLLs themselves, or it cannot find
a required function exported from the DLLs.

--
Remy Lebeau (TeamB)
Nols Smit

Posts: 7
Registered: 6/10/09
Re: ISAPI dll to interface with GMail's SMTP and alternativelyusingMailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 22, 2015 4:14 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
That is an Indy error message. You can use Indy's WhichFailedToLoad() function
in the IdSSLOpenSSLHeaders unit to find out why it is not able to load OpenSSL.
Either it cannot find/load the OpenSSL DLLs themselves, or it cannot find
a required function exported from the DLLs.

Hi,

I tried it the following way:

In the Uses clause of the UserSession Unit of the IntraWeb version:

unit UserSessionUnit;

{
This is a DataModule where you can add components or declare fields that are specific to
ONE user. Instead of creating global variables, it is better to use this datamodule. You can then
access the it using UserSession.
}
interface

uses
IdSSLOpenSSLHeaders,
Controls, variants, IWAdvMessageDialog, IWCompRectangle, IWColor, Graphics, IWRegion, IniFiles,
IWWebGrid, IWDBGrids, IWDBAdvWebGrid, IWAdvWebGrid, StrUtils, IWTMSMenus,
.....
.....
.....

I have a procedure sending the email in this UserSession unit:

procedure TIWUserSession.SendEmail(const MyList: TStringList; const Recipients_Email, edtSubject, edtCC: string;
const chkReturnReciept: boolean; var MailSend: boolean);
var
ErrMsg: string;

begin
with IdMessage1 do
begin
Body.Assign(MyList);
From.Text := User;
ReplyTo.EMailAddresses := ReplyTo_Email;
Recipients.EMailAddresses := Recipients_Email; { To: header }
Subject := edtSubject; { Subject: header }
// Priority := mpNormal;//TIdMessagePriority(cboPriority.ItemIndex); { Message Priority }
CCList.EMailAddresses := edtCC; {CC}
// BccList.EMailAddresses := edtBCC.Text; {BBC}
if chkReturnReciept then
begin {We set the recipient to the From E-Mail address }
ReceiptRecipient.Text := From.Text;
end
else
begin {indicate that there is no receipt recipiant}
ReceiptRecipient.Text := '';
end;
end;

{authentication settings}
with IdSMTP1 do
begin
Host := 'smtp.mandrillapp.com';
Port := 465;

AuthType := satDefault;

IOHandler := IdSSLIOHandlerSocketOpenSSL1;
UseTLS := utUseImplicitTLS;

Username := 'xxxxxx at gmail dot com';
Password := '1234567890qwertyuiop';

Connect;
try
try
Send(IdMessage1);
MailSend := true;
except
ErrMsg := IdSSLOpenSSLHeaders.WhichFailedToLoad;
if ErrMsg <> '' then
raise Exception.Create(ErrMsg)
else
raise;
end;
finally
Disconnect;
end;
end;
end;

But I still get "Could not load SSL library".

It seems I did not intercept the problem.

Regards,

Nols Smit

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: ISAPI dll to interface with GMail's SMTP andalternativelyusingMailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 22, 2015 9:59 AM   in response to: Nols Smit in response to: Nols Smit
Nols wrote:

I have a procedure sending the email in this UserSession unit:
<snip>
But I still get "Could not load SSL library".

Your exception handler is in the wrong place. The SSL exception is coming
from TIdSMTP.Connect(), not from TIdSMTP.Send(). Also, the "could not load
SSL library" error is raised as an EIdOSSLCouldNotLoadSSLLibrary exception,
which you can catch directly so you can ignore other exceptions (since you
are just re-raising them anyway).

Try this:

try
  Connect;
except
  on E: EIdOSSLCouldNotLoadSSLLibrary do
  begin
    ErrMsg := IdSSLOpenSSLHeaders.WhichFailedToLoad;
    if ErrMsg <> '' then
      E.Message := E.Message + sLineBreak + ErrMsg;
    raise;
  end;
end;
try
  Send(IdMessage1);
  MailSend := true;
finally
  Disconnect;
end;


--
Remy Lebeau (TeamB)
Nols Smit

Posts: 7
Registered: 6/10/09
Re: ISAPI dll to interface with GMail's SMTP andalternativelyusingMailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 23, 2015 1:36 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Your exception handler is in the wrong place.

Hi Remy,

I got every thing now working

I first tested smtp.gmail.com successfully by using port 465 with ssleay32.dll and libeay32.dll in the same folder as the ISAPI dll. I got them by downloading openssl-1.0.2a-i386-win32.zip from http://indy.fulgan.com/SSL/

Then I tested smtp.mandrillapp.com using port 587 but then got an error "Error connecting with SSL. error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number".

I then downloaded and installed "Visual C++ 2008 Redistributables" and "Win32 OpenSSL v1.0.2a Light" from http://slproweb.com/products/Win32OpenSSL.html
Subsequently I successfully tested the Mandrill service. According to my registration on Mandril I can only use port 587 and therefore I understand the free emails are not encrypted, see https://mandrill.zendesk.com/hc/en-us/articles/205582167-What-SMTP-ports-can-I-use-

Thanks a lot for the help.

Regards,

Nols Smit

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: ISAPI dll to interface with GMail's SMTPandalternativelyusingMailGun  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 27, 2015 6:52 PM   in response to: Nols Smit in response to: Nols Smit
Nols wrote:

I first tested smtp.gmail.com successfully by using port 465 with
ssleay32.dll and libeay32.dll in the same folder as the ISAPI dll.

Just an FYI, Indy has an IdOpenSSLSetLibPath() function in the IdSSLOpenSSLHeaders
unit that you can use to specify the folder path where the DLLs are located,
if they are not on the system search path (which includes the path that the
calling process is running from, and the current working fodler of the calling
process).

Then I tested smtp.mandrillapp.com using port 587 but then got
an error "Error connecting with SSL. error:1408F10B:SSL
routines:SSL3_GET_RECORD:wrong version number".

That usually means the SSLIOHandler's Method/SSLVersions properies do not
contain an SSL/TLS version that the server supports. Try playing with different
versions. Indy enables only TLS v1.0 by default.

Also, keep in mind that when connecting to port 465 you have to set TIdSMTP.UseTLS
to utUseImplicitTLS, but when connecting to port 587 you have to set TIdSMTP.UseTLS
to utUseExplicitTLS instead.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02