Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

351 lines
11 KiB

  1. VERSION 5.00
  2. Begin VB.Form Main
  3. Caption = "ReplyAll"
  4. ClientHeight = 5325
  5. ClientLeft = 60
  6. ClientTop = 345
  7. ClientWidth = 7275
  8. LinkTopic = "Form1"
  9. ScaleHeight = 5325
  10. ScaleWidth = 7275
  11. StartUpPosition = 3 'Windows Default
  12. Begin VB.TextBox tbOutput
  13. Height = 3855
  14. Left = 240
  15. MultiLine = -1 'True
  16. ScrollBars = 2 'Vertical
  17. TabIndex = 3
  18. Top = 1320
  19. Width = 6855
  20. End
  21. Begin VB.Timer timerPoll
  22. Enabled = 0 'False
  23. Interval = 50
  24. Left = 3120
  25. Top = 120
  26. End
  27. Begin VB.CommandButton btnStart
  28. Caption = "&Start"
  29. Height = 495
  30. Left = 5280
  31. TabIndex = 2
  32. Top = 240
  33. Width = 1215
  34. End
  35. Begin VB.TextBox tbQueueLabel
  36. Height = 285
  37. Left = 1560
  38. TabIndex = 0
  39. Top = 240
  40. Width = 1215
  41. End
  42. Begin VB.Label lblQueueLabel
  43. Caption = "Input Queue Label:"
  44. Height = 255
  45. Left = 120
  46. TabIndex = 1
  47. Top = 240
  48. Width = 1455
  49. End
  50. End
  51. Attribute VB_Name = "Main"
  52. Attribute VB_GlobalNameSpace = False
  53. Attribute VB_Creatable = False
  54. Attribute VB_PredeclaredId = True
  55. Attribute VB_Exposed = False
  56. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  57. ' This is a part of the Microsoft Source Code Samples.
  58. ' Copyright (C) 1999 Microsoft Corporation.
  59. ' All rights reserved.
  60. ' This source code is only intended as a supplement to
  61. ' Microsoft Development Tools and/or WinHelp documentation.
  62. ' See these sources for detailed information regarding the
  63. ' Microsoft samples programs.
  64. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  65. Option Explicit
  66. Dim g_qInput As MSMQQueue
  67. Private Function FFindCreateQueue(strQueueLabel As String, qinfo As MSMQQueueInfo) _
  68. As Boolean
  69. Dim query As MSMQQuery
  70. Dim qinfos As MSMQQueueInfos
  71. Set query = New MSMQQuery
  72. '
  73. 'look for queue
  74. '
  75. Set qinfos = query.LookupQueue(Label:=strQueueLabel, _
  76. ServiceTypeGuid:=MSMQMAIL_SERVICE_MAIL)
  77. qinfos.Reset
  78. Set qinfo = qinfos.Next
  79. 'No such queue found
  80. If qinfo Is Nothing Then
  81. If MsgBox("Mail queue " & strQueueLabel & _
  82. " doesn't exist, would you like to create it?", vbYesNo) _
  83. = vbNo Then
  84. FFindCreateQueue = False
  85. Exit Function
  86. End If
  87. 'Create one
  88. Set qinfo = New MSMQQueueInfo
  89. qinfo.PathName = ".\" & strQueueLabel & "_replyall"
  90. qinfo.Label = strQueueLabel
  91. qinfo.ServiceTypeGuid = MSMQMAIL_SERVICE_MAIL
  92. '
  93. 'Error handling should be added here.
  94. '
  95. qinfo.Create
  96. End If
  97. FFindCreateQueue = True
  98. End Function
  99. Private Function FDoStart() As Boolean
  100. Dim qinfo As MSMQQueueInfo
  101. 'reset return value
  102. FDoStart = False
  103. 'check input
  104. If tbQueueLabel.Text = "" Then
  105. Beep
  106. MsgBox "Please fill in the input queue label", vbOKOnly + vbInformation
  107. tbQueueLabel.SetFocus
  108. Exit Function
  109. End If
  110. 'find or create the queue
  111. If Not FFindCreateQueue(tbQueueLabel.Text, qinfo) Then
  112. tbQueueLabel.SetFocus
  113. Exit Function
  114. End If
  115. 'open the input queue
  116. Set g_qInput = qinfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
  117. 'enable processing of the queue in the background
  118. timerPoll.Interval = 50 'check for messages every 50 msec
  119. timerPoll.Enabled = True
  120. 'return success
  121. FDoStart = True
  122. End Function
  123. Private Sub DoStop()
  124. 'disable processing of the queue in the background
  125. timerPoll.Enabled = False
  126. 'close the input queue
  127. g_qInput.Close
  128. End Sub
  129. Private Sub btnStart_Click()
  130. btnStart.Enabled = False
  131. If btnStart.Caption = "&Start" Then
  132. 'it is start, start processing & change the button to stop
  133. If FDoStart() Then
  134. btnStart.Caption = "S&top"
  135. End If
  136. Else 'it is stop, stop processing & change the button to start
  137. DoStop
  138. btnStart.Caption = "&Start"
  139. End If
  140. btnStart.Enabled = True
  141. End Sub
  142. Private Sub Form_Load()
  143. 'disable processing of the queue in the background
  144. timerPoll.Enabled = False
  145. 'fail and exit if local computer is DS disabled
  146. If Not IsDsEnabled Then
  147. MsgBox "DS disabled mode not supported.", vbOKOnly + vbInformation, "Reply All"
  148. End
  149. End If
  150. End Sub
  151. Function CreateReplyAllEmail(emailIn As MSMQMailEMail) As MSMQMailEMail
  152. Dim emailOut As MSMQMailEMail
  153. Dim strOurAddress As String
  154. 'create email out
  155. Set emailOut = New MSMQMailEMail
  156. 'set date
  157. emailOut.SubmissionTime = Now
  158. 'set subject as reply to original subject
  159. If Left$(emailIn.Subject, 3) <> "RE:" Then
  160. emailOut.Subject = "RE: " & emailIn.Subject
  161. Else
  162. emailOut.Subject = emailIn.Subject
  163. End If
  164. 'set sender properties as ours
  165. emailOut.Sender.Name = "ReplyAll Sample"
  166. 'our address is our input queue label
  167. strOurAddress = g_qInput.QueueInfo.Label
  168. emailOut.Sender.Address = strOurAddress
  169. 'set the recipients list
  170. 'add the sender of the original mail as a primary recipient
  171. emailOut.Recipients.Add emailIn.Sender.Name, emailIn.Sender.Address, _
  172. MSMQMAIL_RECIPIENT_TO
  173. 'add other recipients from original mail, excluding ourselves
  174. Dim recipientIn As MSMQMailRecipient
  175. For Each recipientIn In emailIn.Recipients
  176. 'check recipient's address. if its not us, add it to the recipient list
  177. If recipientIn.Address <> strOurAddress Then
  178. emailOut.Recipients.Add recipientIn.Name, recipientIn.Address, recipientIn.RecipientType
  179. End If
  180. Next recipientIn
  181. 'switch on email type
  182. If emailIn.ContentType = MSMQMAIL_EMAIL_FORM Then
  183. 'it is a form. return the same form, just fill in the reply field
  184. 'set type to form
  185. emailOut.ContentType = MSMQMAIL_EMAIL_FORM
  186. 'set form name from original form
  187. emailOut.FormData.Name = emailIn.FormData.Name
  188. 'set fields from original form
  189. Dim fieldIn As MSMQMailFormField
  190. For Each fieldIn In emailIn.FormData.FormFields
  191. 'skip the reply field if any, we will add one anyway
  192. If fieldIn.Name <> "reply" Then
  193. 'add original form field
  194. emailOut.FormData.FormFields.Add fieldIn.Name, fieldIn.Value
  195. End If
  196. Next fieldIn
  197. 'Add the reply field
  198. emailOut.FormData.FormFields.Add "reply", _
  199. "This is a reply field from the ReplyAll sample"
  200. ElseIf emailIn.ContentType = MSMQMAIL_EMAIL_TEXTMESSAGE Then
  201. 'it is a text message. return reply text plus the original message text
  202. 'set type to text message
  203. emailOut.ContentType = MSMQMAIL_EMAIL_TEXTMESSAGE
  204. 'return a reply text before the original message text
  205. Dim strReply As String
  206. strReply = "This is a reply text message from the ReplyAll sample" & vbNewLine
  207. strReply = strReply & "----------------------------------------------------------" & vbNewLine
  208. 'add the original message text
  209. strReply = strReply & emailIn.TextMessageData.Text
  210. emailOut.TextMessageData.Text = strReply
  211. End If
  212. 'return reply-all email
  213. Set CreateReplyAllEmail = emailOut
  214. Set emailOut = Nothing
  215. End Function
  216. Private Sub SendMsgToQueueLabel(msgOut As MSMQMessage, strQueueLabel As String)
  217. Dim query As MSMQQuery
  218. Dim qinfos As MSMQQueueInfos
  219. Dim qinfo As MSMQQueueInfo
  220. Dim qDestination As MSMQQueue
  221. Set query = New MSMQQuery
  222. Set qinfos = query.LookupQueue(Label:=strQueueLabel, _
  223. ServiceTypeGuid:=MSMQMAIL_SERVICE_MAIL)
  224. qinfos.Reset
  225. Set qinfo = qinfos.Next
  226. If qinfo Is Nothing Then
  227. MsgBox "Destination mail queue " & strQueueLabel & " doesn't exist. Can't send to this queue", vbExclamation
  228. Exit Sub
  229. End If
  230. Set qDestination = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
  231. msgOut.Send qDestination
  232. End Sub
  233. Private Sub OutputEmail(email As MSMQMailEMail)
  234. Dim strDump As String
  235. strDump = "Received the following email:" & vbNewLine
  236. strDump = strDump & "Subject: " & email.Subject & vbNewLine
  237. strDump = strDump & "Sender: " & email.Sender.Name & " " & email.Sender.Address & vbNewLine
  238. strDump = strDump & "Sent on: " & email.SubmissionTime & vbNewLine
  239. strDump = strDump & "Recipients are:" & vbNewLine
  240. 'Dump the recipient list
  241. Dim recipient As MSMQMailRecipient
  242. For Each recipient In email.Recipients
  243. strDump = strDump & recipient.Name & " " & recipient.Address & " " & recipient.RecipientType & vbNewLine
  244. Next recipient
  245. 'Check email type
  246. If email.ContentType = MSMQMAIL_EMAIL_FORM Then
  247. 'Dump form related properties
  248. strDump = strDump & "Form name: " & email.FormData.Name & vbNewLine
  249. strDump = strDump & "Form fields are: " & vbNewLine
  250. 'Dump the form field list
  251. Dim formfield As MSMQMailFormField
  252. For Each formfield In email.FormData.FormFields
  253. strDump = strDump & formfield.Name & " " & formfield.Value & vbNewLine
  254. Next formfield
  255. ElseIf email.ContentType = MSMQMAIL_EMAIL_TEXTMESSAGE Then
  256. 'Dump text related properties
  257. strDump = strDump & "Message Text is:" & vbNewLine
  258. strDump = strDump & email.TextMessageData.Text & vbNewLine
  259. End If
  260. strDump = strDump & "-------------------------------------" & vbNewLine
  261. tbOutput.Text = tbOutput.Text & strDump
  262. End Sub
  263. Private Sub DoProcessMsg(msgIn As MSMQMessage)
  264. Dim emailIn As MSMQMailEMail
  265. Dim emailOut As MSMQMailEMail
  266. Dim msgOut As MSMQMessage
  267. 'create new email object for original message
  268. Set emailIn = New MSMQMailEMail
  269. 'parse the body of the MSMQ message and set email object properties
  270. emailIn.ParseBody msgIn.Body
  271. 'dump the email to the output text box
  272. OutputEmail emailIn
  273. 'create reply-all email
  274. Set emailOut = CreateReplyAllEmail(emailIn)
  275. 'create new MSMQ message
  276. Set msgOut = New MSMQMessage
  277. 'create the body of the MSMQ message from the reply-all email
  278. msgOut.Body = emailOut.ComposeBody()
  279. 'set other MSMQ message properties
  280. msgOut.Delivery = MQMSG_DELIVERY_RECOVERABLE
  281. 'send the MSMQ message to each of the destination queues
  282. Dim varQueueLabel As Variant
  283. For Each varQueueLabel In emailOut.DestinationQueueLabels
  284. SendMsgToQueueLabel msgOut, CStr(varQueueLabel)
  285. Next varQueueLabel
  286. End Sub
  287. Private Sub timerPoll_Timer()
  288. Dim msgIn As MSMQMessage
  289. Set msgIn = New MSMQMessage
  290. 'get first message in the queue, if any
  291. Set msgIn = g_qInput.Receive(ReceiveTimeout:=0)
  292. While Not msgIn Is Nothing
  293. 'process the message
  294. DoProcessMsg msgIn
  295. Set msgIn = Nothing
  296. 'get next message in the queue, if any
  297. Set msgIn = g_qInput.Receive(ReceiveTimeout:=0)
  298. Wend
  299. End Sub