tags:

views:

1406

answers:

6

At the moment we are using MAPI to send a plain text email from our application. We specify the dialog flag when the user invokes this function, so that the email will appear in their email client and they can then modify it and send it.

We would like to embelish the email and send it in an HTML format.
According to this link MSDN link MAPI is not sutiable for this http://support.microsoft.com/kb/268440

I have seen an article on ExpertsExchange that say you can use MAPI to do it but I can't get the example to work with Outlook (not tried anyother client yet)

procedure ShowMailDlg(ToName,Address,HTMLMessage: string);
var
    li: integer;
    lMessage: TMapiMessage;
    lRecipArray: array of TMapiRecipDesc;
    lREs: DWord;
begin
    SetLength(lRecipArray,1);
    lRecipArray[0].ulRecipClass:=MAPI_TO;
    lRecipArray[0].lpszName:=pChar(ToName);
    lRecipArray[0].lpszAddress:=pChar(Address);
    lMessage.ulReserved:=0;
    lMessage.lpszSubject:=nil;
    lMessage.lpszNoteText:=pChar(HTMLMessage);
    lMessage.lpszMessageType:= nil;//pChar('HTML');
    lMessage.lpszDateReceived:=nil;
    lMessage.lpszConversationID:=nil;
    lMessage.flFlags:=0;
    lMessage.lpOriginator:=nil;
    lMessage.nRecipCount:=length(lRecipArray);
    lMessage.lpRecips:=PMapiRecipDesc(lRecipArray);
    lMessage.nFileCount:=0;
    lMessage.lpFiles:=PMapiFileDesc(nil);
    lRes:=MapiSendMail(0, 0 , lMessage,MAPI_DIALOG, 0);
end;

Anyone have any ideas how I can do this. I could probably automate Outlook but I would like to keep it fairly independant of email client (hence MAPI)

Thanks

Update: thanks to everyone for the suggestions. The feature is question is not that heavily used, so asking the user to configure SMTP details is not really an option. I think we will just stick to the plain text email.

Thanks

A: 

You can use SMTP with Indy:
HTML Messages
New HTML Message Builder class (Indy 10)

TOndrej
Unfortunately, this will not pop up the email in the user's default email client like MAPI will.
Scott W
A: 

This is also possible using the Synapse library. A specific example is available from the howto page titled "About MIME and its MIME Parts". I personally have used this technique in several programs to send HTML email.

Unfortunately, this doesn't work over MAPI, you will need to get the users SMTP or IMAP information and handle that communication yourself (the Synapse library has routines to do just that).

If you decide to download Synapse, I strongly suggest getting the latest version from the subversion repository. The update available there includes support for Delphi 2009.

skamradt
+2  A: 

MAPI doesn't support HTML formatted messages. From Microsoft : "Extended Messaging Application Programming Interface (MAPI) should not be used to generate HTML-formatted messages. As an alternative, consider using the Microsoft Outlook Object Model, CDONTS, CDOSYS, CDOEX, or a third-party SMTP control."

I would echo the comments about sending via Indy. I published a unit that works to send HTML messsages with Indy very simply here or feel free to write your own. If you really want to make the messages editable, try a combination of WPTools and Indy. WPTools has good support for HTML markup and then you can send the resulting message via Indy.

I don't have any experience with Synapse so I can't say how easy/hard it is with that project.

Marshall Fryman
A: 

If you only have to serve Outlook clients you could try accessing Outlook by OLE:

procedure SendMail(const aRecipient, aSubject, aNote, aFile: string; Silent, HTML: boolean);
const
  olMailItem = 0;
var
  ii: integer;
  MyOutlook, MyMail: variant;
begin
  //*** Send something via OLE/Outlook...

  //*** Outlook- und Mail-Objekt erstellen...
  MyOutlook := CreateOLEObject('Outlook.Application');
  MyMail    := MyOutlook.CreateItem(olMailItem);

  //*** create a mail message...
  MyMail.To       := aRecipient;
  MyMail.Subject  := aSubject;

  if aNote <> '' then begin
    if HTML then
      MyMail.HTMLBody := aNote
    else begin
      MyMail.Body     := aNote;
    end;
  end;

  //*** Add Attachment...  
  if aFile <> '' then begin
    MyMail.Attachments.Add(aFile);
  end;

  if Silent then
    MyMail.Send
  else
    MyMail.Display;

  MyOutlook := UnAssigned;
end;
Andreas Wieland
A: 
andremo
A: 

You should consider using an SMTP component, Indy for example, and adding the user doing the sending to the CC or BCC field of the message. This largely satisfies the need to have such sent messages appear in the user's own mail client, which is the primary advantage of MAPI. The user may even set up a separate account specifically for receiving such copies.

Doing it this way allows you to completely customize every detail related to sending mail (MHTML being one such example), including caching all mail and doing the sending in a separate thread, or at a different time, and so on. Also, this method is more client-agnostic than even MAPI; for example, this still works even if the user is using web-based email such as Gmail.

cjrh