Newsgroup: it.comp.lang.visual-basic
Titolo: Conversione MIME

Buon giorno a tutto il NG.
Ho sviluppato un programma in Visual Basic per l'invio automatico di e-mail
che devono essere generate quando alcuni files arrivano su una determinata
cartella.
Questi files devono diventare degli allegati alla e-mail.
Qualcuno può indicarmi dove posso trovare il codice per trasformare questi
files in allegati utilizzando MIME Base64?
Sono già in possesso dell'algoritmo, riportato di seguito, che esegue
questa conversione, ma mi sembra di capire che devo passare il files come
una stringa nel parametro asContents della funzione Base64decode.
Come faccio a passare il files come stringa a questo parametro?
Grazie.
   '**************************************
    ' Name: Base 64 Encode / Decode
    ' Description:Base 64 encodeing is used
    '     to convert binary files to a "safe" form
    '     at for transporting files through smtp (
    '     email) and other protocols. It is also u
    '     sed for basic authentication. With this
    '     code, you can decode the current UserNam
    '     e/Password who is visiting a protected p
    '     age on your site by requesting one of th
    '     e serverVariables.
    ' By: Lewis Moten
    '
    ' Inputs:These two algorithims are expec
    '     ting a string that is to be encoded or d
    '     ecoded.
    '
    ' Returns:Returns a string that was enco
    '     ded or decoded.
    '
    ' Side Effects:The string returned from
    '     encoding with Base64 is not delimited by
    '     cariadge returns. Some formats that I ha
    '     ve seen in the past, such as email - may
    '     continue to the next line after the 80th
    '     character.
    Also, these functions work With Strings. (Not binary data). When
working with binary data, some changes may need To come into place.
    '
    'This code is copyrighted and has    ' limited warranties.Please see
http://w
    '     
ww.Planet-Source-Code.com/xq/ASP/txtCode
    '     Id.6268/lngWId.4/qx/vb/scripts/ShowCode.
    '     htm    'for details.    '**************************************
    Const sBASE_64_CHARACTERS
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    ' --------------------------------------
    '     ---------------------------------------
    function Base64decode(ByVal asContents)
     Dim lsResult
     Dim lnPosition
     Dim lsGroup64, lsGroupBinary
     Dim Char1, Char2, Char3, Char4
     Dim Byte1, Byte2, Byte3
     if Len(asContents) Mod 4 > 0 Then asContents = asContents & String(4 -
(Len(asContents) Mod 4), " ")
     lsResult = ""
     For lnPosition = 1 To Len(asContents) Step 4
          lsGroupBinary = ""
          lsGroup64 = Mid(asContents, lnPosition, 4)
          Char1 = INSTR(sBASE_64_CHARACTERS, Mid(lsGroup64, 1, 1)) - 1
          Char2 = INSTR(sBASE_64_CHARACTERS, Mid(lsGroup64, 2, 1)) - 1
          Char3 = INSTR(sBASE_64_CHARACTERS, Mid(lsGroup64, 3, 1)) - 1
          Char4 = INSTR(sBASE_64_CHARACTERS, Mid(lsGroup64, 4, 1)) - 1
          Byte1 = Chr(((Char2 And 48) \ 16) Or (Char1 * 4) And &HFF)
          Byte2 = lsGroupBinary & Chr(((Char3 And 60) \ 4) Or (Char2 * 16)
And &HFF)
          Byte3 = Chr((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))
          lsGroupBinary = Byte1 & Byte2 & Byte3
          lsResult = lsResult + lsGroupBinary
     Next
     Base64decode = lsResult
    End function
    ' --------------------------------------
    '     ---------------------------------------
    function Base64encode(ByVal asContents)
     Dim lnPosition
     Dim lsResult
     Dim Char1
     Dim Char2
     Dim Char3
     Dim Char4
     Dim Byte1
     Dim Byte2
     Dim Byte3
     Dim SaveBits1
     Dim SaveBits2
     Dim lsGroupBinary
     Dim lsGroup64
     if Len(asContents) Mod 3 > 0 Then asContents = asContents & String(3 -
(Len(asContents) Mod 3), " ")
     lsResult = ""
     For lnPosition = 1 To Len(asContents) Step 3
          lsGroup64 = ""
          lsGroupBinary = Mid(asContents, lnPosition, 3)
          Byte1 = Asc(Mid(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
          Byte2 = Asc(Mid(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
          Byte3 = Asc(Mid(lsGroupBinary, 3, 1))
          Char1 = Mid(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1)
          Char2 = Mid(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or
(SaveBits1 * 16) And &HFF) + 1, 1)
          Char3 = Mid(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or
(SaveBits2 * 4) And &HFF) + 1, 1)
          Char4 = Mid(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1)
          lsGroup64 = Char1 & Char2 & Char3 & Char4
          lsResult = lsResult + lsGroup64
     Next
     Base64encode = lsResult
    End function
    ' --------------------------------------
    '     ---------------------------------------
-- 
Posted from [217.57.245.132] 
via Mailgate.ORG Server - 
http://www.Mailgate.ORG

 Come faccio a passare il files come stringa a questo parametro?
    Dim NFile As Integer
    Dim strAllFile As String
    NFile = FreeFile()
    Open "C:\Documenti\Allegato1.zip" For Binary As #NFile
        strAllFile = Input$(LOF(NFile), NFile)
    Close #NFile
questo è uno dei modi per caricare tutto il contenuto di un file in una
stringa...
Ciao
Faber

     Close #NFile
[MSDN]
Note   Use the InputB function for byte data contained within text files.
With InputB, number specifies the number of bytes to return rather than the
number of characters to return.
[--------]
O meglio ancora l'uso di un Byte Array redimmado a LOF(NFile)-1, con un
check per LOF(NFile) = 0,ma questa e' una preferenza personale :)
Ciaotutti,
Asterix