找回密码
 加入
搜索
查看: 9902|回复: 10

[求购] 求邮件编码 Quoted-Printable 解碼UDF

[复制链接]
发表于 2010-6-26 12:11:36 | 显示全部楼层 |阅读模式
悬赏100金钱未解决
线索:
Quoted-Printable编码原理是把一个8bit的字符用两个16进制数值表示,然后在前面加“=”。所以我们看到经过QP编码后的文件通常是这个样子:=B3=C2=BF=A1=C7=E5=A3= AC=C4=FA=BA=C3=A3=A1。

Quoted -printable根据输入的字符串或字节范围进行编码,若是不需编码的字符,直接输出。若需要编码,则先输出'=',后面跟着以2个字符表示的十六进制字节值。有的场合,以“=?charset?Q?xxxxxxxx?=”表示xxxxxxxx是Quoted-printable编码,且原文的字符集是charset。在段体内则直接编码,适当时机换行,换行前额外输出一个'='。

详细出处参考:http://www.jb51.net/web/25072.html

提供一下网络搜索到的参考代码,请高人帮忙弄个解码UDF出来,谢谢!

代码参考1:
    Public Function HexDecode(ByVal p_sIn As String) As String
     Dim a As Integer
     Dim sOut As String

     If (Len(p_sIn) = 0) Then Exit Function

     For a = 1 To Len(p_sIn) Step 4
     sOut = sOut & Chr(CInt("&H" & Mid(p_sIn, a, 4)))
     Next a

     HexDecode = sOut
    End Function
代码参考2:

C# Quoted-Printable编码、解码
# using System; 
# using System.Collections; 
# using System.Text; 
# 
# /// <summary> 
# /// Class for encoding and decoding a string to QuotedPrintable 
# /// RFC 1521 http://www.ietf.org/rfc/rfc1521.txt 
# /// RFC 2045 http://www.ietf.org/rfc/rfc2045.txt 
# /// Date: 2006-03-23 
# /// Author: Kevin Spaun 
# /// Company: SPAUN Informationstechnik GmbH - http://www.spaun-it.com/ 
# /// Feedback: kspaun@spaun-it.de 
# /// License: This piece of code comes with no guaranties. You can use it for whatever you want for free. 
# /// 
# /// Modified by Will Huang ( http://blog.miniasp.com/post/2008/02/14/Quoted-Printable-Encoding-and-Decoding.aspx ) 
# /// Modified at 2008-02-13 
# /// 
# /// Modified by reterry (http://www.jb51.net) 
# /// Modified at 2008-11-29 
# /// Modified for MySelf 
# /// 
# /// </summary> 
# public class QuotedPrintable 
# { 
# private const byte EQUALS = 61; 
# private const byte CR = 13; 
# private const byte LF = 10; 
# private const byte SPACE = 32; 
# private const byte TAB = 9; 
# 
# /// <summary> 
# /// Encodes a string to QuotedPrintable 
# /// </summary> 
# /// <param name="_ToEncode">String to encode</param> 
# /// <returns>QuotedPrintable encoded string</returns> 
# public static string Encode(string _ToEncode) 
# { 
# StringBuilder Encoded = new StringBuilder(); 
# string hex = string.Empty; 
# //byte[] bytes = Encoding.Default.GetBytes(_ToEncode); 
# byte[] bytes = Encoding.UTF8.GetBytes(_ToEncode); 
# //int count = 0; 
# 
# for (int i = 0; i < bytes.Length; i++) 
# { 
# //these characters must be encoded 
# if ((bytes[i] < 33 || bytes[i] > 126 || bytes[i] == EQUALS) && bytes[i] != CR && bytes[i] != LF && bytes[i] != SPACE) 
# { 
# if (bytes[i].ToString("X").Length < 2) 
# { 
# hex = "0" + bytes[i].ToString("X"); 
# Encoded.Append("=" + hex); 
# } 
# else 
# { 
# hex = bytes[i].ToString("X"); 
# Encoded.Append("=" + hex); 
# } 
# } 
# else 
# { 
# //check if index out of range 
# if ((i + 1) < bytes.Length) 
# { 
# //if TAB is at the end of the line - encode it! 
# if ((bytes[i] == TAB && bytes[i + 1] == LF) || (bytes[i] == TAB && bytes[i + 1] == CR)) 
# { 
# Encoded.Append("=0" + bytes[i].ToString("X")); 
# } 
# //if SPACE is at the end of the line - encode it! 
# else if ((bytes[i] == SPACE && bytes[i + 1] == LF) || (bytes[i] == SPACE && bytes[i + 1] == CR)) 
# { 
# Encoded.Append("=" + bytes[i].ToString("X")); 
# } 
# else 
# { 
# Encoded.Append(System.Convert.ToChar(bytes[i])); 
# } 
# } 
# else 
# { 
# Encoded.Append(System.Convert.ToChar(bytes[i])); 
# } 
# } 
# //if (count == 75) 
# //{ 
# // Encoded.Append("=\r\n"); //insert soft-linebreak 
# // count = 0; 
# //} 
# //count++; 
# } 
# 
# return Encoded.ToString(); 
# } 
# 
# /// <summary> 
# /// Decodes a QuotedPrintable encoded string 
# /// </summary> 
# /// <param name="_ToDecode">The encoded string to decode</param> 
# /// <returns>Decoded string</returns> 
# public static string Decode(string _ToDecode) 
# { 
# //remove soft-linebreaks first 
# //_ToDecode = _ToDecode.Replace("=\r\n", ""); 
# 
# char[] chars = _ToDecode.ToCharArray(); 
# 
# byte[] bytes = new byte[chars.Length]; 
# 
# int bytesCount = 0; 
# 
# for (int i = 0; i < chars.Length; i++) 
# { 
# // if encoded character found decode it 
# if (chars[i] == '=') 
# { 
# bytes[bytesCount++] = System.Convert.ToByte(int.Parse(chars[i + 1].ToString() + chars[i + 2].ToString(), System.Globalization.NumberStyles.HexNumber)); 
# 
# i += 2; 
# } 
# else 
# { 
# bytes[bytesCount++] = System.Convert.ToByte(chars[i]); 
# } 
# } 
# 
# //return System.Text.Encoding.Default.GetString(bytes, 0, bytesCount); 
# return System.Text.Encoding.UTF8.GetString(bytes, 0, bytesCount); 
# } 
# }

发表于 2010-6-27 03:53:24 | 显示全部楼层
请联系我QQ:114026307
发表于 2010-7-5 13:33:47 | 显示全部楼层
楼主的ID真强大哈!
发表于 2010-9-1 22:37:20 | 显示全部楼层
$txt="=B3=C2=BF=A1=C7=E5=A3=AC=C4=FA=BA=C3=A3=A1"
$txt=StringReplace  ($txt,"=","")
$name=""

For $i=1 to StringLen($txt) Step 4
        $tmpName=StringMid($txt,$i,4)
$name &= StringStripCR(BinaryToString(Binary("0x"&$tmpName)))
Next
MsgBox(0,"",$name)


陈俊清,您好!
发表于 2010-9-1 22:39:15 | 显示全部楼层
至于检查是否需要编码的字符 可以转换成asc之类的取范围试试看
发表于 2011-5-24 20:04:27 | 显示全部楼层
看不懂哟
发表于 2011-5-24 20:04:32 | 显示全部楼层
看不懂哟
发表于 2011-5-24 20:05:05 | 显示全部楼层
看不懂哟
发表于 2011-5-30 21:13:20 | 显示全部楼层


Local $Str = _
                '=D0=FC=C9=CD=BD=F0=B6=EE: 100 =BF=E9=BD=F0=C7=AE=0D=0A=CF=DF=CB=F7=A3=BA=0D=' & @CRLF & _
                '=0AQuoted-Printable=B1=E0=C2=EB=D4=AD=C0=ED=CA=C7=B0=D1=D2=BB=B8=F68bit=B5=' & @CRLF & _
                '=C4=D7=D6=B7=FB=D3=C3=C1=BD=B8=F616=BD=F8=D6=C6=CA=FD=D6=B5=B1=ED=CA=BE=A3=' & @CRLF & _
                '=AC=C8=BB=BA=F3=D4=DA=C7=B0=C3=E6=BC=D3=A1=B0=3D=A1=B1=A1=A3=CB=F9=D2=D4=CE=' & @CRLF & _
                '=D2=C3=C7=BF=B4=B5=BD=BE=AD=B9=FDQP=B1=E0=C2=EB=BA=F3=B5=C4=CE=C4=BC=FE=CD=' & @CRLF & _
                '=A8=B3=A3=CA=C7=D5=E2=B8=F6=D1=F9=D7=D3=A3=BA=3DB3=3DC2=3DBF=3DA1=3DC7=3D=' & @CRLF & _
                'E5=3DA3=3D AC=3DC4=3DFA=3DBA=3DC3=3DA3=3DA1=A1=A3=0D=0A=0D=0AQuoted=20-pr=' & @CRLF & _
                'intable=B8=F9=BE=DD=CA=E4=C8=EB=B5=C4=D7=D6=B7=FB=B4=AE=BB=F2=D7=D6=BD=DA=' & @CRLF & _
                '=B7=B6=CE=A7=BD=F8=D0=D0=B1=E0=C2=EB=A3=AC=C8=F4=CA=C7=B2=BB=D0=E8=B1=E0=C2=' & @CRLF & _
                '=EB=B5=C4=D7=D6=B7=FB=A3=AC=D6=B1=BD=D3=CA=E4=B3=F6=A1=A3=C8=F4=D0=E8=D2=AA=' & @CRLF & _
                "=B1=E0=C2=EB=A3=AC=D4=F2=CF=C8=CA=E4=B3=F6'=3D'=A3=AC=BA=F3=C3=E6=B8=FA=D7=" & @CRLF & _
                '=C5=D2=D42=B8=F6=D7=D6=B7=FB=B1=ED=CA=BE=B5=C4=CA=AE=C1=F9=BD=F8=D6=C6=D7=' & @CRLF & _
                '=D6=BD=DA=D6=B5=A1=A3=D3=D0=B5=C4=B3=A1=BA=CF=A3=AC=D2=D4=A1=B0=3D?charse=' & @CRLF & _
                't?Q?xxxxxxxx?=3D=A1=B1=B1=ED=CA=BExxxxxxxx=CA=C7Quoted-printable=B1=E0=C2=' & @CRLF & _
                '=EB=A3=AC=C7=D2=D4=AD=CE=C4=B5=C4=D7=D6=B7=FB=BC=AF=CA=C7charset=A1=A3=D4=' & @CRLF & _
                '=DA=B6=CE=CC=E5=C4=DA=D4=F2=D6=B1=BD=D3=B1=E0=C2=EB=A3=AC=CA=CA=B5=B1=CA=B1=' & @CRLF & _
                '=BB=FA=BB=BB=D0=D0=A3=AC=BB=BB=D0=D0=C7=B0=B6=EE=CD=E2=CA=E4=B3=F6=D2=BB=B8=' & @CRLF & _
                "=F6'=3D'=A1=A3=0D=0A=0D=0A=CF=EA=CF=B8=B3=F6=B4=A6=B2=CE=BF=BC=A3=BAhttp:=" & @CRLF & _
                '//www.jb51.net/web/25072.html=0D=0A=0D=0A=CC=E1=B9=A9=D2=BB=CF=C2=CD=F8=C2=' & @CRLF & _
                '=E7=CB=D1=CB=F7=B5=BD=B5=C4=B2=CE=BF=BC=B4=FA=C2=EB=A3=AC=C7=EB=B8=DF=C8=CB=' & @CRLF & _
                '=B0=EF=C3=A6=C5=AA=B8=F6=BD=E2=C2=EBUDF=B3=F6=C0=B4=A3=AC=D0=BB=D0=BB=A3=A1=' & @CRLF

msgbox(0,0,QP_Decode($str))

Func QP_Decode($str)
Local $Test = StringRegExp($str, '=[0-9a-fA-F]{2}|[\x20-\x3c\x3e-\x7e\x09]', 3)
local $output=''
for $i=0 to UBound($Test)-1
        if StringLen($Test[$i])>1 then 
                $temp="0x"&StringReplace($Test[$i]&$Test[$i+1],"=","")
                $output&=BinaryToString($temp)
                $i+=1
        ElseIf $Test[$i]="=0D" And $Test[$i+1]="=0A" Then
                $output&=@CRLF
                $i+=1
        Else
                $output&=$Test[$i]
        EndIf
Next
Return $output
EndFunc
发表于 2013-1-18 23:10:11 | 显示全部楼层
回复 9# 3mile


    3m代码很帅!我有个问题想请教一下您,就是用Jmail收信时,如何提前知道邮件是用什么编码的?会不会同一个邮箱里面的不同的邮件会有不同的编码,那样的话,如何知道哪封邮件是哪种编码方式。不能所有的邮件都用一种解码方式吧,请前辈指点迷津。
发表于 2013-1-21 13:31:07 | 显示全部楼层
应该可以了吧。。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-29 09:44 , Processed in 0.086031 second(s), 17 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表