You are here:

Delphi/Send email using Delphi code

Advertisement


Question
Hello!
I'm studying Delphi and I'm trying to make this example work, it's taken from "Teach Yourself Delphi 4 in 21 Days"
For the host I tried smtp.gmail.com, I also tried port
465, I tried imtp... the program connects to the server, but when I execute the code to send an email a messagedlg box pops up saying "incomplete header". I tried it in Delphi 4 as well as in 7, I can't find the way of making this work, it's very tiring going through a book and speding months trying things that never work...
Could you please help me out? I include the code I'm trying below. I also have installed Delphi 2010 if you wish to provide examples for 2010 version.
Thank!

SMTP.Host := `mail';
SMTP.Connect;

procedure TMainForm.SMTPConnect(Sender: TObject);
begin
 with SMTP.PostMessage do begin
   FromAddress := `';
   ToAddress.Add(`');
   Subject := `Test';
   Body.Add(`This is a test');
 end;
 SMTP.SendMail;
end;

Answer
Daniel,
 I don't have a lot of experience with smtp, but I know that if you leave the From and To fields blank as in the code above, the mail server (and possibly even Delphi) will reject the message. You've got to put in an email address in both of those, and SMTP.Host needs to be pointing at a live SMTP server instead of 'mail'. The best test is to use your own email address given to you by your ISP, and then email yourself as a test, since the ISP should give you a valid server address that matches your email account.
Sincerely,
Brian Sill  

Delphi

All Answers


Answers by Expert:


Ask Experts

Volunteer


Brian Sill

Expertise

Designing and implementing DOS, DPMI, and Windows apps in TP, BP, and Delphi (Windows VCL, Custom Controls, Runtime UI design, Windows API, Database, SQL, Threading, Btrieve, Pervasive.SQL, Advantage Database, ADO to MDB, ADO to MS SQL, MDI Apps, MS Office Integration, barcode generation, Document imaging application development, TIFF file structure, and some IFF file experience).

Experience

Turbo Pascal and Delphi developer since 1986. Proficient in web design incuding XHTML, CSS, and Javascript. Fluent in C, C++, Visual Basic (especially VBA), and Modula-2.

Education/Credentials
Over 20 years designing and implementing DOS, DPMI, and Windows apps in TP, BP, and Delphi.

©2016 About.com. All rights reserved.