求邮件编码 Quoted-Printable 解碼UDF
线索: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 < 33 || bytes > 126 || bytes == EQUALS) && bytes != CR && bytes != LF && bytes != SPACE)
# {
# if (bytes.ToString("X").Length < 2)
# {
# hex = "0" + bytes.ToString("X");
# Encoded.Append("=" + hex);
# }
# else
# {
# hex = bytes.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 == TAB && bytes == LF) || (bytes == TAB && bytes == CR))
# {
# Encoded.Append("=0" + bytes.ToString("X"));
# }
# //if SPACE is at the end of the line - encode it!
# else if ((bytes == SPACE && bytes == LF) || (bytes == SPACE && bytes == CR))
# {
# Encoded.Append("=" + bytes.ToString("X"));
# }
# else
# {
# Encoded.Append(System.Convert.ToChar(bytes));
# }
# }
# else
# {
# Encoded.Append(System.Convert.ToChar(bytes));
# }
# }
# //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;
#
# int bytesCount = 0;
#
# for (int i = 0; i < chars.Length; i++)
# {
# // if encoded character found decode it
# if (chars == '=')
# {
# bytes = System.Convert.ToByte(int.Parse(chars.ToString() + chars.ToString(), System.Globalization.NumberStyles.HexNumber));
#
# i += 2;
# }
# else
# {
# bytes = System.Convert.ToByte(chars);
# }
# }
#
# //return System.Text.Encoding.Default.GetString(bytes, 0, bytesCount);
# return System.Text.Encoding.UTF8.GetString(bytes, 0, bytesCount);
# }
# } 请联系我QQ:114026307 楼主的ID真强大哈!{:face (411):} $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)
陈俊清,您好! 至于检查是否需要编码的字符 可以转换成asc之类的取范围试试看 {:face (394):}看不懂哟 {:face (394):}看不懂哟 {:face (394):}看不懂哟
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=' & @CRL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2=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, '={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
回复 9# 3mile
3m代码很帅!我有个问题想请教一下您,就是用Jmail收信时,如何提前知道邮件是用什么编码的?会不会同一个邮箱里面的不同的邮件会有不同的编码,那样的话,如何知道哪封邮件是哪种编码方式。不能所有的邮件都用一种解码方式吧,请前辈指点迷津。 应该可以了吧。。
页:
[1]