Consolidated ASP Classic MVC framework from best components
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

156 lignes
4.9KB

  1. <%
  2. ' Class: CDOEmail
  3. ' Handles email creation and sending via CDO in VBScript
  4. Class CDOEmail_Class
  5. ' Public properties
  6. Public From
  7. Public Subject
  8. Public Body
  9. Public IsBodyHTML
  10. Public SMTPServer
  11. Public SMTPPort
  12. Public SMTPUsername
  13. Public SMTPPassword
  14. Public SMTPUseSSL
  15. ' Private members
  16. Private cfg
  17. Private msg
  18. Private dictRecipients
  19. Private arrAttachments
  20. ' Initialize default values and objects
  21. Private Sub Class_Initialize()
  22. ' Create CDO configuration and message objects
  23. Set cfg = Server.CreateObject("CDO.Configuration")
  24. Set msg = Server.CreateObject("CDO.Message")
  25. ' Default SMTP settings
  26. SMTPServer = "localhost"
  27. SMTPPort = 25
  28. SMTPUsername = ""
  29. SMTPPassword = ""
  30. SMTPUseSSL = False
  31. ' Initialize recipient collections
  32. Set dictRecipients = Server.CreateObject("Scripting.Dictionary")
  33. dictRecipients.Add "TO", Array()
  34. dictRecipients.Add "CC", Array()
  35. dictRecipients.Add "BCC", Array()
  36. ' Initialize attachments dynamic array
  37. ReDim arrAttachments(-1)
  38. ' Default message settings
  39. From = ""
  40. Subject = ""
  41. Body = ""
  42. IsBodyHTML = False
  43. End Sub
  44. ' Add a recipient by type: "To", "Cc", or "Bcc"
  45. Public Sub AddRecipient(recipientType, address)
  46. Dim key, tmp
  47. key = UCase(recipientType)
  48. If Not dictRecipients.Exists(key) Then
  49. Err.Raise vbObjectError + 1000, "CDOEmail", "Invalid recipient type: " & recipientType
  50. End If
  51. tmp = dictRecipients(key)
  52. If UBound(tmp) < LBound(tmp) Then
  53. ReDim tmp(0)
  54. Else
  55. ReDim Preserve tmp(UBound(tmp) + 1)
  56. End If
  57. tmp(UBound(tmp)) = address
  58. dictRecipients(key) = tmp
  59. End Sub
  60. ' Add a file attachment
  61. Public Sub AddAttachment(filePath)
  62. If UBound(arrAttachments) < LBound(arrAttachments) Then
  63. ReDim arrAttachments(0)
  64. Else
  65. ReDim Preserve arrAttachments(UBound(arrAttachments) + 1)
  66. End If
  67. arrAttachments(UBound(arrAttachments)) = filePath
  68. End Sub
  69. ' Send the email, returning True on success, False on failure
  70. Public Function Send()
  71. ' Configure SMTP settings
  72. With cfg.Fields
  73. .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 ' cdoSendUsingPort
  74. .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
  75. .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPPort
  76. .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = Choice(Len(SMTPUsername) > 0, 1, 0) ' cdoBasic or cdoAnonymous
  77. .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTPUsername
  78. .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SMTPPassword
  79. .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = SMTPUseSSL
  80. .Update
  81. End With
  82. ' Apply configuration to message
  83. Set msg.Configuration = cfg
  84. ' Populate message fields
  85. msg.From = From
  86. msg.Subject = Subject
  87. If dictRecipients.Exists("TO") And UBound(dictRecipients("TO")) >= LBound(dictRecipients("TO")) Then
  88. msg.To = Join(dictRecipients("TO"), ";")
  89. End If
  90. If dictRecipients.Exists("CC") And UBound(dictRecipients("CC")) >= LBound(dictRecipients("CC")) Then
  91. msg.CC = Join(dictRecipients("CC"), ";")
  92. End If
  93. If dictRecipients.Exists("BCC") And UBound(dictRecipients("BCC")) >= LBound(dictRecipients("BCC")) Then
  94. msg.BCC = Join(dictRecipients("BCC"), ";")
  95. End If
  96. If IsBodyHTML Then
  97. msg.HTMLBody = Body
  98. Else
  99. msg.TextBody = Body
  100. End If
  101. ' Add attachments if any
  102. Dim i
  103. For i = LBound(arrAttachments) To UBound(arrAttachments)
  104. msg.AddAttachment arrAttachments(i)
  105. Next
  106. ' Send and handle errors
  107. On Error Resume Next
  108. msg.Send
  109. errNum = Err.Number
  110. errDesc = Err.Description
  111. On Error Goto 0
  112. If Err.Number <> 0 Then
  113. Response.Write "CDO Error #" & errNum & ": " & errDesc & "<br/>"
  114. Send = False
  115. Err.Clear
  116. Else
  117. Send = True
  118. End If
  119. End Function
  120. ' Clean up objects
  121. Private Sub Class_Terminate()
  122. On Error Resume Next
  123. Set msg = Nothing
  124. Set cfg = Nothing
  125. Set dictRecipients = Nothing
  126. Erase arrAttachments
  127. End Sub
  128. End Class
  129. dim CDOEmail_Class__Singleton
  130. Function CDOEmail()
  131. if IsEmpty(CDOEmail_Class__Singleton) then
  132. set CDOEmail_Class__Singleton = new CDOEmail_Class
  133. end if
  134. set CDOEmail = CDOEmail_Class__Singleton
  135. End Function
  136. %>

Powered by TurnKey Linux.