這篇是已前小喵在寫ASP時的文章,今天驚覺沒有收錄在點部落,特別將之收錄進來。
原本使用JMail需要透過許多的設定,然後才能將信件寄出
特別利用Include的方式
將JMail的使用包裝成Function
讓使用JMail寄信變得輕鬆容易
JMail是一個免費的寄信元件,可以讓ASP依照程式的需求寄信出去
不過由於需要做許多的設定,讓使用起來不是那麼的方便
於是特別利用Include, Function的方式
將JMail的使用包裝成只需傳遞幾個參數,就能夠輕鬆的使用
首先...先來看一下Include的檔案內容
檔案名稱EMail.asp放在INC/中
INC/EMail.asp
<%
Function SendMail(Subject,Sender,SenderName,Recipient,bodytxt,Attachment,RecipientCC,RecipientBCC)
'************************************************************
' 撰寫者:TOPCAT 撰寫日期:2001/11/29
' 功能:用來傳送電子郵件的INCLUDE
' 參數說明:
' Subject : string 傳入信件的主旨
' Sender : string 傳入寄件者的電子郵件
' SenderName : string 傳入寄件者的名稱
' Recipient : Array 傳入收件者的電子郵件
' bodytext : string 傳入信件內容
' Attachment : Array 傳入附加檔案的絕對路徑
' RecipientCC : Array 傳入副本的電子郵件
' RecipientBCC : Array 傳入隱藏副本的電子郵件
' 程式說明:
'*************************************************************
Set JMail=Server.CreateObject("JMail.SMTPMail")
JMail.Subject = Subject
'JMail.ContentType = "txt/html"
JMail.Charset = "big5"
'JMAIL.ContentTransferEncoding = "base64"
JMAIL.Encoding = "base64"
JMAIL.ISOEncodeHeaders = false
JMail.Sender = Sender
JMail.SenderName = SenderName
Dim x
For x = 1 to UBound(Recipient)
JMail.AddRecipient(Recipient(x))
Next
If IsArray(RecipientCC) Then
For k = 1 to UBound(RecipientCC)
JMail.AddRecipientCC(RecipientCC(k))
Next
End If
If IsArray(RecipientBCC) Then
For l = 1 to UBound(RecipientBCC)
JMail.AddRecipientBCC(RecipientBCC(l))
Next
End If
If IsArray(Attachment) Then
For j = 1 to UBound(Attachment)
JMail.AddAttachment(Attachment(j))
Next
End If
JMail.ServerAddress = "msa.hinet.net" '請在此變更您的送件郵件伺服器
JMail.Silent = true
JMail.Body = bodytxt
MailRC=JMail.Execute()
set JMail=nothing
SendMail = "Success"
End Function
Function HTMLSendMail(Subject, Sender, SenderName, Recipient, bodytxt, Attachment, RecipientCC, RecipientBCC, HTMLBody)
'************************************************************
' 撰寫者:TOPCAT 撰寫日期:2005/03/29
' 功能:用來傳送電子郵件的INCLUDE
' 參數說明:
' Subject : string 傳入信件的主旨
' Sender : string 傳入寄件者的電子郵件
' SenderName : string 傳入寄件者的名稱
' Recipient : Array 傳入收件者的電子郵件
' HTMLBody : string 傳入信件內容(HTML)
' Attachment : Array 傳入附加檔案的絕對路徑
' RecipientCC : Array 傳入副本的電子郵件
' RecipientBCC: Array 傳入隱藏副本的電子郵件
' 程式說明:
'*************************************************************
Set JMail = Server.CreateObject("JMail.Message")
JMail.Subject = Subject
JMail.Charset = "big5"
JMail.Encoding = "base64"
JMail.ISOEncodeHeaders = False
JMail.ReplyTo = Sender
JMail.From = Sender
JMail.FromName = SenderName
Dim x
For x = 1 To UBound(Recipient)
JMail.AddRecipient Recipient(x)
Next
If IsArray(RecipientCC) Then
For k = 1 To UBound(RecipientCC)
JMail.AddRecipientCC RecipientCC(k)
Next
End If
If IsArray(RecipientBCC) Then
For l = 1 To UBound(RecipientBCC)
JMail.AddRecipientBCC RecipientBCC(l)
Next
End If
If IsArray(Attachment) Then
For j = 1 To UBound(Attachment)
JMail.AddAttachment (Attachment(j))
Next
End If
JMail.Silent = True
JMail.Body = bodytxt
HTMLBody = "<style>.dl{font-size:12;}.dh{font-size:14;background-color:blue;color:yellow}"
HTMLBody = HTMLBody & ".dt{font-size:16;background-color:DarkBlue;color:yellow}</style><html "
HTMLBody = HTMLBody & "bgcolor=LightCyan><body><table width=750 border=0 bgcolor=LightCyan><tr>"
HTMLBody = HTMLBody & "<td class=dl>" & HTMLBody & "</td></tr></table></body></html>"
JMail.HTMLBody = HTMLBody
MailRC = JMail.Send("msa.hinet.net") '請在此變更您的送件郵件伺服器
Set JMail = Nothing
HTMLSendMail = MailRC
End Function
%>
接著,就來看一下如何的使用Include中的兩個Function
如果您安裝的是舊版的JMail
那麼請使用第一個Function
寄信的內容只能是一般的【文字方式】
方式如下
<!--#include file="INC/EMail.asp" -->
<%
Subject="信件的主旨"
Sender="abc@msa.hinet.net" '寄信者EMail
SenderName="送件者姓名"
Dim Recipient(3) '假設收件者有3人
Recipient(1) = "111@ms1.hinet.net"
Recipient(2) = "222@ms2.hinet.net"
Recipient(3) = "333@ms3.hinet.net"
bodytxt = ""
Bodytxt = Bodytxt & " 您可以在此 " & chr(13) & Chr(10)
Bodytxt = Bodytxt & " 輸入您的信件內容 " & chr(13) & Chr(10)
Bodytxt = Bodytxt & " " & chr(13) & Chr(10)
Dim Attachment(2) '設定附件,假設有2個
Attachment(1) = "D:\Fiels\Excel1.xls"
Attachment(2) = "D:\Fiels\Excel1.xls"
Dim RecipientCC(2) '設定副本收件人有2人
RecipientCC(1) = "444@ms4.hinet.net"
RecipientCC(2) = "555@ms5.hinet.net"
'RecipientBCC 假設沒有密件副本
rc=SendMail(Subject,Sender,SenderName,Recipient,bodytxt,Attachment,RecipientCC,RecipientBCC)
%>
只需要設定這些參數,就能夠將信寄出
如果安裝的版本是JMail4.2以後的版本
就能夠支援HTML的信件內容
如果您的信需要較多的排版(比如說購物車內容)
就可以使用新的方式,寄出HTML的內容
範例如下
<!--#include file="INC/EMail.asp" -->
<%
Subject="信件的主旨"
Sender="abc@msa.hinet.net" '寄信者EMail
SenderName="送件者姓名"
Dim Recipient(3) '假設收件者有3人
Recipient(1) = "111@ms1.hinet.net"
Recipient(2) = "222@ms2.hinet.net"
Recipient(3) = "333@ms3.hinet.net"
'bodytxt = ""
'Bodytxt = Bodytxt & " 您可以在此 " & chr(13) & Chr(10)
'Bodytxt = Bodytxt & " 輸入您的信件內容 " & chr(13) & Chr(10)
'Bodytxt = Bodytxt & " " & chr(13) & Chr(10)
HTMLBody=""
HTMLBody=HTMLBody & " "
HTMLBody=HTMLBody & " "
HTMLBody=HTMLBody & " <table border=1 bgcolor=pink> "
For y = 1 to 5
HTMLBody=HTMLBody & " <tr> "
For x = 1 to 6
HTMLBody=HTMLBody & " <td class=dl>X:" & x & ",Y:" & y & "</td> "
Next
HTMLBody=HTMLBody & " </tr> "
Next
HTMLBody=HTMLBody & " </table> "
Dim Attachment(2) '設定附件,假設有2個
Attachment(1) = "D:\Fiels\Excel1.xls"
Attachment(2) = "D:\Fiels\Excel1.xls"
Dim RecipientCC(2) '設定副本收件人有2人
RecipientCC(1) = "444@ms4.hinet.net"
RecipientCC(2) = "555@ms5.hinet.net"
'RecipientBCC 假設沒有密件副本
rc=HTMLSendMail(Subject, Sender, SenderName, Recipient, bodytxt, Attachment, RecipientCC, RecipientBCC, HTMLBody)
%>
希望對大家有幫助
以下是簽名:
- 歡迎轉貼本站的文章,不過請在貼文主旨上加上【轉貼】,並在文章中附上本篇的超連結與站名【topcat姍舞之間的極度凝聚】,感恩大家的配合。
- 小喵大部分的文章會以小喵熟悉的語言VB.NET撰寫,如果您需要C#的Code,也許您可以試著用線上的工具進行轉換,這裡提供幾個參考
Microsoft MVP Visual Studio and Development Technologies (2005~2019/6) | topcat Blog:http://www.dotblogs.com.tw/topcat |