Fórum Erro ao enviar email #336408
18/01/2007
0
Pessoal,
Tenho uma aplicação que possui o recurso de enviar emails.
Para isso, utilizo o seguinte código para envio... Ele abre o outlook e anexa um PDF.
Qual o problema? Se faço isso no outlook express, funciona bem.., Mas se tento usá-lo no outlook não dá certo! Logo que envio, já recebo uma mensagem de retorno dizendo que o email não pode ser entregue.
Achei que o endereço de email que eu estava tentando enviar era inválido, mas não é.
Se tento fazer um email manualmente para esse destinatário, funciona!
O problema é quando uso via sistema...
Alguém tem alguma idéia?
Segue o código:
function TModulo.EnviarEMail(const De, Para, Assunto, Texto, Arquivo: string; Confirma: Boolean): Integer;
var
Msg: TMapiMessage;
lpSender, lpRecepient: TMapiRecipDesc;
FileAttach: TMapiFileDesc;
SM: TFNMapiSendMail;
MAPIModule: HModule;
Flags: Cardinal;
begin
// cria propriedades da mensagem
FillChar(Msg, SizeOf(Msg), 0);
with Msg do
begin
if (Assunto <> ´´) then
lpszSubject := PChar(Assunto);
if (Texto <> ´´) then
lpszNoteText := PChar(Texto);
// remetente
if (De <> ´´) then
begin
lpSender.ulRecipClass := MAPI_ORIG;
lpSender.lpszName := PChar(De);
lpSender.lpszAddress := PChar(De);
lpSender.ulReserved := 0;
lpSender.ulEIDSize := 0;
lpSender.lpEntryID := nil;
lpOriginator := @lpSender;
end;
// destinatário
if (Para <> ´´) then
begin
lpRecepient.ulRecipClass := MAPI_TO;
lpRecepient.ulReserved := 0;
lpRecepient.ulEIDSize := 0;
lpRecepient.lpEntryID := nil;
nRecipCount := 1;
lpRecips := @lpRecepient;
lpRecepient.lpszAddress := Pchar(Para);
lpRecepient.lpszName := Pchar(Para);
end
else
lpRecips := nil;
// arquivo anexo
if Arquivo = ´´ then
begin
nFileCount := 0;
lpFiles := nil;
end
else
begin
FillChar(FileAttach, SizeOf(FileAttach), 0);
FileAttach.nPosition := Cardinal($FFFFFFFF);
FileAttach.lpszPathName := PChar(Arquivo);
nFileCount := 1;
lpFiles := @FileAttach;
end;
end;
// carrega dll e o método para envio do email
MAPIModule := LoadLibrary(PChar(MAPIDLL));
if MAPIModule = 0 then
Result := -1
else
try
if Confirma then
Flags := MAPI_DIALOG or MAPI_LOGON_UI
else
Flags := 0;
@SM := GetProcAddress(MAPIModule, ´MAPISendMail´);
if @SM <> nil then
Result := SM(0, Application.Handle, Msg, Flags, 0)
else
Result := 1;
finally
FreeLibrary(MAPIModule);
end;
end;
Tenho uma aplicação que possui o recurso de enviar emails.
Para isso, utilizo o seguinte código para envio... Ele abre o outlook e anexa um PDF.
Qual o problema? Se faço isso no outlook express, funciona bem.., Mas se tento usá-lo no outlook não dá certo! Logo que envio, já recebo uma mensagem de retorno dizendo que o email não pode ser entregue.
Achei que o endereço de email que eu estava tentando enviar era inválido, mas não é.
Se tento fazer um email manualmente para esse destinatário, funciona!
O problema é quando uso via sistema...
Alguém tem alguma idéia?
Segue o código:
function TModulo.EnviarEMail(const De, Para, Assunto, Texto, Arquivo: string; Confirma: Boolean): Integer;
var
Msg: TMapiMessage;
lpSender, lpRecepient: TMapiRecipDesc;
FileAttach: TMapiFileDesc;
SM: TFNMapiSendMail;
MAPIModule: HModule;
Flags: Cardinal;
begin
// cria propriedades da mensagem
FillChar(Msg, SizeOf(Msg), 0);
with Msg do
begin
if (Assunto <> ´´) then
lpszSubject := PChar(Assunto);
if (Texto <> ´´) then
lpszNoteText := PChar(Texto);
// remetente
if (De <> ´´) then
begin
lpSender.ulRecipClass := MAPI_ORIG;
lpSender.lpszName := PChar(De);
lpSender.lpszAddress := PChar(De);
lpSender.ulReserved := 0;
lpSender.ulEIDSize := 0;
lpSender.lpEntryID := nil;
lpOriginator := @lpSender;
end;
// destinatário
if (Para <> ´´) then
begin
lpRecepient.ulRecipClass := MAPI_TO;
lpRecepient.ulReserved := 0;
lpRecepient.ulEIDSize := 0;
lpRecepient.lpEntryID := nil;
nRecipCount := 1;
lpRecips := @lpRecepient;
lpRecepient.lpszAddress := Pchar(Para);
lpRecepient.lpszName := Pchar(Para);
end
else
lpRecips := nil;
// arquivo anexo
if Arquivo = ´´ then
begin
nFileCount := 0;
lpFiles := nil;
end
else
begin
FillChar(FileAttach, SizeOf(FileAttach), 0);
FileAttach.nPosition := Cardinal($FFFFFFFF);
FileAttach.lpszPathName := PChar(Arquivo);
nFileCount := 1;
lpFiles := @FileAttach;
end;
end;
// carrega dll e o método para envio do email
MAPIModule := LoadLibrary(PChar(MAPIDLL));
if MAPIModule = 0 then
Result := -1
else
try
if Confirma then
Flags := MAPI_DIALOG or MAPI_LOGON_UI
else
Flags := 0;
@SM := GetProcAddress(MAPIModule, ´MAPISendMail´);
if @SM <> nil then
Result := SM(0, Application.Handle, Msg, Flags, 0)
else
Result := 1;
finally
FreeLibrary(MAPIModule);
end;
end;
Hebrao
Curtir tópico
+ 0
Responder
Posts
09/02/2007
Coelholeandro
eu usei esta rotina e deu certo:
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;
chamanda:
mail := TStringList.Create;
try
mail.values[´to´] := EditDestinatarios.Text;
mail.values[´subject´] := EditAssunto.text;
mail.values[´body´] := ´Segue anexo confirmação de pedido.´+#13+´Por Favor Confirmar o recebimento.´;
mail.values[´attachment0´] := tmpfile;
sendEMail(Application.Handle, mail);
finally
mail.Free;
end;
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;
chamanda:
mail := TStringList.Create;
try
mail.values[´to´] := EditDestinatarios.Text;
mail.values[´subject´] := EditAssunto.text;
mail.values[´body´] := ´Segue anexo confirmação de pedido.´+#13+´Por Favor Confirmar o recebimento.´;
mail.values[´attachment0´] := tmpfile;
sendEMail(Application.Handle, mail);
finally
mail.Free;
end;
Responder
Gostei + 0
09/02/2007
Hebrao
Resolvi o problema!
Vou postar aqui a solução caso alguém precise:
No meu código, basta colocar a string ´SMTP:´ concatenada com e email do destinatário...
valeu!
Heber
Vou postar aqui a solução caso alguém precise:
No meu código, basta colocar a string ´SMTP:´ concatenada com e email do destinatário...
valeu!
Heber
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)