可以生成郵件,可以發送郵件,稍做修改就可以寫成一個com組件,在asp里調用.
以后我會整理成一個完整的.
--------------------------------------------------------------------------------
'-------------------------------------------------
'生成基本郵件格式(包括附件),發送郵件到smtp服務器,
'只能發送到發件人smtp服務器(需驗證),直接投遞功能正在編寫。
'
'聲明:本段代碼中,有一部份借簽了網上一位大俠的c#代碼.由于找不到原文,無法寫出原作者名字
'
'代碼編寫:頭太暈
'qq:2538288
'msn:[email protected]
'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