Código
Função:
Uses Mapi, ShellAPI;
function EnviaMail(hWnd: Integer; const Destinatario, Assunto, Body, Anexo: PChar): WordBool; stdcall; export;
var
MapiMessage: TMapiMessage;
MError: Cardinal;
Sender: TMapiRecipDesc;
PRecip, Recipients: PMapiRecipDesc;
PFiles, Attachments: PMapiFileDesc;
begin
Result := False;
Application.Handle := hWnd;
MapiMessage.nRecipCount := 1;
GetMem(Recipients, MapiMessage.nRecipCount * sizeof(TMapiRecipDesc));
try
with MapiMessage do
begin
ulReserved := 0;
lpszSubject := Assunto;
lpszNoteText := PChar(Body);
lpszMessageType := nil;
lpszDateReceived := nil;
lpszConversationID := nil;
flFlags := 0;
Sender.ulReserved := 0;
Sender.ulRecipClass := MAPI_ORIG;
Sender.lpszName := PChar('');
Sender.lpszAddress := PChar('');
Sender.ulEIDSize := 0;
Sender.lpEntryID := nil;
lpOriginator := @Sender;
PRecip := Recipients;
PRecip^.ulReserved := 0;
PRecip^.ulRecipClass := MAPI_TO;
PRecip^.lpszName := Destinatario;
PRecip^.lpszAddress := StrNew(PChar('SMTP:' + Destinatario));
PRecip^.ulEIDSize := 0;
PRecip^.lpEntryID := nil;
Inc(PRecip);
lpRecips := Recipients;
nFileCount := 1;
GetMem(Attachments, nFileCount * sizeof(TMapiFileDesc));
PFiles := Attachments;
Attachments^.lpszPathName := Anexo;
Attachments^.lpszFileName := PChar(ExtractFileName(Anexo));
Attachments^.ulReserved := 0;
Attachments^.flFlags := 0;
Attachments^.nPosition := Cardinal(-1);
Attachments^.lpFileType := nil;
Inc(Attachments);
lpFiles := PFiles;
end;
MError := MapiSendMail(0, hWnd, MapiMessage, MAPI_DIALOG or
MAPI_LOGON_UI or MAPI_NEW_SESSION, 0);
case MError of
MAPI_E_USER_ABORT: MessageDlg('Operação abortada pelo usuário',
mtInformation, [mbOk], 0);
SUCCESS_SUCCESS: Result := True;
else
MessageDlg('Ocorreu um erro inesperado!'#13 +
'Código: ' + IntToStr(MError), mtError, [mbOk], 0);
end;
finally
PRecip := Recipients;
StrDispose(PRecip^.lpszAddress);
Inc(PRecip);
FreeMem(Recipients, MapiMessage.nRecipCount * sizeof(TMapiRecipDesc));
if Assigned(PFiles) then FreeMem(PFiles, MapiMessage.nFileCount * sizeof
(TMapiFileDesc));
Application.Handle := 0;
end;
end;
Chamada da Função:
EnviaMail(110,'mauropmarques@studiozeus.com.br', 'Teste de Email', '', PChar('C:Arquivo.WMF'));
Obs.: Após attachado o arquivo o outlook express abrirá e você possuirá duas alternativas:
- Enviar Email = pressionando as teclas CTRL + ENTER;
- Cancelar o Envio do E-mail = pressionando a tecla ESC
Caso você trabalhe com o delphi 3, entre em contato para algumas modificações
E-mail: mauropmarques@studiozeus.com.br
Planeta Delphi - www.planetadelphi.com.br - Todos os direitos reservados | Copyright 2001-2009