Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

diegocrosa

Erro ao enviar email

Recommended Posts

Tenho essa função que ta dando erro inesperado 2.Alguem poderia me ajudar a corrigir???procedure TForm.EnviarClick(Sender: TObject);var destino, assunto, corpo, anexo :PAnsiChar;begin inherited; destino := 'suporte@sulsoftware.com.br'; assunto := 'teste'; corpo := 'blaaaa'; anexo := 'c:\aditivos_esportacao.txt'; EnviaMail(1, destino, assunto, corpo, anexo);end;function TForm.EnviaMail(hWnd: Integer; Destinatario, Assunto, Body, Anexo: PAnsiChar): WordBool; stdcall; export;var // const 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;

Compartilhar este post


Link para o post
Compartilhar em outros sites

amigo...beleza??

 

Uma Dica:

segue abaixo uma função.

Eu uso ela a um bom tempo e funciona perfeitamente:

 

uses Mapi;

 

 

 

function SendEMail(Handle: THandle; Mail: TStrings): Cardinal; type TAttachAccessArray = array [0..0] of TMapiFileDesc; PAttachAccessArray = ^TAttachAccessArray; var MapiMessage: TMapiMessage; Receip: TMapiRecipDesc; Attachments: PAttachAccessArray; AttachCount: Integer; i1: integer; FileName: string; dwRet: Cardinal; MAPI_Session: Cardinal; WndList: Pointer; begin dwRet := MapiLogon(Handle, PChar(''), PChar(''), MAPI_LOGON_UI or MAPI_NEW_SESSION, 0, @MAPI_Session);   if (dwRet <> SUCCESS_SUCCESS) then begin MessageBox(Handle, PChar('Error while trying to send email'), PChar('Error'), MB_ICONERROR or MB_OK); end else begin FillChar(MapiMessage, SizeOf(MapiMessage), #0); Attachments := nil; FillChar(Receip, SizeOf(Receip), #0);   if Mail.Values['to'] <> '' then begin Receip.ulReserved := 0; Receip.ulRecipClass := MAPI_TO; Receip.lpszName := StrNew(PChar(Mail.Values['to'])); Receip.lpszAddress := StrNew(PChar('SMTP:' + Mail.Values['to'])); Receip.ulEIDSize := 0; MapiMessage.nRecipCount := 1; MapiMessage.lpRecips := @Receip; end;   AttachCount := 0;   for i1 := 0 to MaxInt do begin if Mail.Values['attachment' + IntToStr(i1)] = '' then break; Inc(AttachCount); end;   if AttachCount > 0 then begin GetMem(Attachments, SizeOf(TMapiFileDesc) * AttachCount);   for i1 := 0 to AttachCount - 1 do begin FileName := Mail.Values['attachment' + IntToStr(i1)]; Attachments[i1].ulReserved := 0; Attachments[i1].flFlags := 0; Attachments[i1].nPosition := ULONG($FFFFFFFF); Attachments[i1].lpszPathName := StrNew(PChar(FileName)); Attachments[i1].lpszFileName := StrNew(PChar(ExtractFileName(FileName))); Attachments[i1].lpFileType := nil; end; MapiMessage.nFileCount := AttachCount; MapiMessage.lpFiles := @Attachments^; end;   if Mail.Values['subject'] <> '' then MapiMessage.lpszSubject := StrNew(PChar(Mail.Values['subject'])); if Mail.Values['body'] <> '' then MapiMessage.lpszNoteText := StrNew(PChar(Mail.Values['body']));   WndList := DisableTaskWindows(0); try Result := MapiSendMail(MAPI_Session, Handle, MapiMessage, MAPI_DIALOG, 0); finally EnableTaskWindows( WndList ); end;   for i1 := 0 to AttachCount - 1 do begin StrDispose(Attachments[i1].lpszPathName); StrDispose(Attachments[i1].lpszFileName); end;   if Assigned(MapiMessage.lpszSubject) then StrDispose(MapiMessage.lpszSubject); if Assigned(MapiMessage.lpszNoteText) then StrDispose(MapiMessage.lpszNoteText); if Assigned(Receip.lpszAddress) then StrDispose(Receip.lpszAddress); if Assigned(Receip.lpszName) then StrDispose(Receip.lpszName); MapiLogOff(MAPI_Session, Handle, 0, 0); end; end;

No Botão coloque:

 

procedure TForm1.BitBtn5Click(Sender: TObject);varmail: TStringList;beginmail := TStringList.Create;trymail.values['to'] := 'Email@test.xyz'; ///AQUI VAI O EMAIL DO DESTINATARIO///mail.values['subject'] := 'Hello'; ///AQUI O ASSUNTO///mail.values['body'] := 'blah'; ///AQUI O TEXTO NO CORPO DO EMAIL///mail.values['attachment0'] := 'C:\Test.txt'; ////AQUI O ENDEREÇO ONDE ENCONTRAM OS ARQUIVOS//mail.values['attachment1']:='C:\Test2.txt'; ///IDEM  - NO ATTACHMENT1    TEM QUE COLOCAR A SEQUNCIA DO EMAIL A QUAL DESEJA ENVIAR EXEMPLO: ATTACHMENT1sendEMail(Application.Handle, mail);finallymail.Free;end;end;

Espero Ter Ajudado

 

Fwl... http://forum.imasters.com.br/public/style_emoticons/default/yes.gif

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.