'(B for Base64, Q for Quoted-Printable)
Sub DecodeMIMEText(EncodedText As String) As String
Dim CharSet As String
Dim Buffer() As Byte
Dim SupportedEncodings() As String
Dim result As String
Dim Part As String
Dim cnv As ByteConverter
Dim sbj_sb As StringBuilder
Dim s_u As StringUtils
Dim k, j, x As Int
Dim booCharSetOK, HasMIMECoding, ValidMIME As Boolean
Dim MIMEStart, MIMEEnd As Int
Dim MIMEPart As String
Dim NONMIMEPart1 As String
Dim NONMIMEPart2 As String
sbj_sb.Initialize
SupportedEncodings = cnv.SupportedEncodings
MIMEStart = 0
MIMEEnd = 0
HasMIMECoding = False
MIMEStart = EncodedText.Trim.IndexOf("=?")
MIMEEnd = EncodedText.Trim.IndexOf("?=")
If MIMEEnd >=0 Then
MIMEEnd = MIMEEnd + 2 'To account for '?=' when will be be passed later through SubSustring Function
End If
'(B for Base64, Q for Quoted-Printable)
If EncodedText.Trim.IndexOf("?B?") >= 0 OR EncodedText.Trim.IndexOf("?Q?") >= 0 Then
HasMIMECoding = True
Else
HasMIMECoding = False
End If
If MIMEStart >= 0 AND MIMEEnd > MIMEStart AND HasMIMECoding = True Then
ValidMIME = True
If MIMEStart > 0 Then
NONMIMEPart1 = EncodedText.Trim.SubString2(0,MIMEStart)
Else
NONMIMEPart1 = ""
End If
MIMEPart = EncodedText.Trim.SubString2(MIMEStart,MIMEEnd)
If MIMEEnd < EncodedText.Trim.Length Then
NONMIMEPart2 = EncodedText.Trim.SubString(MIMEEnd)
Else
NONMIMEPart2 = ""
End If
Else
ValidMIME = False
NONMIMEPart1 = EncodedText
MIMEPart = ""
NONMIMEPart2 = ""
End If
If ValidMIME = True Then
'Check if String passed as parameter is MIME encoded
If MIMEPart.Length > 0 Then
'Check if MIME coding used is "B" (B for Base64, Q for Quoted-Printable)
If MIMEPart.Trim.Contains("?B?") = True Then
'Retrieve Character Set Used for Coding
CharSet = MIMEPart.Trim.SubString2(0, MIMEPart.Trim.IndexOf("?B?"))
If CharSet.Trim.StartsWith("=?") = True Then
CharSet = CharSet.Trim.SubString(2)
End If
booCharSetOK = False 'Check if Character Set of Header is supported by the system
For j = 0 To SupportedEncodings.Length - 1
If CharSet.ToUpperCase = SupportedEncodings(j).ToUpperCase Then
booCharSetOK = True
End If
Next
If booCharSetOK = True Then
Part = MIMEPart.Trim.SubString2(MIMEPart.Trim.IndexOf("?B?")+3,MIMEPart.Trim.Length-2)
Try
Buffer = s_u.DecodeBase64(Part)
'result = BytesToString(Buffer, 0, Buffer.Length, CharSet)
result = cnv.StringFromBytes(Buffer, CharSet)
Catch
result = MIMEPart
End Try
sbj_sb.Append(result.Trim)
Else
sbj_sb.Append(MIMEPart) 'Since Header's Character Set is not supported by the system, return it back asis
End If
'Check if MIME coding used is "Q" (B for Base64, Q for Quoted-Printable)
Else If MIMEPart.Contains("?Q?") = True Then
Dim WordsBuffer() As String
Dim CharsBuffer() As String
'Retrieve Character Set Used for Coding
CharSet = MIMEPart.Trim.SubString2(0, MIMEPart.Trim.IndexOf("?Q?"))
If CharSet.Trim.StartsWith("=?") = True Then
CharSet = CharSet.Trim.SubString(2)
End If
booCharSetOK = False 'Check if Character Set of Header is supported by the system
For j = 0 To SupportedEncodings.Length - 1
If CharSet.ToUpperCase = SupportedEncodings(j).ToUpperCase Then
booCharSetOK = True
End If
Next
If booCharSetOK = True Then
Part = MIMEPart.Trim.SubString2(MIMEPart.Trim.IndexOf("?Q?")+3,MIMEPart.Trim.Length-2).Trim
'Split string to Words
WordsBuffer=Regex.Split("_", Part)
For k = 0 To WordsBuffer.Length - 1
If WordsBuffer(k).Contains("=") = True Then
'Split Word to Characters
CharsBuffer=Regex.Split("=", WordsBuffer(k))
For x = 0 To CharsBuffer.Length - 1
Try
Buffer = cnv.HexToBytes(CharsBuffer(x))
'sbj_sb.Append(cnv.StringFromBytes(Buffer,"WINDOWS-1253").Trim)
sbj_sb.Append(cnv.StringFromBytes(Buffer,CharSet).Trim)
Catch
sbj_sb.Append(CharsBuffer(x))
End Try
Next
Else
sbj_sb.Append(WordsBuffer(k))
End If
sbj_sb.Append(" ")
Next
Else
sbj_sb.Append(MIMEPart) 'Since Header's Character Set is not supported by the system, return it back asis
End If
Else
sbj_sb.Append(MIMEPart) 'Header is not Encoded or Wrongly Encoded therefor send it back asis
End If
Else
sbj_sb.Append(MIMEPart) 'Header is not Encoded or Wrongly Encoded therefor send it back asis
End If
Return NONMIMEPart1 & sbj_sb.ToString & NONMIMEPart2
Else
Return NONMIMEPart1 & MIMEPart & NONMIMEPart2
End If
End Sub