玩转MAPI发送HTML邮件经验 | 雨律在线

因为MAPI没有导出参数支持HTML格式邮件,所以我们只能用附件带HTML文件来实现了。
然而在HTML文件中怎么附带图片呢?(编辑HTML使用DHTMLEdit控件,支持即…既…)
经过研究得知,发送HTML邮件的原理是用BASE64编码,那么很容易想到……
那就是在HTML文件中可以直接镶入图片,也是用BASE64编码的方法,在FireFox浏览器中“img对象可以直接使用data协议”,也就是说可以直接解析BASE64编码为图片,但是我在IE7上却调试不成功。
不过经过长时间人肉搜索发现,可以自定义解析,方法如下:

A modified "data" URL for DeleGate which is prefixed with "/-/" to the original URL:
SRC="/-/data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw AAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFz ByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSp a/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJl ZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uis F81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PH hhx4dbgYKAAA7" ALT="Larry">



在火狐浏览器中可以直接使用下面的代码,IE不行(网上说的保存为MHT也不行)。
SRC="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw AAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFz ByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSp a/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJl ZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uis F81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PH hhx4dbgYKAAA7" ALT="Larry">


这里提供自己解析的一个工具:
http://www.delegate.org/delegate/
http://www.delegate.org/delegate/download/
http://www.delegate.org/delegate/sample/data-url.html

#########################################################################################


Option Explicit

'需要引用Microsoft XML, v3.0
Private Function Encode(iArray() As Byte) As String
Dim
iXml As New MSXML2.DOMDocument30
With iXml.createElement("Encoder")
.dataType =
"bin.base64"
.nodeTypedValue = iArray()
Encode = .Text
End With
End Function


Private Function
Decode(ByVal iStrbase64 As String) As Byte()
Dim strXML As String
strXML = "& Chr(34) & "urn:schemas-microsoft-com:datatypes" & Chr(34) & " dt:dt=" & Chr(34) & "bin.base64" & Chr(34) & ">" & iStrbase64 & ""
With New MSXML2.DOMDocument30
.loadXML strXML
Decode = .selectSingleNode(
"DECODER").nodeTypedValue
End With
End Function



Public Function EncodeBase64(ByVal vsFullPathname As String) As String
'For Encoding BASE64
Dim b As Integer
Dim
Base64Tab As Variant
Dim
bin(3) As Byte
Dim
s As String
Dim
l As Long
Dim
i As Long
Dim
FileIn As Long
Dim
sResult As String
Dim
n As Long

'Base64Tab=>tabla de tabulaci髇
Base64Tab = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a
"
, "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/")

Erase bin
l =
0: i = 0: FileIn = 0: b = 0:
s =
""

'Gets the next free filenumber
FileIn = FreeFile

'Open Base64 Input File
Open vsFullPathname For Binary As FileIn

sResult = s & vbCrLf
s =
""

l = LOF(FileIn) - (LOF(FileIn) Mod 3)

For i = 1 To l Step 3

'Read three bytes
Get FileIn, , bin(0)
Get FileIn, , bin(1)
Get FileIn, , bin(2)

'Always wait until there're more then 64 characters
If Len(s) > 64 Then

s = s & vbCrLf
sResult = sResult & s
s =
""

End If

'Calc Base64-encoded char
b = (bin(n) \ 4) And &H3F 'right shift 2 bits (&H3F=111111b)
s = s & Base64Tab(b) 'the character s holds the encoded chars

b = ((bin(n) And &H3) * 16) Or ((bin(1) \ 16) And &HF)
s = s & Base64Tab(b)

b = ((bin(n +
1) And &HF) * 4) Or ((bin(2) \ 64) And &H3)
s = s & Base64Tab(b)

b = bin(n +
2) And &H3F
s = s & Base64Tab(b)

Next i

'Now, you need to check if there is something left
If Not (LOF(FileIn) Mod 3 = 0) Then

'Reads the number of bytes left
For i = 1 To (LOF(FileIn) Mod 3)
Get FileIn, , bin(i - 1)
Next i

'If there are only 2 chars left
If (LOF(FileIn) Mod 3) = 2 Then
b = (bin(0) \ 4) And &H3F 'right shift 2 bits (&H3F=111111b)
s = s & Base64Tab(b)


b = ((bin(
0) And &H3) * 16) Or ((bin(1) \ 16) And &HF)
s = s & Base64Tab(b)

b = ((bin(
1) And &HF) * 4) Or ((bin(2) \ 64) And &H3)
s = s & Base64Tab(b)

s = s &
"="

Else 'If there is only one char left
b = (bin(0) \ 4) And &H3F 'right shift 2 bits (&H3F=111111b)
s = s & Base64Tab(b)

b = ((bin(
0) And &H3) * 16) Or ((bin(1) \ 16) And &HF)
s = s & Base64Tab(b)

s = s &
"=="
End If
End If

'Send the characters left
If s <> "" Then
s = s & vbCrLf
sResult = sResult & s
End If

'Send the last part of the MIME Body
s = ""

Close FileIn
EncodeBase64 = sResult

End Function


 
目前有0条回应
Comment
Trackback
你目前的身份是游客,请输入昵称和电邮!