如何從VB/ASP中透過外部SMTP發信?
轉貼自 http://blog.roodo.com/phanix/archives/428744.html
因為工作用到,雖然以前自己也寫過,但年代久遠...忘光光XD
現在再把它挖出來...轉到自己部落格好了..以後外面的都查不到時可以查自己的:P
====Document Start====
如何從 asp 中透過 gmail smtp 發信 / How to send mail in asp via gmail smtp
最近因為有需要從 asp 中透過 gmail smtp 發信;
不幸的是在網路上似乎只能找到 for php, jsp, .net 的 sample code,受不了乾脆來寫一個 =.=
'Create CDO.Message object instance
Set myMail = CreateObject("CDO.Message")
'============================================================
' 使用外部 SMTP
'============================================================
'設定是否使用外部 SMTP
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 '1 代表使用 local smtp, 2 為外部 smtp
'SMTP Server domain name
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
'Server port, gmail use ssl smtp authentication, port number is 465
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
'Authentication method, ssl or not, Username and password for the SMTP Server
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic 基本驗證
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
myMail.Configuration.Fields.Update
'============================================================
' End of 使用外部 SMTP
'============================================================
myMail.Subject = strSubject
myMail.From = strEmailFrom
myMail.To = strEmailTo
myMail.TextBody = strBody
myMail.Send
set myMail=nothing
====End====
下面是我自己寫的VB6的Function
====My Code====
'自訂寄送Mail函數 Sam+
'(寄件者,收件者,副件收件者,密件收件者,主旨,內容)
Public Function SendMail_SMTP(lcFrom As String, lcTo As String, lcCC As String, lcBCC As String, lcSubject As String, lcBody As String) As Boolean
Dim loMsg As Object
Set loMsg = CreateObject("cdo.message")
On Error GoTo SendMail_SMTP_Error_EH
'開一個Form顯示發送Mail的訊息. Sam+
SendMailForm.SendMailLabel.ForeColor = vbRed
SendMailForm.SendMailLabel = "○發送中‧‧‧"
SendMailForm.show
loMsg.bodypart.Charset = "big5"
loMsg.From = Trim(lcFrom)
loMsg.To = Trim(lcTo)
If Trim(lcCC) <> "" Then
loMsg.cc = Trim(lcCC)
End If
If Trim(lcBCC) <> "" Then
loMsg.Bcc = Trim(lcBCC)
End If
loMsg.Subject = lcSubject
'TextBody是Text格式,HTMLBody是網頁格式的內容
'loMsg.TextBody = "Text格式內容"
If Trim(lcBody) <> "" Then
loMsg.HTMLBody = Trim(lcBody)
Else
loMsg.HTMLBody = " "
End If
SendMailForm.SendMailLabel.ForeColor = vbBlue
SendMailForm.SendMailLabel = "●發送中‧‧‧‧"
'loMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "abcd" '寄件認證帳號
'loMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "1234" '寄件認證密碼
'sendusing =1 使用local的SMTP =2使用外部的SMTP
loMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
loMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "SMTP.XXXX.com.tw" ' 請改成你要用的SMTP Server
loMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'SMTP 作業使用的通訊埠
loMsg.Configuration.Fields.Update
'DNSOptions=4是要求回傳發送狀況..會送比較久(等回傳).
'DNSOptions=2是傳送失敗時回傳發送狀況.
loMsg.DSNOptions = 2
loMsg.Send
SendMailForm.SendMailLabel.ForeColor = vbRed
SendMailForm.SendMailLabel = "●發送完成!"
SendMailForm.ExitFrom = True
SendMail_SMTP = True
Exit Function
SendMail_SMTP_Error_EH:
SendMailForm.SendMailLabel = "●發送失敗!" & vbCrLf & Err.Description
MsgBox (Err.Description)
SendMail_SMTP = False
End Function
====End====
留言