DelphiFAQ Home Search:

Compose an HTML email with Outlook from Delphi

 

comments5 comments. Current rating: 4 stars (2 votes). Leave comments and/ or rate it.

Question:

I need to send an HTML formatted email with Outlook.

Answer:

Take a look at the procedure below. Only in the newer versions of Outlook (98, 2000 and newer) you can send HTML-formatted emails. This works by assigning the HTML as a string to the MailItem's HTMLBody property instead of the Body property. Outlook 97 doesn't support this.

If you use Delphi 5, you should place an OutlookApplication component on your form. Then you can use early binding which is safer and faster. Instead of using type variant you could be using types like MAPIFolder, MailItem etc.

The example below uses no type library.
The advantage is that you are more likely to get it compiled right away.
The disadvantage is that you are less likely to get it working right away.

uses 
  Windows, ComObj, ActiveX;

const
  olMailItem = 0;

var
  Outlook, NmSpace, Folder: OleVariant; 
  miMail: Variant;

begin
  Outlook := CreateOleObject('Outlook.Application');   

  miMail := Outlook.CreateItem(olMailItem);
  miMail.Recipients.Add('billy@boy.com');
  miMail.Subject := 'Hello Bill';

  // use this to send a plain text email (all versions of Outlook)
  miMail.Body := 'Attached is the list of email addresses.';

  // alternatively send an HTML email (not in Outlook 97)
  miMail.HTMLBody := '<font color=red>Attached is the <b>list of email</b> addresses.</font>';

  miMail.Attachments.Add('C:\temp\list.txt', EmptyParam, EmptyParam, EmptyParam);
  miMail.Send;

  //...
end;
You don't like the formatting? Check out SourceCoder then!
Content-type: text/html

Comments:

2006-09-14, 15:59:53
anonymous from Spain  
rating
Good. Worked at 1st in Delphi 4
Now I have to solve the outlook security warning. Found one, but didnt work in D4, didnt recognize the 'olSecurityManager' var.
2008-08-21, 07:04:35
anonymous from India  
its great
2009-08-09, 04:52:25   (updated: 2009-08-09, 05:19:01)
anonymous from United Arab Emirates  
hi ,

i cannot type in the outlook mail,because it shows a x mark inside check box on the left top corner of body,this is when i try to send new mail.please send a solution
2009-08-19, 03:58:38
anonymous from United Kingdom  
How can I format (HTML code) the body text? I want to open the outlook expresess to have some editing facility instead of sending directly.

ShellExecute(0,'open','mailto: peter@preview.org?subject=my subject&body=my message text', NIL, NIL, SW_SHOWNORMAL);


--> Example bold--'my message text'. I want this.
2017-07-18, 20:20:07
Rudolf from Riverhead, United States  
rating
I see your page needs some fresh articles.
Writing manually takes a lot of time, but there is tool for this boring
task, search in google: murgrabia's tools unlimited content

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: