Link to home
Start Free TrialLog in
Avatar of DonBartholomew
DonBartholomew

asked on

How to use the NMSMTP component ?

I'd like to know how the NMSMTP component (from the FastNet tab in Delphi 5) is used. I want to use it to notify users automatically when their submitted forms are processed by the secretary personnel. The request from the users are sent via a distributed application, the processing is to be done in my application, working together with a MSSQL 7 database.
Avatar of inthe
inthe

Hi
there is example in the delphi\demos\internet\smtp dirctory of your delphi installation.this example shows pretty much everything(parameter wise) that can be done with the component.
Regards Barry
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
one more thing you probably alreadty know is it can be used with the the nmpop3 component (pop to read mail ,smtp to send) ..there is example of all the internet comps in the demos dir.
God knwos what you're talking about with MSSQL and all that but I can help you out with the NMSMTP stuff. The following code simply constructs an NMSMTP component dynamically, connects to the server, builds the mail (using the supplied message) and sends it.

PROCEDURE SendMail(sMSG : STRING);
VAR
  SMTP : TNMSMTP;
BEGIN
  SMTP := TNMSMTP.Create(nil);
  TRY
    IF NOT(SMTP.Connected)     //We SHOULDN'T be connected but check anyway
    THEN
    BEGIN
      SMTP.Host:= '192.168.0.1';  //Setup the details of the SENDING account
      SMTP.UserID := 'neils';
      SMTP.Connect;
    END;

    IF SMTP.Connected             //By now we SHOULD be connected but check
    THEN
    BEGIN
      SMTP.ClearParams := TRUE;   //Setup the required fields
      SMTP.SubType := mtPlain;
      SMTP.EncodeType := uuMime;

      //The header details MUST be complete so we can stick in some dummy values
      SMTP.PostMessage.FromAddress := 'neils@minorplanet.com';
      SMTP.PostMessage.FromName := 'MyProg';

SMTP.PostMessage.ToAddress.Text := 'neils@minorplanet.com';
      SMTP.PostMessage.Body.Text := sMSG;

      SMTP.PostMessage.Subject := 'Test Mail';
      SMTP.PostMessage.Date := DateToStr(Date);

      SMTP.SendMail;   //Actually send the mail
    END;

    IF SMTP.Connected  //Tidy up
    THEN
      SMTP.Disconnect;
  FINALLY
    SMTP.Free;
  END;
END;

All you need to make sure of is that the host and user ID values are correct, and that the header is completed (it'll fail if it isn't). It's then just a case of passing in the subject text, body text, and destination details. If you're sending a lot of emails it would be worth doing the initialisation and connection just once, and keeping the connection open until you've finished.

The Neil
The suggested answer happens to be mine. Award the points how you see fit

The Neil
like i said there is example in the delphi\demos\internet\smtp directory
i just thought a small peice of code would give the general idea and happened to have the other question opn also...


Sure, no problem - that's what the whole site is about

The Neil