I am trying extract attachments from EML files. I have no issue extracting Bas64 encoded attachments using System.Convert.FromBase64String but when it comes to QP I am stuck. The Attachments tend to be PDF. I have tried encoding a PDF into QP and writing the output to a text file, and then decoding back to a PDF with no success. Has anyone any pointers please ... any help would be greatly appreciated
I am using the following two routines which I found on the web
Private Sub cmdEncode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEncode.Click
Dim lReadFS As New FileStream(txtSourceEncode.Text, FileMode.Open)
Dim lSR As New System.IO.StreamReader(lReadFS, System.Text.Encoding.Default, True, _Buffer_size)
Dim lWriteFS As New FileStream(txtDestinationEncode.Text, FileMode.Create)
Dim lSW As New StreamWriter(lWriteFS)
Dim s As String = ""
Dim Line As String = lSR.ReadLine
While Not Line Is Nothing
s = QPEncode(Line.ToCharArray) + vbCrLf
lSW.Write(s)
Line = lSR.ReadLine
End While
lSR.Close()
lSW.Close()
lReadFS.Close()
lWriteFS.Close()
End Sub
Private Sub cmdDecode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecode.Click
Dim lReadFS As New FileStream(txtSourceDecode.Text, FileMode.Open)
Dim lSR As New System.IO.StreamReader(lReadFS, System.Text.Encoding.Default, True, _Buffer_size)
Dim lWriteFS As New FileStream(txtDestinationDecode.Text, FileMode.Create)
Dim lSW As New StreamWriter(lWriteFS)
Dim s As String = ""
Dim Line As String = lSR.ReadLine
While Not Line Is Nothing
s = QPDecode(Line.ToCharArray) + vbCrLf
lSW.Write(s)
Line = lSR.ReadLine
End While
lSR.Close()
lSW.Close()
lReadFS.Close()
lWriteFS.Close()
End Sub
Private Function QPEncode(ByVal Chars() As Char) As String
Dim i As Integer
Dim Ascii As Integer
Dim EncodedChar As String = ""
Dim ReturnString As New StringBuilder
For i = 0 To Chars.Length - 1
Ascii = Asc(Chars(i))
If Ascii < 32 Or Ascii = 61 Or Ascii > 126 Then
EncodedChar = Hex(Ascii).ToUpper
If EncodedChar.Length = 1 Then EncodedChar = "0" & EncodedChar
ReturnString.Append("=" & EncodedChar)
Else
ReturnString.Append(Chars(i))
End If
Next
Return ReturnString.ToString
End Function
Private Function QPDecode(ByVal Chars() As Char) As String
Dim i As Integer
Dim ReturnString As New StringBuilder
For i = 0 To Chars.Length - 1
If Chars(i) = "=" Then
Dim TheValue As String
If Chars(i + 1) = "0" Then
TheValue = Chars(i + 2)
Else
TheValue = Chars(i + 1) & Chars(i + 2)
End If
Dim IntValue As Integer = Val("&H" & TheValue)
If TheValue = Hex(IntValue) Then
ReturnString.Append(Chr(IntValue))
i += 2
Else
ReturnString.Append(Chars(i))
End If
Else
ReturnString.Append(Chars(i))
End If
Next
Return ReturnString.ToString
End Function