Sending EMail via SMTP or MAPI

Never done this before but I'd like to send EMail with attachments from a Delphi7 application. How do I determine if SMTP server is available (get it's address) or if SMTP is not available how to send the email via. the default email program. Any assistance is appreciated.
rickluttrellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KristaoCommented:
Hi

Try use Indy commponents

simplest code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdMessageClient, IdSMTP, IdMessage;

type
  TForm1 = class(TForm)
    send_mail: TIdSMTP;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  msg: TIdMessage;
begin
  msg := TIdMessage.Create(self);
  msg.Body.Text := 'This is Test mail';
  msg.From.Address := 'myname@myhost.com';
  msg.Subject := 'Test';
  msg.Recipients.EMailAddresses := 'friend@friendshost.com';
  TIdAttachment.Create(msg.MessageParts, 'C:\myattachemt.jpg');
  send_mail.Send(msg);
  msg.Free;
end;

end.

you can tune it up =) but its the simplest way =)

all you need to know is Host and Port to SMTP server and set it up to commponent in this case send_mail: TIdSMTP;

regards,
kristao.
0
rickluttrellAuthor Commented:

Thank's kristao , but my question was 'How do I determine if SMTP server is available (get it's address)'. Is it derived from the msg.From.Address as listed in you code snip ?
0
KristaoCommented:
How to determine if SMTP server is awailable.

Connect to SMTP server with telnet =) if connection is succesfuly SMTP is awailable. Indy has a telnet component.

exsample:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdTelnet;

type
  TForm1 = class(TForm)
    IdTelnet1: TIdTelnet;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  go: boolean;
  msg: string;
begin
  go := true;
  try
    IdTelnet1.Host := 'SMTP host';
    IdTelnet1.Port := 25;
    IdTelnet1.Connect(1000);
    if IdTelnet1.Connected then
    begin
      msg := IdTelnet1.ReadLn;
      IdTelnet1.Disconnect;
    end;
  except
    go := false;
  end;

  if go then
  begin
    showmessage('Hellow message from SMTP: ' + msg);
    //send email here
  end
  else showmessage('SMTP server is down please try later');
end;

end.

Now you know SMTP is alive and can work. Telnet is good think you can connect to anything :)

"Is it derived from the msg.From.Address as listed in you code snip ?"

Yes it is. This will be just a information for SMTP server who did send this email. Every email, has its heder and from heder SMTP server knows what does this email contain attachments, pictures and other things, like encoding and so on.

You can put in msg.From.Addres what you wan't :) its just plain info form SMTP server. for fun you can put there bugs@cartoonnetwork.uk. Of corse you can leve it emty.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.