• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

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.
0
rickluttrell
Asked:
rickluttrell
  • 2
1 Solution
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now