home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 1999 July / APC47-1.ISO / workshop / win95 / pws / msmqocm.cab / replyall.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-10-06  |  10.5 KB  |  273 lines

  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. Attribute VB_Name = "Main"
  51. Attribute VB_GlobalNameSpace = False
  52. Attribute VB_Creatable = False
  53. Attribute VB_PredeclaredId = True
  54. Attribute VB_Exposed = False
  55. Option Explicit
  56. Dim g_qInput As MSMQQueue
  57. Private Function FFindCreateQueue(strQueueLabel As String, qinfo As MSMQQueueInfo) As Boolean
  58.     Dim query As MSMQQuery
  59.     Dim qinfos As MSMQQueueInfos
  60.     Set query = New MSMQQuery
  61.     Set qinfos = query.LookupQueue(Label:=strQueueLabel, ServiceTypeGuid:=MSMQMAIL_SERVICE_MAIL)
  62.     qinfos.Reset
  63.     Set qinfo = qinfos.Next
  64.     If qinfo Is Nothing Then
  65.         If MsgBox("Mail queue " & strQueueLabel & " doesn't exist, would you like to create it?", vbYesNo) = vbNo Then
  66.             FFindCreateQueue = False
  67.             Exit Function
  68.         End If
  69.         Set qinfo = New MSMQQueueInfo
  70.         qinfo.PathName = ".\" & strQueueLabel & "_replyall"
  71.         qinfo.Label = strQueueLabel
  72.         qinfo.ServiceTypeGuid = MSMQMAIL_SERVICE_MAIL
  73.         qinfo.Create
  74.     End If
  75.     FFindCreateQueue = True
  76. End Function
  77. Private Function FDoStart() As Boolean
  78.     Dim qinfo As MSMQQueueInfo
  79.     'reset return value
  80.     FDoStart = False
  81.     'check input
  82.     If tbQueueLabel.Text = "" Then
  83.         Beep
  84.         MsgBox "Please fill in the input queue label", vbOKOnly + vbInformation
  85.         tbQueueLabel.SetFocus
  86.         Exit Function
  87.     End If
  88.     'find or create the queue
  89.     If Not FFindCreateQueue(tbQueueLabel.Text, qinfo) Then
  90.         tbQueueLabel.SetFocus
  91.         Exit Function
  92.     End If
  93.     'open the input queue
  94.     Set g_qInput = qinfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
  95.     'enable processing of the queue in the background
  96.     timerPoll.Interval = 50 'check for messages every 50 msec
  97.     timerPoll.Enabled = True
  98.     'return success
  99.     FDoStart = True
  100. End Function
  101. Private Sub DoStop()
  102.     'disable processing of the queue in the background
  103.     timerPoll.Enabled = False
  104.     'close the input queue
  105.     g_qInput.Close
  106. End Sub
  107. Private Sub btnStart_Click()
  108.     btnStart.Enabled = False
  109.     If btnStart.Caption = "&Start" Then
  110.         'it is start, start processing & change the button to stop
  111.         If FDoStart() Then
  112.             btnStart.Caption = "S&top"
  113.         End If
  114.     Else 'it is stop, stop processing & change the button to start
  115.         DoStop
  116.         btnStart.Caption = "&Start"
  117.     End If
  118.     btnStart.Enabled = True
  119. End Sub
  120. Private Sub Form_Load()
  121.     'disable processing of the queue in the background
  122.     timerPoll.Enabled = False
  123. End Sub
  124. Function CreateReplyAllEmail(emailIn As MSMQMailEMail) As MSMQMailEMail
  125.     Dim emailOut As MSMQMailEMail
  126.     Dim strOurAddress As String
  127.     'create email out
  128.     Set emailOut = New MSMQMailEMail
  129.     'set date
  130.     emailOut.SubmissionTime = Now
  131.     'set subject as reply to original subject
  132.     If Left$(emailIn.Subject, 3) <> "RE:" Then
  133.         emailOut.Subject = "RE: " & emailIn.Subject
  134.     Else
  135.         emailOut.Subject = emailIn.Subject
  136.     End If
  137.     'set sender properties as ours
  138.     emailOut.Sender.Name = "ReplyAll Sample"
  139.     'our address is our input queue label
  140.     strOurAddress = g_qInput.QueueInfo.Label
  141.     emailOut.Sender.Address = strOurAddress
  142.     'set the recipients list
  143.     'add the sender of the original mail as a primary recipient
  144.     emailOut.Recipients.Add emailIn.Sender.Name, emailIn.Sender.Address, MSMQMAIL_RECIPIENT_TO
  145.     'add other recipients from original mail, excluding ourselves
  146.     Dim recipientIn As MSMQMailRecipient
  147.     For Each recipientIn In emailIn.Recipients
  148.         'check recipient's address. if its not us, add it to the recipient list
  149.         If recipientIn.Address <> strOurAddress Then
  150.             emailOut.Recipients.Add recipientIn.Name, recipientIn.Address, recipientIn.RecipientType
  151.         End If
  152.     Next recipientIn
  153.     'switch on email type
  154.     If emailIn.ContentType = MSMQMAIL_EMAIL_FORM Then
  155.         'it is a form. return the same form, just fill in the reply field
  156.         
  157.         'set type to form
  158.         emailOut.ContentType = MSMQMAIL_EMAIL_FORM
  159.         
  160.         'set form name from original form
  161.         emailOut.FormData.Name = emailIn.FormData.Name
  162.         
  163.         'set fields from original form
  164.         Dim fieldIn As MSMQMailFormField
  165.         For Each fieldIn In emailIn.FormData.FormFields
  166.             'skip the reply field if any, we will add one anyway
  167.             If fieldIn.Name <> "reply" Then
  168.                 'add original form field
  169.                 emailOut.FormData.FormFields.Add fieldIn.Name, fieldIn.Value
  170.             End If
  171.         Next fieldIn
  172.         'Add the reply field
  173.         emailOut.FormData.FormFields.Add "reply", "This is a reply field from the ReplyAll sample"
  174.     ElseIf emailIn.ContentType = MSMQMAIL_EMAIL_TEXTMESSAGE Then
  175.         'it is a text message. return reply text plus the original message text
  176.         
  177.         'set type to text message
  178.         emailOut.ContentType = MSMQMAIL_EMAIL_TEXTMESSAGE
  179.         
  180.         'return a reply text before the original message text
  181.         Dim strReply As String
  182.         strReply = "This is a reply text message from the ReplyAll sample" & vbNewLine
  183.         strReply = strReply & "----------------------------------------------------------" & vbNewLine
  184.         'add the original message text
  185.         strReply = strReply & emailIn.TextMessageData.Text
  186.         emailOut.TextMessageData.Text = strReply
  187.     End If
  188.     'return reply-all email
  189.     Set CreateReplyAllEmail = emailOut
  190.     Set emailOut = Nothing
  191. End Function
  192. Private Sub SendMsgToQueueLabel(msgOut As MSMQMessage, strQueueLabel As String)
  193.     Dim query As MSMQQuery
  194.     Dim qinfos As MSMQQueueInfos
  195.     Dim qinfo As MSMQQueueInfo
  196.     Dim qDestination As MSMQQueue
  197.     Set query = New MSMQQuery
  198.     Set qinfos = query.LookupQueue(Label:=strQueueLabel, ServiceTypeGuid:=MSMQMAIL_SERVICE_MAIL)
  199.     qinfos.Reset
  200.     Set qinfo = qinfos.Next
  201.     If qinfo Is Nothing Then
  202.         MsgBox "Destination mail queue " & strQueueLabel & " doesn't exist. Can't send to this queue", vbExclamation
  203.         Exit Sub
  204.     End If
  205.     Set qDestination = qinfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE)
  206.     msgOut.Send qDestination
  207. End Sub
  208. Private Sub OutputEmail(email As MSMQMailEMail)
  209.     Dim strDump As String
  210.     strDump = "Received the following email:" & vbNewLine
  211.     strDump = strDump & "Subject: " & email.Subject & vbNewLine
  212.     strDump = strDump & "Sender: " & email.Sender.Name & " " & email.Sender.Address & vbNewLine
  213.     strDump = strDump & "Sent on: " & email.SubmissionTime & vbNewLine
  214.     strDump = strDump & "Recipients are:" & vbNewLine
  215.     'Dump the recipient list
  216.     Dim recipient As MSMQMailRecipient
  217.     For Each recipient In email.Recipients
  218.         strDump = strDump & recipient.Name & " " & recipient.Address & " " & recipient.RecipientType & vbNewLine
  219.     Next recipient
  220.     'Check email type
  221.     If email.ContentType = MSMQMAIL_EMAIL_FORM Then
  222.         'Dump form related properties
  223.         strDump = strDump & "Form name: " & email.FormData.Name & vbNewLine
  224.         strDump = strDump & "Form fields are: " & vbNewLine
  225.         'Dump the form field list
  226.         Dim formfield As MSMQMailFormField
  227.         For Each formfield In email.FormData.FormFields
  228.             strDump = strDump & formfield.Name & " " & formfield.Value & vbNewLine
  229.         Next formfield
  230.     ElseIf email.ContentType = MSMQMAIL_EMAIL_TEXTMESSAGE Then
  231.         'Dump text related properties
  232.         strDump = strDump & "Message Text is:" & vbNewLine
  233.         strDump = strDump & email.TextMessageData.Text & vbNewLine
  234.     End If
  235.     strDump = strDump & "-------------------------------------" & vbNewLine
  236.     tbOutput.Text = tbOutput.Text & strDump
  237. End Sub
  238. Private Sub DoProcessMsg(msgIn As MSMQMessage)
  239.     Dim emailIn As MSMQMailEMail
  240.     Dim emailOut As MSMQMailEMail
  241.     Dim msgOut As MSMQMessage
  242.     'create new email object for original message
  243.     Set emailIn = New MSMQMailEMail
  244.     'parse the body of the MSMQ message and set email object properties
  245.     emailIn.ParseBody msgIn.Body
  246.     'dump the email to the output text box
  247.     OutputEmail emailIn
  248.     'create reply-all email
  249.     Set emailOut = CreateReplyAllEmail(emailIn)
  250.     'create new MSMQ message
  251.     Set msgOut = New MSMQMessage
  252.     'create the body of the MSMQ message from the reply-all email
  253.     msgOut.Body = emailOut.ComposeBody()
  254.     'set other MSMQ message properties
  255.     msgOut.Delivery = MQMSG_DELIVERY_RECOVERABLE
  256.     'send the MSMQ message to each of the destination queues
  257.     Dim varQueueLabel As Variant
  258.     For Each varQueueLabel In emailOut.DestinationQueueLabels
  259.         SendMsgToQueueLabel msgOut, CStr(varQueueLabel)
  260.     Next varQueueLabel
  261. End Sub
  262. Private Sub timerPoll_Timer()
  263.     Dim msgIn As MSMQMessage
  264.     'get first message in the queue, if any
  265.     Set msgIn = g_qInput.Receive(ReceiveTimeout:=0)
  266.     While Not (msgIn Is Nothing)
  267.         'process the message
  268.         DoProcessMsg msgIn
  269.         'get next message in the queue, if any
  270.         Set msgIn = g_qInput.Receive(ReceiveTimeout:=0)
  271.     Wend
  272. End Sub
  273.