2009. március 5., csütörtök

How to use MAPI to auto-send new mail


Problem/Question/Abstract:

How can I do ShellExecute(nil, 'open', 'mailto:abc@123.com', nil, nil, sw_shownormal) and auto-send new mail?

Answer:

You'd better try MapiSendMail. This uses the unit MAPI, if MAPI is configured correctly, this works quite fine.

uses
  Mapi;

{ ...}
var
  MapiMessage: TMapiMessage;
  MapiFileDesc: PMapiFileDesc;
  MError: Cardinal;
  FNStr: string;
  R, i: Integer;
begin
  FNStr := AttachedFileName;
  if R <> mrOK then
    exit;
  MapiFileDesc := New(PMapiFileDesc);
  try
    MapiFileDesc.lpszPathName := PChar(FNStr);
    MapiFileDesc.lpszFileName := '';
    with MapiMessage do
    begin
      ulReserved := 0;
      lpszSubject := nil;
      lpszNoteText := '';
      lpszMessageType := nil;
      lpszDateReceived := nil;
      lpszConversationID := nil;
      flFlags := 0;
      lpOriginator := nil;
      nRecipCount := 0;
      lpRecips := nil;
      nFileCount := 1;
      lpFiles := MapiFileDesc;
    end;
    MError := MapiSendMail(0, 0, MapiMessage, MAPI_DIALOG or MAPI_LOGON_UI or
      MAPI_NEW_SESSION, 0);
    if MError <> 0 then
      MessageDlg(SSendError, mtError, [mbOK], 0);
  finally
    Dispose(MapiFileDesc);
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése