Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    vb.net recuperare allegati mail POP3

    Buongiorno a tutti,

    dovrei realizzare una sorta di client di posta per leggere le email e gestirle.

    Ho fatto diversi esempi utilizzando i comandi per il pop3, e fin qui non ci sono problemi, mi collego al mio server di posta, mi autentico e recupero i messaggi.

    Il problema risulta recuperare gli allegati contenuti in questi messaggi!

    Ad es. un messaggio contenente un file .jpg, ad un certo punto presenta questo testo:

    ------=_NextPart_000_01A5_01CCBBEA.BB90EB80
    Content-Type: image/jpeg;
    name="letture.JPG"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;
    filename="letture.JPG"


    da cui si capisce il fatto che ci sia un allegato e il suo nome

    ma poi c'è una serie lunghissima di caratteri tipo

    /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a
    HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy
    MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjI



    che penso identifichino il file .jpg.

    come faccio io a far "tornare" questi caratteri l'immagine allegata?

    grazie a chi mi vorrà aiutare!

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,463
    Se hai del codice scritto per gestire quanto hai detto nel messaggio, postalo così possiamo suggerirti come correggerlo/migliorarlo.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  3. #3
    Codice per la parte in cui mi sono bloccata non ce l'ho perchè non so proprio come fare. Ma provo a mettere il punto esatto in cui mi blocco, magari aiuta a capire il mio problema.

    Riesco a collegarmi al mio server con il comando telnet mail.mioserver.it 110 e poi specificando user e password con i comandi USER e PASS (uso System.Net.Sockets)

    Poi con il comando STAT recupero il numero di messaggi della mia casella, e con il comando
    RETR li recupero uno per uno.

    E qui mi blocco.

    Riesco a recuperare diversi dati, tra cui l'oggetto, la data, il mittente, destinatario e anche il corpo del messaggio. Ma quest'ultimo sembra contenere, oltre al testo della mail, anche gli allegati in formato testo! come faccio ad estrarre questi allegati e farli ritornare file "normali"?

    ecco le mie funzioni (ma vanno tutte fin qui, il problema è da qui in avanti):

    codice:
        Public Function connect(ByVal pop3Server As String, ByVal userAccount As String, ByVal PasswordAccount As String) As Integer
            Dim POP3Account As String
            POP3Account = pop3Server '"mail.pop.yahoo.it"
            If POP3Account.Trim = "" Then Exit Function
            Try
                Server = New TcpClient(POP3Account.Trim, 110)
                NetStrm = Server.GetStream
                RdStrm = New StreamReader(Server.GetStream)
            Catch exc As Exception
                MsgBox(exc.Message)
    
                Exit Function
            End Try
    
            Dim user As String
            user = userAccount
            Dim data As String = "USER " + user.Trim + vbCrLf
            Dim szData() As Byte = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
            NetStrm.Write(szData, 0, szData.Length)
            Dim POPResponse As String
            POPResponse = RdStrm.ReadLine
            If POPResponse.Substring(0, 4) = "-ERR" Then
                MsgBox("User name non valido.")
                Return -1
            End If
            Dim password As String
            password = PasswordAccount '"test1234"
            data = "PASS " & password & vbCrLf
            szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
            NetStrm.Write(szData, 0, szData.Length)
            POPResponse = RdStrm.ReadLine
            If POPResponse.Substring(0, 4) = "-ERR" Then
                MsgBox("Invalida la Password")
                Return (-1)
            End If
            data = "STAT" + vbCrLf
            szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
            NetStrm.Write(szData, 0, szData.Length)
            POPResponse = RdStrm.ReadLine
            If POPResponse.Substring(0, 4) = "-ERR" Then
                MsgBox("Impossibile loggarsi,Controllare la password e nome account.")
                Return -1
            End If
            data = "Stat" + vbCrLf
            szData = System.Text.Encoding.ASCII.GetBytes(data.ToCharArray())
            NetStrm.Write(szData, 0, szData.Length)
            POPResponse = RdStrm.ReadLine
            If POPResponse.Substring(0, 4) = "-ERR" Then
                MsgBox("Impossibile loggarsi,Controllare la password e nome account.")
                Return -1
            End If
            Dim parts() As String
            parts = POPResponse.Split(" ")
            Dim messages As Integer
     
            messages = CInt(parts(1))
            Return messages
    
    
    
        End Function
    
    
    
        Function GetMessage(ByVal msgindex As Integer) As String
            Dim tmpString As String
            Dim Data As String
            Dim SzData() As Byte
            Dim msg As String
            msg = ""
            Try
                Data = "RETR " & msgindex.ToString & vbCrLf
                SzData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray())
                NetStrm.Write(SzData, 0, SzData.Length)
                tmpString = RdStrm.ReadLine()
                If tmpString.Substring(0, 4) <> "-ERR" Then
                    While (tmpString <> ".")
                        msg = msg & tmpString & vbCrLf
                        tmpString = RdStrm.ReadLine
                    End While
                End If
            Catch exc As InvalidOperationException
                MsgBox("Message Retrival Failed: " & vbCrLf & Err.ToString())
            End Try
            Return msg
        End Function
    
    
    
        Public Function CreateFromText(ByVal strMessage As String) As Message
            Dim Mssg As New Message
            Dim brkPos As Integer
            Dim Header As String
            Dim Headers() As String
            Dim Body As String
            Dim vField As Object
            Dim strHeader As String
            Dim HeaderName As String
            Dim HeaderValue As String
    
    
            brkPos = InStr(1, strMessage, vbCrLf & vbCrLf)
            If brkPos Then
                Header = strMessage.Substring(0, brkPos - 1)
                Body = strMessage.Substring(brkPos + 1, _
                strMessage.Length - Header.Length - 3)
                Mssg._Body = Body
            Else
                Throw New Exception("Formato messaggio non valido.")
                Exit Function
            End If
            Headers = Split(Header, vbCrLf)
            Dim _header As String
            For Each _header In Headers
                brkPos = _header.IndexOf(":")
                If brkPos >= 0 Then
                    HeaderName = _header.Substring(0, brkPos)
                Else
                    HeaderName = ""
                End If
                HeaderValue = _header.Substring(brkPos + 1)
                Select Case HeaderName.ToLower
                    Case "received"
                        Mssg._Received = HeaderValue
                    Case "from"
                        Mssg._From = HeaderValue
                    Case "to"
                        Mssg._To = HeaderValue
                    Case "cc"
                        Mssg._CC = HeaderValue
                    Case "bcc"
                        Mssg._BCC = HeaderValue
                    Case "subject"
                        Mssg._Subject = HeaderValue
                    Case "date"
                        Mssg._Date = HeaderValue
                End Select
            Next
            Return Mssg
        End Function

  4. #4
    Ciao a tutti, volevo avvisare che ho risolto usando questa libreria

    http://hpop.sourceforge.net/


    Grazie comunque per la vostra attenzione!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.