Árvore de páginas

Salva um arquivo anexo a mensagem em disco.

Sintaxe

SaveAttach( < nIndex >, < cPath > )

Parâmetros

Nome

Tipo

Descrição

Obrigatório

Referência

nIndex

numérico

Índice do anexo iniciando em 1.

X


cPath

caractere

Caminho com o nome do arquivo que será gravado.

X


Retorno

Nome

Tipo

Descrição

lRet

lógico

Retorna verdadeiro (.T.) caso consiga salvar o anexo com sucesso. Falso (.F.) caso contrário.

Observações

Em versões superiores a 19.3.1.8 e 20.3.0.5, o método irá mostrar as mensagens:

  • "Warning - Could not get attachment info for index [nIndex]!", em caso de erro ao pegar o anexo informado em nIndex.
  • "Warning - Could not open attachment file [FILE]!", em caso de erro ao abrir o arquivo FILE, que é o caminho do arquivo temporário de anexo na máquina do TOTVS | Application Server.
  • "Warning - Could not create attachment file [cPath]!", em caso de erro ao criar o arquivo informado em cPath.
  • "Warning - Could not write in attachment file [cPath]!", em caso de erro ao escrever no arquivo informado em cPath.

Exemplos

user function saveAttach()
  Local oServer
  Local oMessage
  Local nAttach := 0, nI := 0
  Local nMessages := 0
  Local aAttInfo := {}
  Local cBaseName := "", cName := ""
  Local xRet
  
  oServer := TMailManager():New()
  writePProString( "Mail", "Protocol", "POP3", getsrvininame() )
  
  oServer:SetUseSSL( .T. )
  xRet := oServer:Init( "mail.totvs.com.br", "", "username", "password", 995, 0 )
  if xRet <> 0
    conout( "Could not initialize mail server: " + oServer:GetErrorString( xRet ) )
    return
  endif
  
  xRet := oServer:POPConnect()
  if xRet <> 0
    conout( "Could not connect on POP3 server: " + oServer:GetErrorString( xRet ) )
    return
  endif
  
  oServer:GetNumMsgs( @nMessages )
  conout( "Number of messages: " + cValToChar( nMessages ) )
  
  oMessage := TMailMessage():New()
  oMessage:Clear()
  
  conout( "Receiving newest message" )
  xRet := oMessage:Receive( oServer, nMessages )
  if xRet <> 0
    conout( "Could not get message " + cValToChar( nMessages ) + ": " + oServer:GetErrorString( xRet ) )
    return
  endif
  
  cBaseName := GetSrvProfString( "RootPath", "" )
  if Right( cBaseName, 1 ) <> '\'
    cBaseName += '\'
  endif
  cBaseName += "mail\pop3\"
  
  nAttach := oMessage:GetAttachCount()
  for nI := 1 to nAttach
    aAttInfo := oMessage:GetAttachInfo( nI )
    varinfo( "", aAttInfo )
    
    cName := cBaseName
    
    if aAttInfo[1] == ""
      cName += "message." + SubStr( aAttInfo[2], At( "/", aAttInfo[2] ) + 1, Len( aAttInfo[2] ) )
    else
      cName += aAttInfo[1]
    endif
    
    conout( "Saving attachment " + cValToChar( nI ) + ": " + cName )
    xRet := oMessage:SaveAttach( nI, cName )
    if xRet == .F.
      conout( "Could not save attachment " + cName  )
      loop
    endif
  next nI
  
  xRet := oServer:POPDisconnect()
  if xRet <> 0
    conout( "Could not disconnect from POP3 server: " + oServer:GetErrorString( xRet ) )
  endif
return
  • Sem rótulos