2010. március 16., kedd

How to send mail in HTML format from a Delphi application


Problem/Question/Abstract:

I would like to create a program which is able to send mail in HTML format. I tested some code but got a 'Connection Failed' message when I attempted to send the mail.

Answer:

You may have to logon to the smtp server via Pop3 first:

var
  oLogon: TNMPop3;
  oMail: TNMSmtp;
begin
  oLogon := TNMPop3.Create(self);
  try
    with oLogon do
    begin
      Host := 'pop.mail.yahoo.com';
      UserID := 'user';
      Password := 'password';
    end;
    oLogon.Connect;
    oLogon.Disconnect;
  finally
    oLogon.Free;
  end;
  oMail := TNMSmtp.Create(self);
  with oMail do
  begin
    try
      Host := 'smtp.mail.yahoo.com';
      Port := 25;
      UserID := 'YourID';
      Connect;
      SubType := mtHTML;
      { set all other properties, e. g. FromName, FromAddress, ReplyTo, Subject, etc }
      PostMessage.FromAddress := 'yourname@yahoo.com';
      PostMessage.FromName := 'YourName';
      PostMessage.Subject := 'My First HTML mail';
      PostMessage.ToAddress.Add('yourname@yahoo.com');
      {Replace [ ] brackets in the following three lines with < > ones}
      PostMessage.Body.Add('[html] [head] [/head]');
      PostMessage.Body.Add('[body] [h1]My 1st html msg[/h1] [/body]');
      PostMessage.Body.Add('[/html]');
      Connect;
      SendMail;
      Disconnect;
    finally
      Free;
    end;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése