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