Sending an Email with a PDF attachment
Using Delphi XE, Indy 10, and RB 12.05, I'm attempting to send an email
using the following code. I don't receive an error (like I do when I have
an unknown host name), but I also don't receive an email. This SMTP server
doesn't currently have a problem with a different program of ours that's
using a TIdSMTP component, so the login information is good, and it accepts
and sends attachments. Some of the host and port setting is certainly
excessive, but I wanted to make sure I wasn't leaving one of them out that
may cause it to silently fail. SSL is not required for this server.
procedure TfrmRPSelector.EmailReport(reportName: string);
var
ioHandler: TIdIOHandlerStack;
lEmail: TppEmail;
begin
if not FileExists(reportName) then
Exit;
ppReport1.Template.FileName := reportName;
ppReport1.Template.LoadFromFile;
TppSMTPPlugIn.RegisterClass(TppSMTPIndy);
ppReport1.AllowPrintToFile := true;
TppEmail(ppReport1.Email).SMTP.OnEmailError := EmailErrorEvent;
ppReport1.ArchiveFileName := 'Test.pdf';
ppReport1.EmailSettings.Enabled := true;
ppReport1.EmailSettings.HostAddress := 'smtp.east.cox.net';
ppReport1.EmailSettings.UserName := myUserName;
ppReport1.EmailSettings.Password := myPassword;
ppReport1.EmailSettings.ShowEmailDialog := false;
ppReport1.EmailSettings.PreviewInEmailClient := false;
ppReport1.EmailSettings.Subject := 'Test Report';
ppReport1.EmailSettings.Recipients.Text := sSendTo;
ppReport1.EmailSettings.FileName := 'Test.pdf';
ppReport1.EmailSettings.Body.Text := 'See attached.';
ppReport1.EmailSettings.ReplyTo := sSendFrom;
ppReport1.EmailSettings.FromAddress := sSendFrom;
ppReport1.EmailSettings.FromName := 'Steve';
lEmail := TppEmail(ppReport1.Email);
TppSMTPIndy(lEmail.SMTP).IndySMTP.Port := 25;
TppSMTPIndy(lEmail.SMTP).IndySMTP.Host := 'smtp.east.cox.net';
ioHandler := TIdIOHandlerStack.Create(self);
try
ioHandler.Destination := 'smtp.east.cox.net:25';
ioHandler.Port := 25;
ioHandler.Host := 'smtp.east.cox.net';
TppSMTPIndy(lEmail.SMTP).IndySMTP.IOHandler := ioHandler;
ppReport1.SendMail;
finally
ioHandler.Free;
end;
end;
using the following code. I don't receive an error (like I do when I have
an unknown host name), but I also don't receive an email. This SMTP server
doesn't currently have a problem with a different program of ours that's
using a TIdSMTP component, so the login information is good, and it accepts
and sends attachments. Some of the host and port setting is certainly
excessive, but I wanted to make sure I wasn't leaving one of them out that
may cause it to silently fail. SSL is not required for this server.
procedure TfrmRPSelector.EmailReport(reportName: string);
var
ioHandler: TIdIOHandlerStack;
lEmail: TppEmail;
begin
if not FileExists(reportName) then
Exit;
ppReport1.Template.FileName := reportName;
ppReport1.Template.LoadFromFile;
TppSMTPPlugIn.RegisterClass(TppSMTPIndy);
ppReport1.AllowPrintToFile := true;
TppEmail(ppReport1.Email).SMTP.OnEmailError := EmailErrorEvent;
ppReport1.ArchiveFileName := 'Test.pdf';
ppReport1.EmailSettings.Enabled := true;
ppReport1.EmailSettings.HostAddress := 'smtp.east.cox.net';
ppReport1.EmailSettings.UserName := myUserName;
ppReport1.EmailSettings.Password := myPassword;
ppReport1.EmailSettings.ShowEmailDialog := false;
ppReport1.EmailSettings.PreviewInEmailClient := false;
ppReport1.EmailSettings.Subject := 'Test Report';
ppReport1.EmailSettings.Recipients.Text := sSendTo;
ppReport1.EmailSettings.FileName := 'Test.pdf';
ppReport1.EmailSettings.Body.Text := 'See attached.';
ppReport1.EmailSettings.ReplyTo := sSendFrom;
ppReport1.EmailSettings.FromAddress := sSendFrom;
ppReport1.EmailSettings.FromName := 'Steve';
lEmail := TppEmail(ppReport1.Email);
TppSMTPIndy(lEmail.SMTP).IndySMTP.Port := 25;
TppSMTPIndy(lEmail.SMTP).IndySMTP.Host := 'smtp.east.cox.net';
ioHandler := TIdIOHandlerStack.Create(self);
try
ioHandler.Destination := 'smtp.east.cox.net:25';
ioHandler.Port := 25;
ioHandler.Host := 'smtp.east.cox.net';
TppSMTPIndy(lEmail.SMTP).IndySMTP.IOHandler := ioHandler;
ppReport1.SendMail;
finally
ioHandler.Free;
end;
end;
This discussion has been closed.
Comments
Some servers do not function well with the Authenticate command that
Indy sends. Try setting the AuthenticateConnection property of the
TppSMTPIndy object to False and see if that helps.
TppSMTPIndy(lEmail.SMTP).AuthenticateConnection := False;
If this does not help, as a test, try removing all code after setting
the FromName property of the EmailSettings except for the SendMail call.
There should be no need to create an iohandler if you are not trying
to connect to a secured server and by default the port number is 25.
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com