Agent on incoming email

Hello all, can someone help me out??  I want to create an agent the will run against a mail file when new messages are recieved.  Basically it will grab the message, create a new message (to change the from attributes), insert the original body and subject contents, send the new message, delete the originall message.  Its going to turn the mail box into a forwarding box.  I thought I had it but must be missing something.  Its finally to where it will send the message but I can't seem to get the contents of the original message.  I'm probably missing something stupid.  Can someone help me out??? Thanks!!!


Current code:
Sub Initialize
      On Error Resume Next
      Dim Session As New NotesSession
      Dim SendDoc As NotesDocument
      Dim db As NotesDatabase
      Dim doc As NotesDocument
      
      Dim workspace As New NotesUIWorkspace      
      Dim uidoc As NotesUIdocument
      
      Set uidoc = workspace.currentdocument
      
      Set doc = Session.DocumentContext
      Set db = Session.Currentdatabase
      
      Set SendDoc = New NotesDocument(db)
      
      SendDoc.Form = "Memo"
      SendDoc.SendTo = "my.email@zyx.com"
      SendDoc.Subject = "Msg Forward"
      SendDoc.Principal = "message.forward.zyx.com"
      SendDoc.From = "message.forward.zyx.com"
      SendDoc.Body = uidoc.FieldGetText("Body")            
      Call SendDoc.Send(False)             
End Sub

It sends the message but the body is blank!!  Thanks!!
LVL 3
NARobertsAsked:
Who is Participating?
 
pgloorConnect With a Mentor Commented:
Sorry, I'm in a hurry. Try this (it's far from being perfect):

Dim SendDoc As NotesDocument
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim uidoc As NotesUIdocument
      
On Error Resume Next
Dim Session As New NotesSession
      
Dim workspace As New NotesUIWorkspace    

Set uidoc = workspace.currentdocument
      
Set doc = Session.DocumentContext
Set db = Session.Currentdatabase
      
Set SendDoc = New NotesDocument(db)

' Copy all items from original memo
Call doc.CopyAllItems(SendDoc, True)

' Overwrite some fields with new values      
SendDoc.SendTo = "my.email@zyx.com"
SendDoc.Subject = "Msg Forward"
SendDoc.Principal = "message.forward.zyx.com"
SendDoc.From = "message.forward.zyx.com"

Call SendDoc.Send(False)
0
 
marilyngCommented:
Why no simply change the forwarding address in the NAB? Once you add an address there, anything sent to  abc.xyz.com will be sent, instead to whatever you specify in the forwarding address.  Since you don't need to keep a copy, then there shouldn't be a problem routing it at the router, right?
0
 
NARobertsAuthor Commented:
Tried it don't work!!  Its complicated.  I need to trick notes, its too smart for its own good!!!!
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
brwwigginsIT ManagerCommented:
Is the trigger after new mail arrives or is it from the action menu?

Just trying to figure out why you have the NotesUIDocument in there since that would mean one you have open on your workspace. If this is running on the server then you can't use that.
0
 
NARobertsAuthor Commented:
It is running on new mail.....however, it does work  guess because of the on error resume next????
0
 
NARobertsAuthor Commented:
I took out the uidoc stuff and its still ok.....thanks for the help guys and the code pgloor!
0
 
SysExpertCommented:
You can also use a Rule to send a copy, and then delete the original.
Wat Version are you using ?

I hope this helps !
0
 
marilyngCommented:
>>Tried it don't work!!  << impossible!!  This is idiot proof.
0
 
marilyngCommented:
Brwiggins is right, it doesn't work because you can't use UI/WS commands in the agent.
0
 
marilyngCommented:
The selected answer is incorrect!!  Because it cannot run on a scheduled agent!!!  
0
 
Sjef BosmanGroupware ConsultantCommented:
As Marilyn says, using uidoc in an agent is impossible. Better to remove those unused lines from the code...
0
 
NARobertsAuthor Commented:
Idiot proof????  Maybe you should know the whole story before you answer!!  

I have message disclaimers setup for all users.  However since I am the administrator I get alot of system messages.  Sometimes important stuff like servers going down.  These messages get forwarded to my cell phone.  Thing is w/ a disclaimer a 2 line text message becomes 5 messages and gets rather costly.  I initially tried to create another box w/ a forwarder.  Lotus in trying to be efficient sees the forward and never delivers to the other box (which doesn't have a disclaimer setup) but just reroutes the mail.  Thus you still get disclaimers!!  Soooo as I said, it doesn't work!!!  The code pgloor gave works perfect (even w/ a little extra code).  Thus he gets credit and I'm happy.  
0
 
marilyngCommented:
What we're saying is that the code won't work in an agent.  It will not send you messages.

Sub Initialize
      Dim session As New NotesSession
      Dim db As NotesDatabase
      Dim doc As NotesDocument
      Dim coll As NotesDocumentCollection
      Dim principal As NotesName
      Dim maildoc As NotesDocument
      Dim rtitem As Variant
      Dim sendToName As String
      Set principal = New NotesName(session.username)
      sendToName = "my.email@zyx.com"
      'First time this runs it will probably send a message for every document in your inbox
      Dim todayDate As New NotesDateTime(Format(Now,"mm/dd/yyyy") + " 12:00:01 AM")
      Call todaydate.AdjustDay (-1)
      
      Set db = session.currentdatabase      
      Set coll = db.unprocesseddocuments
      
      If coll.count>0 Then
            Set doc = coll.getfirstdocument
            While Not doc Is Nothing
                  With doc
                        Set maildoc = New NotesDocument(db)
                        maildoc.sendTo=sendToName
                        Maildoc.from = Principal.abbreviated
                        Maildoc.principal = principal.canonical
                        Maildoc.Subject = "FWD: " + doc.subject(0)
                        Set rtitem = doc.GetFirstItem( "Body" )
                        If ( rtitem.Type = RICHTEXT ) Then
                              'Strip tabs and wrap every 15 characters for cell phone?
                              Maildoc.Body= rtitem.GetFormattedText( True, 15 )
                        Else
                              maildoc.body = rtitem.text
                        End If
                        maildoc.send False
                        Set maildoc = Nothing
                  End With
                  Set doc = coll.getNextDocument(doc)
            Wend            
            Print "Finished processing " + Cstr(coll.count) + " forward documents..."
            Call coll.UpdateAll
      Else
            Print "No documents to process..."
            Call coll.UpdateAll
      End If
      
End Sub


sjef, gotta run.. can you fix the todayDate stuff, so that the agent only processes stuff from the last day, instead of the entire inbox from the past two years?
0
 
NARobertsAuthor Commented:
This is what I'm using. It runs just fine as an agent.  It runs "before new mail arrives".

Sub Initialize
      On Error Resume Next
      Dim Session As New NotesSession
      Dim SendDoc As NotesDocument
      Dim db As NotesDatabase
      Dim doc As NotesDocument
      
      Set doc = Session.DocumentContext
      Set db = Session.Currentdatabase
      
      Set SendDoc = New NotesDocument(db)
      
      SendDoc.Form = "Memo"
      
      ' Copy all items from original memo
      Call doc.CopyAllItems(SendDoc, True)
      
      SendDoc.SendTo = "937xxxxxxx@mobile.mycingular.com"
      SendDoc.Subject = "Msg Forward"
      SendDoc.Principal = "NForward"
      SendDoc.From = "NForward"      
      Call SendDoc.Send(False)             
End Sub
0
 
Sjef BosmanGroupware ConsultantCommented:
Seems okay to me, since you left out all the UI-stuff ;)
I don't really share Marilyn's main concern, that all mail might be reprocessed some day: the agent is triggered "before new mail arrives". She could be right though.

What she also may be after is that you might get only 1 mail when several mails are received at the same time. Can you check that?
0
 
brwwigginsIT ManagerCommented:
only think I would add is make sure you have a valid reply to address contained in the memo in case of errors or bounce backs.
0
 
marilyngCommented:
Call doc.CopyAllItems(SendDoc, True) << this also concerns me.  since you only want text sent, you should specify text.  

Without seeing the code you were using and what the trigger was, I didn't know how you were getting all documents that arrive without the uidoc handle that was included in the original code.


If the trigger is before mail arrives, then it should run on each document. Otherwise you have to use the collection object to run on After new mail arrives, or when documents are copied or pasted.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.