可以生成邮件,可以发送邮件,稍做修改就可以写成一个com组件,在asp里调用.
以后我会整理成一个完整的.
——————————————————————————–
————————————————-
生成基本邮件格式(包括附件),发送邮件到smtp服务器,
只能发送到发件人smtp服务器(需验证),直接投递功能正在编写。
声明:本段代码中,有一部份借签了网上一位大侠的c#代码.由于找不到原文,无法写出原作者名字
代码编写:头太晕
qq:2538288
msn:qq2538288@hotmail.com
blog: http://spaces.msn.com/members/headfaint http://blog.csdn.net/super852
————————————————-
imports system
imports system.text
imports system.io
imports system.net
imports system.net.sockets
imports system.collections
namespace ewebmail
public class mail
邮件类,生成基本的邮件格式。访问作者blog: http://spaces.msn.com/members/headfaint
public charset as string = "gb2312"
public from as string
public fromname as string
public replyto as string
public subject as string = ""
public ishtml as boolean = false
public body as string = ""
public textbody as string = "this is a html mail."
public recipientmaxnum as integer = 15 最大收件人数 访问作者blog: http://spaces.msn.com/members/headfaint
public recipient as new arraylist
public recipientcc as new arraylist
public recipientbcc as new arraylist
protected mpriority as string = "normal"
protected boundary as string = "=====000_ewebmail0099887766554433_====="
protected boundary1 as string = "=====001_ewebmail0099887766554433_====="
protected attachments as new arraylist
protected attachmentssb as new stringbuilder
protected recipientname as string = ""
private shared fileht as hashtable
shared sub new()
添加一些常见的文件格式 访问作者blog: http://spaces.msn.com/members/headfaint
fileht = new hashtable
fileht.add(".323", "text/h323")
fileht.add(".3g2", "video/3gpp2")
fileht.add(".3gp", "video/3gpp")
fileht.add(".act", "text/xml")
fileht.add(".actproj", "text/plain")
fileht.add(".ai", "application/postscript")
fileht.add(".aif", "audio/aiff")
fileht.add(".aifc", "audio/aiff")
fileht.add(".aiff", "audio/aiff")
fileht.add(".asa", "text/asa")
fileht.add(".asf", "video/x-ms-asf")
fileht.add(".asm", "text/plain")
fileht.add(".asp", "text/asp")
fileht.add(".asx", "video/x-ms-asf")
fileht.add(".au", "audio/basic")
fileht.add(".avi", "video/avi")
fileht.add(".bmp", "image/bmp")
fileht.add(".c", "text/plain")
fileht.add(".cat", "application/vnd.ms-pki.seccat")
fileht.add(".cc", "text/plain")
fileht.add(".cdf", "application/x-netcdf")
fileht.add(".cer", "application/x-x509-ca-cert")
fileht.add(".class", "java/*")
fileht.add(".cod", "text/plain")
fileht.add(".cpp", "text/plain")
fileht.add(".crl", "application/pkix-crl")
fileht.add(".crt", "application/x-x509-ca-cert")
fileht.add(".cs", "text/plain")
fileht.add(".css", "text/css")
fileht.add(".cxx", "text/plain")
fileht.add(".dbs", "text/plain")
fileht.add(".def", "text/plain")
fileht.add(".der", "application/x-x509-ca-cert")
fileht.add(".dib", "image/bmp")
fileht.add(".dll", "application/x-msdownload")
fileht.add(".doc", "application/msword")
fileht.add(".dot", "application/msword")
fileht.add(".dps", "interface/vnd.divx-skin")
fileht.add(".dsp", "text/plain")
fileht.add(".dsw", "text/plain")
fileht.add(".dxu", "video/vnd.divx-playlist")
fileht.add(".edn", "application/vnd.adobe.edn")
fileht.add(".eml", "message/rfc822")
fileht.add(".eps", "application/postscript")
fileht.add(".etd", "application/x-ebx")
fileht.add(".etp", "text/plain")
fileht.add(".exe", "application/x-msdownload")
fileht.add(".ext", "text/plain")
fileht.add(".fdf", "application/vnd.fdf")
fileht.add(".fif", "application/fractals")
fileht.add(".fky", "text/plain")
fileht.add(".gif", "image/gif")
fileht.add(".h", "text/plain")
fileht.add(".hpp", "text/plain")
fileht.add(".hqx", "application/mac-binhex40")
fileht.add(".hta", "application/hta")
fileht.add(".htc", "text/x-component")
fileht.add(".htm", "text/html")
fileht.add(".html", "text/html")
fileht.add(".htt", "text/webviewhtml")
fileht.add(".htx", "text/html")
fileht.add(".hxx", "text/plain")
fileht.add(".i", "text/plain")
fileht.add(".ico", "image/x-icon")
fileht.add(".idl", "text/plain")
fileht.add(".iii", "application/x-iphone")
fileht.add(".inc", "text/plain")
fileht.add(".inl", "text/plain")
fileht.add(".ins", "application/x-internet-signup")
fileht.add(".isp", "application/x-internet-signup")
fileht.add(".java", "java/*")
fileht.add(".jfif", "image/jpeg")
fileht.add(".jpe", "image/jpeg")
fileht.add(".jpeg", "image/jpeg")
fileht.add(".jpg", "image/jpeg")
fileht.add(".js", "application/x-javascript")
fileht.add(".kci", "text/plain")
fileht.add(".latex", "application/x-latex")
fileht.add(".lgn", "text/plain")
fileht.add(".ls", "application/x-javascript")
fileht.add(".lst", "text/plain")
fileht.add(".m1v", "video/mpeg")
fileht.add(".m3u", "audio/x-mpegurl")
fileht.add(".mak", "text/plain")
fileht.add(".man", "application/x-troff-man")
fileht.add(".map", "text/plain")
fileht.add(".mdb", "application/msaccess")
fileht.add(".mfp", "application/x-shockwave-flash")
fileht.add(".mht", "message/rfc822")
fileht.add(".mhtml", "message/rfc822")
fileht.add(".mid", "audio/mid")
fileht.add(".midi", "audio/mid")
fileht.add(".mk", "text/plain")
fileht.add(".mocha", "application/x-javascript")
fileht.add(".movie", "video/x-sgi-movie")
fileht.add(".mp2", "video/mpeg")
fileht.add(".mp2v", "video/mpeg")
fileht.add(".mp3", "audio/mpeg")
fileht.add(".mpa", "video/mpeg")
fileht.add(".mpe", "video/mpeg")
fileht.add(".mpeg", "video/mpeg")
fileht.add(".mpg", "video/mpeg")
fileht.add(".mpv2", "video/mpeg")
fileht.add(".nmw", "application/nmwb")
fileht.add(".nws", "message/rfc822")
fileht.add(".odh", "text/plain")
fileht.add(".odl", "text/plain")
fileht.add(".p10", "application/pkcs10")
fileht.add(".p12", "application/x-pkcs12")
fileht.add(".p7b", "application/x-pkcs7-certificates")
fileht.add(".p7c", "application/pkcs7-mime")
fileht.add(".p7m", "application/pkcs7-mime")
fileht.add(".p7r", "application/x-pkcs7-certreqresp")
fileht.add(".p7s", "application/pkcs7-signature")
fileht.add(".pdf", "application/pdf")
fileht.add(".pdx", "application/vnd.adobe.pdx")
fileht.add(".pfx", "application/x-pkcs12")
fileht.add(".pko", "application/vnd.ms-pki.pko")
fileht.add(".pl", "application/x-perl")
fileht.add(".plg", "text/html")
fileht.add(".png", "image/png")
fileht.add(".prc", "text/plain")
fileht.add(".prf", "application/pics-rules")
fileht.add(".ps", "application/postscript")
fileht.add(".py", "text/plain")
fileht.add(".pys", "text/plain")
fileht.add(".pyw", "text/plain")
fileht.add(".ra", "audio/vnd.rn-realaudio")
fileht.add(".ram", "audio/x-pn-realaudio")
fileht.add(".rat", "application/rat-file")
fileht.add(".rc", "text/plain")
fileht.add(".rc2", "text/plain")
fileht.add(".rct", "text/plain")
fileht.add(".rgs", "text/plain")
fileht.add(".rjs", "application/vnd.rn-realsystem-rjs")
fileht.add(".rjt", "application/vnd.rn-realsystem-rjt")
fileht.add(".rm", "application/vnd.rn-realmedia")
fileht.add(".rmf", "application/vnd.adobe.rmf")
fileht.add(".rmi", "audio/mid")
fileht.add(".rmj", "application/vnd.rn-realsystem-rmj")
fileht.add(".rmm", "audio/x-pn-realaudio")
fileht.add(".rmp", "application/vnd.rn-rn_music_package")
fileht.add(".rms", "application/vnd.rn-realmedia-secure")
fileht.add(".rmvb", "application/vnd.rn-realmedia-vbr")
fileht.add(".rmx", "application/vnd.rn-realsystem-rmx")
fileht.add(".rnx", "application/vnd.rn-realplayer")
fileht.add(".rp", "image/vnd.rn-realpix")
fileht.add(".rpm", "audio/x-pn-realaudio-plugin")
fileht.add(".rsml", "application/vnd.rn-rsml")
fileht.add(".rt", "text/vnd.rn-realtext")
fileht.add(".rtf", "application/msword")
fileht.add(".rul", "text/plain")
fileht.add(".rv", "video/vnd.rn-realvideo")
fileht.add(".s", "text/plain")
fileht.add(".sct", "text/scriptlet")
fileht.add(".sit", "application/x-stuffit")
fileht.add(".sln", "application/octet-stream")
fileht.add(".smi", "application/smil")
fileht.add(".smil", "application/smil")
fileht.add(".snd", "audio/basic")
fileht.add(".sol", "text/plain")
fileht.add(".sor", "text/plain")
fileht.add(".spc", "application/x-pkcs7-certificates")
fileht.add(".spl", "application/futuresplash")
fileht.add(".sql", "text/plain")
fileht.add(".srf", "text/plain")
fileht.add(".sst", "application/vnd.ms-pki.certstore")
fileht.add(".stl", "application/vnd.ms-pki.stl")
fileht.add(".stm", "text/html")
fileht.add(".swf", "application/x-shockwave-flash")
fileht.add(".tab", "text/plain")
fileht.add(".tdl", "text/xml")
fileht.add(".tif", "image/tiff")
fileht.add(".tiff", "image/tiff")
fileht.add(".tlh", "text/plain")
fileht.add(".tli", "text/plain")
fileht.add(".torrent", "application/x-bittorrent")
fileht.add(".trg", "text/plain")
fileht.add(".txt", "text/plain")
fileht.add(".udf", "text/plain")
fileht.add(".udt", "text/plain")
fileht.add(".uls", "text/iuls")
fileht.add(".user", "text/plain")
fileht.add(".usr", "text/plain")
fileht.add(".vb", "text/plain")
fileht.add(".vcf", "text/x-vcard")
fileht.add(".vcproj", "text/plain")
fileht.add(".viw", "text/plain")
fileht.add(".vspscc", "text/plain")
fileht.add(".vsscc", "text/plain")
fileht.add(".vssscc", "text/plain")
fileht.add(".wav", "audio/x-wav")
fileht.add(".wax", "audio/x-ms-wax")
fileht.add(".wiz", "application/msword")
fileht.add(".wm", "video/x-ms-wm")
fileht.add(".wma", "audio/x-ms-wma")
fileht.add(".wmd", "application/x-ms-wmd")
fileht.add(".wmv", "video/x-ms-wmv")
fileht.add(".wmx", "video/x-ms-wmx")
fileht.add(".wmz", "application/x-ms-wmz")
fileht.add(".wpl", "application/vnd.ms-wpl")
fileht.add(".wsc", "text/scriptlet")
fileht.add(".wvx", "video/x-ms-wvx")
fileht.add(".xbm", "image/x-xbitmap")
fileht.add(".xdp", "application/vnd.adobe.xdp+xml")
fileht.add(".xfd", "application/vnd.adobe.xfd+xml")
fileht.add(".xfdf", "application/vnd.adobe.xfdf")
fileht.add(".xls", "application/vnd.ms-excel")
fileht.add(".xml", "text/xml")
fileht.add(".xsl", "text/xml")
fileht.add(".ymg", "application/ymsgr")
fileht.add(".yps", "application/ymsgr")
fileht.add(".z", "application/x-compress")
end sub
public shared function getmime(byval strfilename as string) as string
根据文件扩展名获取文件的格式 访问作者blog: http://spaces.msn.com/members/headfaint
if fileht(strfilename) <> nothing then return fileht(strfilename) else return "*/*"
end function
public property rcpname() as string
收件人名称 访问作者blog: http://spaces.msn.com/members/headfaint
get
if recipientname <> "" then return recipientname
if recipient.count > 0 then return recipient(0)
return ""
end get
set(byval value as string)
recipientname = value
end set
end property
public function addrecipient(byval str as string) as boolean
添加一个收件人地址 访问作者blog: http://spaces.msn.com/members/headfaint
return addrs(str, recipient)
end function
public function addrecipient(byval str() as string) as boolean
添加一组收件人地址 访问作者blog: http://spaces.msn.com/members/headfaint
return addrs(str, recipient)
end function
public function addrecipientcc(byval str() as string) as boolean
添加一组抄送地址 访问作者blog: http://spaces.msn.com/members/headfaint
return addrs(str, recipientcc)
end function
public function addrecipientbcc(byval str() as string) as boolean
添加一组暗送地址 访问作者blog: http://spaces.msn.com/members/headfaint
return addrs(str, recipientbcc)
end function
public function addrecipientcc(byval str as string) as boolean
添加一个抄送地址 访问作者blog: http://spaces.msn.com/members/headfaint
return addrs(str, recipientcc)
end function
public function addrecipientbcc(byval str as string) as boolean
添加一个暗送地址 访问作者blog: http://spaces.msn.com/members/headfaint
return addrs(str, recipientbcc)
end function
protected function addrs(byval str as string, byref ra as arraylist) as boolean
添加一个邮件地址到一个列表中 访问作者blog: http://spaces.msn.com/members/headfaint
str = str.trim()
if str = "" or str.indexof("@") = -1 then
return true
end if
if ra.count < recipientmaxnum then
ra.add(str)
return true
else
ra.clear()
return false
end if
end function
protected function addrs(byval str() as string, byref ra as arraylist) as boolean
添加一组邮件地址到一个列表中 访问作者blog: http://spaces.msn.com/members/headfaint
dim i as integer
for i = 0 to str.length – 1
if not addrs(str(i), ra) then
return false
end if
next
end function
public function addattachment(byval path as string, optional byval strcid as string = "") as string
添加一个文件到附件中,并设置一个id,用来在html格式邮件正文中调用 访问作者blog: http://spaces.msn.com/members/headfaint
if file.exists(path) then
dim fs as filestream
try
fs = new filestream(path, filemode.open)
catch ex as exception
return "error no file!"
end try
dim strreturn as string = addattachment(fs, path, strcid)
fs.close()
return strreturn
else
return "error no file!"
end if
end function
public function addattachment(byref attachmentstream as stream, byval attachmentname as string, byval strcid as string)
添加一个数据流,保存到附件中,并设置一个id。 访问作者blog: http://spaces.msn.com/members/headfaint
if attachmentstream.length > 0 then
attachments.add(attachmentname)
dim sl as long = attachmentstream.length
dim barray(sl) as byte
dim dotidx as integer = attachmentname.lastindexof(".")
dim strtype as string
if dotidx <> -1 then strtype = getmime(attachmentname.substring(dotidx)) else strtype = "*/*"
attachmentstream.read(barray, 0, sl)
attachmentssb.append("–" & boundary & vbnewline)
attachmentssb.append("content-type: " & strtype & "; name=""" & attachmentnamestr(attachmentname.substring(attachmentname.lastindexof("\") + 1)) & """" & vbnewline)
attachmentssb.append("content-transfer-encoding: base64" & vbnewline)
if strcid = "" then
strcid = getrndstr()
end if
attachmentssb.append("content-id: <" & strcid & ">" & vbnewline)
attachmentssb.append("content-disposition: attachment; filename=""" & attachmentnamestr(attachmentname.substring(attachmentname.lastindexof("\") + 1)) & """" & vbnewline & vbnewline)
attachmentssb.append(base64.strline(convert.tobase64string(barray)) & vbnewline & vbnewline)
return strcid
else
return "error no data!"
end if
end function
private function getrndstr() as string
当没有为附件设置id时,自动随机生成一个id 访问作者blog: http://spaces.msn.com/members/headfaint
dim strtemp as string = ""
do while strtemp.length < 6
randomize()
strtemp += chr(int(26 * rnd() + 65))
loop
return strtemp
end function
protected function attachmentnamestr(byval fn as string) as string
生成邮件标题 访问作者blog: http://spaces.msn.com/members/headfaint
if encoding.default.getbytecount(fn) > fn.length then
return "=?" & charset.toupper() & "?b?" & base64.encode(fn) + "?="
else
return fn
end if
end function
public property priority() as string
设置邮件的优先级 访问作者blog: http://spaces.msn.com/members/headfaint
get
return mpriority
end get
set(byval value as string)
select case value
case "1", "high"
mpriority = "high"
case "3", "normal"
mpriority = "normal"
case "5", "low"
mpriority = "low"
end select
end set
end property
public overrides function tostring() as string
重新编写tostring方法,用于输出整体的邮件格式文本。
这是一个十分关键的函数 访问作者blog: http://spaces.msn.com/members/headfaint
dim sendbufferstr as string
dim stritem as string
if charset = "" then
sendbufferstr = "from:""" & fromname & """ <" & from & ">" & vbnewline
else
sendbufferstr = "from:""=?" & charset.toupper() & "?b?" & base64.encode(fromname) & "?="" <" & from & ">" & vbnewline
end if
if replyto <> "" then sendbufferstr += "reply-to: " & replyto & vbnewline
if recipient.count > 0 then
sendbufferstr += "to:"
for each stritem in recipient
sendbufferstr += stritem & "<" & stritem & ">," & vbnewline
next
sendbufferstr = sendbufferstr.substring(0, sendbufferstr.length – 3) & vbnewline
end if
if recipientcc.count > 0 then
sendbufferstr += "cc:"
for each stritem in recipientcc
sendbufferstr += stritem & "<" & stritem & ">," & vbnewline
next
sendbufferstr = sendbufferstr.substring(0, sendbufferstr.length – 3) & vbnewline
end if
if recipientbcc.count > 0 then
sendbufferstr += "bcc:"
for each stritem in recipientbcc
sendbufferstr += stritem & "<" & stritem & ">," & vbnewline
next
sendbufferstr = sendbufferstr.substring(0, sendbufferstr.length – 3) & vbnewline
end if
if charset = "" then
sendbufferstr += "subject:" & subject & vbnewline
else
sendbufferstr += "subject:" & "=?" & charset.toupper() & "?b?" & base64.encode(subject) & "?=" & vbnewline
end if
sendbufferstr += "x-priority:" & priority & vbnewline
sendbufferstr += "x-msmail-priority:" & priority & vbnewline
sendbufferstr += "importance:" & priority & vbnewline
sendbufferstr += "x-mailer: ewebmail" & vbnewline
sendbufferstr += "mime-version: 1.0" & vbnewline
if attachments.count > 0 then
sendbufferstr += "content-type: multipart/related;" & vbnewline & " boundary=""" & boundary & """;" & vbnewline & " type=""multipart/alternative""" & vbnewline & vbnewline
sendbufferstr += "this is a multi-part message in mime format." & vbnewline & vbnewline
sendbufferstr += "–" & boundary & vbnewline
end if
if ishtml then
sendbufferstr += "content-type: multipart/alternative;" & vbnewline & " boundary=""" & boundary1 & """" & vbnewline & vbnewline & vbnewline
sendbufferstr += "this is a multi-part message in mime format." & vbnewline & vbnewline
sendbufferstr += "–" & boundary1 & vbnewline
sendbufferstr += "content-type: text/plain;" & vbnewline
if charset = "" then
sendbufferstr += " charset=""iso-8859-1""" & vbnewline
else
sendbufferstr += " charset=""" & charset.tolower() & """" & vbnewline
end if
sendbufferstr += "content-transfer-encoding: base64" & vbnewline & vbnewline
sendbufferstr += base64.strline(base64.encode(textbody)) & vbnewline & vbnewline & "–" & boundary1 & vbnewline & "content-type: text/html;" & vbnewline
else
sendbufferstr += "content-type: text/plain;" & vbnewline
end if
if charset = "" then
sendbufferstr += " charset=""iso-8859-1""" & vbnewline
else
sendbufferstr += " charset=""" & charset.tolower() & """" & vbnewline
end if
sendbufferstr += "content-transfer-encoding: base64" & vbnewline & vbnewline
sendbufferstr += base64.strline(base64.encode(body)) & vbnewline
if ishtml then sendbufferstr += vbnewline & "–" & boundary1 & "–" & vbnewline
if attachments.count > 0 then
sendbufferstr += vbnewline & attachmentssb.tostring()
sendbufferstr += "–" & boundary & "–" & vbnewline & vbnewline
end if
return sendbufferstr
end function
end class
class base64
用base64编码 访问作者blog: http://spaces.msn.com/members/headfaint
public shared function encode(byval str as string) as string
将字符串编码 访问作者blog: http://spaces.msn.com/members/headfaint
return convert.tobase64string(encoding.default.getbytes(str))
end function
public shared function decode(byval str as string) as string
将字符串解码 访问作者blog: http://spaces.msn.com/members/headfaint
return encoding.default.getstring(convert.frombase64string(str))
end function
public shared function strline(byval str as string) as string
将长的字符串内容按邮件格式进行base64编码 访问作者blog: http://spaces.msn.com/members/headfaint
dim b64sb as new stringbuilder
dim sl as integer = str.length – 76
dim i as integer = 0
do while i < sl
b64sb.append(str.substring(i, 76))
b64sb.append(vbnewline)
i += 76
loop
b64sb.append(str.substring(i, str.length – i))
return b64sb.tostring()
end function
end class
public class smtpmail
用smtp协议发送邮件 访问作者blog: http://spaces.msn.com/members/headfaint
public smtpserver as string = ""
public smtpport as integer = 25
public chksmtp as boolean = false
public smtpusername as string = ""
public smtppassword as string = ""
protected shared errcodeht as new hashtable
protected shared rghcodeht as new hashtable
public function send(byval strmailto as string, byval mailfrom as string, byval strmail as string) as boolean
发送邮件 访问作者blog: http://spaces.msn.com/members/headfaint
dim sendbuffer as new arraylist
dim sendbufferstr as string
for each sendbufferstr in strmailto.split(",")
if not sendbufferstr = "" then sendbuffer.add(sendbufferstr)
next
if sendbuffer.count = 0 then return false
return send(sendbuffer, mailfrom, strmail)
end function
public function send(byval strmailto as arraylist, byval mailfrom as string, byval strmail as string) as boolean
发送邮件 访问作者blog: http://spaces.msn.com/members/headfaint
if strmailto.count = 0 then return false
dim tc as tcpclient
try
tc = new tcpclient(smtpserver, smtpport)
catch ex as exception
return false
end try
dim ns as networkstream = tc.getstream()
try 与服务器建立链接 访问作者blog: http://spaces.msn.com/members/headfaint
if rghcodeht(recvresponse(ns).substring(0, 3)) = nothing then return false
catch ex as exception
return false
end try
dim sendbuffer as new arraylist
dim sendbufferstr as string
if chksmtp then 验证用户名密码 访问作者blog: http://spaces.msn.com/members/headfaint
if not smtpauth(ns) then return false
else
sendbufferstr = "helo " & smtpserver & vbnewline
if not dialog(sendbufferstr, ns) then return false
end if
sendbufferstr = "mail from:<" & mailfrom & ">" & vbnewline 发送"mail from" 访问作者blog: http://spaces.msn.com/members/headfaint
if not dialog(sendbufferstr, ns) then return false
sendbuffer.clear()
for each sendbufferstr in strmailto 发送收件人地址 访问作者blog: http://spaces.msn.com/members/headfaint
if not sendbufferstr = "" then sendbuffer.add("rcpt to:<" & sendbufferstr & ">" & vbnewline)
next
if not dialog(sendbuffer, ns) then return false
sendbufferstr = "data" & vbnewline 发送正文和附件 访问作者blog: http://spaces.msn.com/members/headfaint
if not dialog(sendbufferstr, ns) then return false
sendbufferstr = strmail & vbnewline & "." & vbnewline
if not dialog(sendbufferstr, ns) then return false
sendbufferstr += "quit" & vbnewline 完成发送,断开连接 访问作者blog: http://spaces.msn.com/members/headfaint
if not sendcommand(sendbufferstr, ns) then return false
ns.close()
tc.close()
return true
end function
public function send(byval email as mail) as boolean
发送邮件 访问作者blog: http://spaces.msn.com/members/headfaint
dim sendbuffer as new arraylist
dim sendbufferstr as string
for each sendbufferstr in email.recipient
sendbuffer.add(sendbufferstr)
next
for each sendbufferstr in email.recipientcc
sendbuffer.add(sendbufferstr)
next
for each sendbufferstr in email.recipientbcc
sendbuffer.add(sendbufferstr)
next
return send(sendbuffer, email.from, email.tostring())
end function
protected function sendcommand(byval command as string, byref ns as networkstream) as boolean
向smtp服务器发送一行命令 访问作者blog: http://spaces.msn.com/members/headfaint
dim writebuffer() as byte
if command.trim() = "" then return true
writebuffer = encoding.default.getbytes(command)
try
ns.write(writebuffer, 0, writebuffer.length)
catch ex as exception
return false
end try
return true
end function
protected function dialog(byval command as string, byref ns as networkstream) as boolean
向smtp服务器发送一行命令,并等待服务器回应 访问作者blog: http://spaces.msn.com/members/headfaint
if command.trim() = "" then return true
if sendcommand(command, ns) then
dim rr as string = recvresponse(ns)
if rr = "false" then return false
try
dim rrcode as string = rr.substring(0, 3)
if rghcodeht(rrcode) <> nothing then return true
catch ex as exception
return false
end try
return false
else
return false
end if
end function
protected function dialog(byval command as arraylist, byref ns as networkstream) as boolean
向smtp服务器发送一行命令,关等待服务器回应 访问作者blog: http://spaces.msn.com/members/headfaint
dim strcmd as string
for each strcmd in command
if not dialog(strcmd, ns) then return false
next
return true
end function
protected function smtpauth(byref ns as networkstream) as boolean
向服务器发送用户名密码验证信息 访问作者blog: http://spaces.msn.com/members/headfaint
dim sendbuffer as new arraylist
dim sendbufferstr as string
sendbufferstr = "ehlo " & smtpserver & vbnewline 发送ehlo命令 访问作者blog: http://spaces.msn.com/members/headfaint
if sendcommand(sendbufferstr, ns) then
dim i as integer = 0
do
if ns.dataavailable then
dim rr as string = recvresponse(ns)
if rr = "false" then return false
dim rrcode as string = rr.substring(0, 3)
if not rghcodeht(rrcode) = nothing then
if rr.indexof("auth") <> -1 then exit do
else
return false
end if
else
system.threading.thread.sleep(50)
i = i + 1
if i > 60 then
return false
end if
end if
loop
else
return false
end if
sendbuffer.add("auth login" & vbnewline) 发送用户名密码 访问作者blog: http://spaces.msn.com/members/headfaint
sendbuffer.add(base64.encode(smtpusername) & vbnewline)
sendbuffer.add(base64.encode(smtppassword) & vbnewline)
return dialog(sendbuffer, ns)
end function
protected function recvresponse(byref ns as networkstream) as string
从smtp服务器接收一个回应 访问作者blog: http://spaces.msn.com/members/headfaint
dim streamsize as integer
dim returnvalue as string = ""
dim readbuffer(1023) as byte
try
streamsize = ns.read(readbuffer, 0, 1024)
catch ex as exception
return "false"
end try
if streamsize = 0 then
return ""
else
returnvalue = encoding.default.getstring(readbuffer).substring(0, streamsize)
return returnvalue
end if
end function
shared sub new()
添加一个smtp反回信息的对照哈希表 访问作者blog: http://spaces.msn.com/members/headfaint
errcodeht.add("500", "邮箱地址错误")
errcodeht.add("501", "参数格式错误")
errcodeht.add("502", "命令不可实现")
errcodeht.add("503", "服务器需要smtp验证")
errcodeht.add("504", "命令参数不可实现")
errcodeht.add("421", "服务未就绪,关闭传输信道")
errcodeht.add("450", "要求的邮件操作未完成,邮箱不可用(例如,邮箱忙)")
errcodeht.add("550", "要求的邮件操作未完成,邮箱不可用(例如,邮箱未找到,或不可访问)")
errcodeht.add("451", "放弃要求的操作;处理过程中出错")
errcodeht.add("551", "用户非本地,请尝试<forward-path>")
errcodeht.add("452", "系统存储不足,要求的操作未执行")
errcodeht.add("552", "过量的存储分配,要求的操作未执行")
errcodeht.add("553", "邮箱名不可用,要求的操作未执行(例如邮箱格式错误)")
errcodeht.add("432", "需要一个密码转换")
errcodeht.add("534", "认证机制过于简单")
errcodeht.add("538", "当前请求的认证机制需要加密")
errcodeht.add("454", "临时认证失败")
errcodeht.add("530", "需要认证")
rghcodeht.add("220", "服务就绪")
rghcodeht.add("250", "要求的邮件操作完成")
rghcodeht.add("251", "用户非本地,将转发向<forward-path>")
rghcodeht.add("354", "开始邮件输入,以<crlf>.<crlf>结束")
rghcodeht.add("221", "服务关闭传输信道")
rghcodeht.add("334", "服务器响应验证base64字符串")
rghcodeht.add("235", "验证成功")
end sub
end class
end namespace