Realiza a autenticação no servidor SMTP - Simple Mail Transfer Protocol - para envio de mensagens.
Sintaxe
SMTPAuth( < cUser >, < cPass > )
Parâmetros
Nome | Tipo | Descrição | Obrigatório | Referência |
|---|---|---|---|---|
cUser | caractere | Indica o usuário no qual será feita a autenticação. | X | |
cPass | caractere | Indica a senha do usuário para autenticação. | X |
Retorno
Nome | Tipo | Descrição |
|---|---|---|
nRet | numérico | Retorna 0 (zero) em caso de sucesso. Caso contrário, retornará um código de erro. |
Observações
- O método SMTPAuth ao tentar realizar a autenticação do usuário no servidor de e-mail, verifica a configuração da chave AuthSmtp, na seção [Mail], no arquivo de configuração (INI) do TOTVS Application Server, para determinar o valor.
- Se a conexão do tipo POP/POP3 estiver sendo utilizada para envio de e-mail, o sistema verificará as chaves SmtpPopServer e SmtpPopPort, na seção [Mail], e realizará a autenticação pela conexão com o servidor POP. (Isto é realizado devido a configuração determinada por alguns servidores de e-mail). Caso contrário, será realizada a autenticação do usuário no servidor SMTP, através dos protocolos de autenticação suportados LOGIN, NTLM e PLAIN .
- Por padrão, o servidor habilita a tentativa de autenticação pelos protocolos LOGIN e NTLM. No entanto, é possível desabilitar algum tipo de autenticação através das chaves AuthNTLM=0 ou AuthLogin=0, na seção [Mail], do arquivo de configuração (INI) do TOTVS Application Server.
- Para habilitar a autenticação do tipo PLAIN, é necessário definir no arquivo de configuração (INI), do TOTVS Application Server, a chave AuthPlain=1, na seção [Mail].
- Se a conexão utilizada for usando o protocolo MAPI, o método retornará 1 (erro).
Exemplos
Exemplo 1
User function tstsmtp( cSMTPServer, cPort, cUser, cAccount, cPass, lAuth, nTimeout )
Local oServer := Nil
Local oMessage := Nil
Local nret := 0
oServer := TMailManager():New()
oServer:Init( "", cSMTPServer, cAccount, cPass, 0, val( cPort ) )
If( nTimeout <= 0 )
conout( "[TIMEOUT] DISABLE" )
Else
conout( "[TIMEOUT] ENABLE()" )
nRet := oServer:SetSmtpTimeOut( 120 )
If nRet != 0
conout( "[TIMEOUT] Fail to set" )
conout( "[TIMEOUT][ERROR] " + str( nRet, 6 ), oServer:GetErrorString( nRet ) )
Return .F.
EndIf
Endif
Conout( "[SMTPCONNECT] connecting ..." )
nRet := oServer:SmtpConnect()
If nRet != 0
conout( "[SMTPCONNECT] Falha ao conectar" )
conout( "[SMTPCONNECT][ERROR] " + str( nRet, 6 ), oServer:GetErrorString( nRet ) )
Return .F.
Else
conout( "[SMTPCONNECT] Sucesso ao conectar" )
EndIf
If lAuth
conout( "[AUTH] ENABLE" )
conout( "[AUTH] TRY with ACCOUNT() and PASS()" )
// try with account and pass
nRet := oServer:SMTPAuth( cAccount, cPass )
If nRet != 0
conout( "[AUTH] FAIL TRY with ACCOUNT() and PASS()")
conout( "[AUTH][ERROR] " + str(nRet,6) , oServer:GetErrorString( nRet ) )
conout( "[AUTH] TRY with USER() and PASS()" )
// try with user and pass
nRet := oServer:SMTPAuth( cUser, cPass )
If nRet != 0
conout( "[AUTH] FAIL TRY with USER() and PASS()" )
conout( "[AUTH][ERROR] " + str( nRet, 6 ), oServer:GetErrorString( nRet ) )
Return .F.
Else
conout( "[AUTH] SUCEEDED TRY with USER() and PASS()" )
Endif
Else
conout( "[AUTH] SUCEEDED TRY with ACCOUNT and PASS" )
Endif
Else
conout( "[AUTH] DISABLE" )
Endif
conout( "[MESSAGE] Criando mail message" )
oMessage := TMailMessage():New()
oMessage:Clear()
oMessage:cFrom := cAccount
oMessage:cTo := "[email protected]"
oMessage:cCc := ""
oMessage:cBcc := ""
oMessage:cSubject := "Teste de Email " + dtoc( date() ) + " " + time()
oMessage:cBody := "Teste"
conout( "[SEND] Sending ..." )
nRet := oMessage:Send( oServer )
If nRet != 0
conout( "[SEND] Fail to send message" )
conout( "[SEND][ERROR] " + str( nRet, 6 ), oServer:GetErrorString( nRet ) )
Else
conout( "[SEND] Success to send message" )
EndIf
conout( "[DISCONNECT] smtp disconnecting ... " )
nRet := oServer:SmtpDisconnect()
If nRet != 0
conout( "[DISCONNECT] Fail smtp disconnecting ... " )
conout( "[DISCONNECT][ERROR] " + str( nRet, 6 ), oServer:GetErrorString( nRet ) )
Else
conout( "[DISCONNECT] Success smtp disconnecting ... " )
EndIf
return
Visão Geral
Conteúdo das Ferramentas