Showing posts with label mailNotification. Show all posts
Showing posts with label mailNotification. Show all posts

Sunday, July 19, 2009

Verifying Email notifications

For long time, I was looking a solution to automate the verification of email contents. It required to verify for few user accounts, attachments like Excel, PowerPoint and PDF docs. We have few critical features, which required to verify email contents and attachments. Silktest does not have any built-in functions for this activity. However I have to find a solution. The solution should supports IE6 and IE7 browsers and Windows 2000, Windows XP and Vista OS. I had four choices in mind. They are,

  1. Using a command line utility
  2. Using MS Office Outlook COM interface
  3. Using Collaboration Data Objects (CDO) Interface with Outlook Express
  4. Worst case - Automate any email client with UI

Automate any email client with UI
This option was last one. Because I do not want to automate with User Interface. We can build the solution in couple of weeks effort. The issue is the changes with that client. I have to look on support for different Operating Systems such as Windows XP and Vista.

Using Collaboration Data Objects (CDO) with Outlook Express
In my first company, I have used CDO with Visual Basic. See my previous post - Checking Mail Notification. I got a surprise, while I was searching information for CDO Help. Microsoft has stopped the updates on Outlook Express. Earlier Internet Explorer Intallation was bundled with Outlook Express.

Using MS Office Outlook COM interface
I am familiar with VB script. I have used Outlook COM interface [CreateObject("Outlook.Application")] earlier. The issue is, the particular user account mailboxes should be opened, while executing VB Script. Also I thought the license and installation costs.

Using a command line utility
I have searched many times and found a utility. GetMail is a command line utility and it is working in all windows OS. It is available at GETMAIL for Windows It is a cheapest and best utility to fit for my requirements.

Different Cmdline switches of GetMail
Getmail has different switches for various purposes. You can opt appropriate switches as per your requirement.
 

GetMail v1.33: WinNT console utility to download a mailbox's mail.
syntax:
Getmail -u <user> -pw <password> -s <server> [optional switches (see below)]
Getmail -install [ see install details below ]
Getmail -profile [-delete | "<default>"] [profile1] [profileN] [-q]
Getmail -h [-q]
Getmail -forceextract filename

-install <server> <userid> <password> [<delete> [<xtract> [<try> [<port> [<profile>]]]]]
: set's POP3 server, login, password, whether to delete or not (Yes/No),
whether to automatically extract base64/7bit/UU encoded files or not (Yes/No),
number of tries and port for profile
(<delete> <xtract> <try> and <port> may be replaced by '-').

-u <userid> : Specify userid on remote pop3 host
-pw <password>: Specify password for userid on remote mail host
-s <server> : Specify mail server (pop3) to contact
-nodelete : Do not delete messages after downloading (default)
-delete : Delete messages after downloading
-noxtract : Do not extract base64/7bit/UU files after downloading (default)
-xtract [defname]: Extract base64/7bit/UU encoded files after downloading messages
defname is an optional default filename for the extracted file
-domainstamp : Prepend sender's domain name to extracted attachments
-headersonly : Download only the headers of the message
-port <port> : port to be used on the server, defaults to POP3 (110)
-p <profile> : send with SMTP server, user and port defined in <profile>.
-q : supresses *all* output.
-n <n> : Only get 'n' messages
-m <n> : Only get message # n
-b <n> : Retrieve messages beginning from # n
-plain : Extract text/plain segments too (usually ignored)
-h : displays this help.
-try <n times>: how many attempts to access mail. from '1' to 'INFINITE'
-ti <n> : Set timeout to 'n' seconds.
-forceextract fn: Attempt to extract any encoded messages in 'fn'

Installing GetMail
You need to follow the belows steps to install it. Assume that getmail.exe is stored under D:\tools.
  1. First open a command prompt.
  2. Go to D:\tools folder.
  3. Install it by executing below commmand
    Getmail -install mailserver script1@auto.com wrongpwd
    Here mailserver means the mail Server name or IP address

Verifying GetMail
To verify the GetMail installation, you can use following steps.
  1. Goto folder D:\tools
  2. Execute the following command for any user.
    For example, D:\tools\getmail.exe -u script1 -pw TestPassword -s mailserver
  3. Check any error exists on command prompt.
  4. Check on that folder, whether all the mails are downloaded or not. You can see mails like msg1.txt,msg2.txt,..etc

4Test code - To Verify the GetMail
 

[ ] sMailCommand = "{gsToolsDir}\getmail.exe -u {sUser} -pw {sPass} -s {sServer} {sAdditionalSwitch}"
[ ] Print ("Mail Command: {sMailCommand}")
[+] // if (bDeleteAfterReceive)
[ ] // sMailCommand = "{gsToolsDir}getmail.exe -u {sUser} -pw {sPass} -s {sServer} -delete"
[+] // else
[ ] // sMailCommand = "{gsToolsDir}getmail.exe -u {sUser} -pw {sPass} -s {sServer} -nodelete"
[ ] // Checking any errors on command line output
[+] if( 0 != SYS_Execute( sMailCommand, lsGetmailOut ) )
[ ] ListPrint( lsGetmailOut )
[ ] LogError ( "FAIL. Unable to receive the mail." )
[ ] return FALSE
[ ]

Thursday, April 10, 2008

Checking Mail Notification

Earlier I have written the code to verify the mails from Microsoft Outlook Express. I used to call CDO(Collaboration Data Objects) object methods. I developed a small VB application to delete all the objects from Inbox. I think, now few methods are deprecated.

Code:



'It is possible, by calling OLE objects from CDO.dll
'Mostly it will be in C:\Program Files\Common Files\System\Mapi\1033\NT\
'-------------------------
' Method : CheckMail
' Author : T.Palani Selvam
' Purpose : Check the mails in the MS Outlook Mail Client for the particular subject & its status.
' Parameters: sDatavalue, String - Contains Username, Password of the MS outlook, Subject & Status of mail.
' Returns : Returns Integer data type(True or False.)
'----------------------------
Function CheckMail(sDataValue As String) As Integer
Dim objSession As Variant
Dim objInbox As Variant
Dim objMessages As Variant
Dim objItem As Variant
Dim intIndex As Integer
Dim intCount As Integer
Dim varReceived As Variant
Dim iStatus As Integer 'To get Current status of the message.
Dim sSubject As String 'Subject of the current message.
Dim sUser As String, sPword As String, sSearch As String, sStatus As String 'Information gets from the Data value
Dim iPosition As Integer, iPosition1 As Integer
Dim sTemp As String
Dim iResult As Integer

iResult = False 'Initializing function
'Error Information Handling
On Local Error GoTo Error_Handler1

'****** Parsing information from Datavalue
sTemp = sDatavalue
intCount = 1
While (Instr(sTemp,"::") <>0 )
iPosition = Instr(sTemp,"::")

Select Case intCount
Case 1
sUser = Left$ (sTemp, iPosition - 1)
Case 2
sPWord = Left$(sTemp, iPosition - 1)
Case 3
sSearch = Left$(sTemp, iPosition - 1)
'Case 4
'sStatus = Left$(sTemp, iPosition)
sStatus = Mid$ (sTemp, iPosition + 2)
Case Else
LogWrite("CheckMail: More information is given in data file.", 1)
End Select
intCount = intCount + 1
sTemp = Mid$ (sTemp, iPosition + 2)
Wend
'**** End Parsing Information

Logwrite("Search Mail Status: " + sStatus + " & Subject String: " + sSearch, 1)
'Set objSession = CreateObject("MAPI.Session")
objSession = OleCreateObject("MAPI.Session") ' Create MAPI session

'objSession.Logon strUser, strPword, False, False, 0
OleDispatch(objSession,"Logon",sUser, sPword,False, False, 0) ' Logon using an existing MAPI session

'Set objInbox = objSession.Inbox
objInbox=OleDispatch(objSession,"Inbox") ' Get inbox folder

'Set objMessages = objInbox.Messages
objMessages=OleDispatch(objInbox,"Messages") 'Get all messages


intCount=OleGetProperty(objMessages,"Count")

LogWrite("Total Messages in Outlook: " + str(intCount) , 1)

sStatus = Trim (sStatus)

For intIndex=1 To intCount Step 1
objItem=OleDispatch(objMessages,"Item",intIndex) 'Get Each message

sSubject = OleGetProperty(objItem,"Subject")
iStatus = OleGetProperty(objItem,"Unread")


If (InStr(UCase(sSubject), UCase(sSearch)) <> 0) Then
If (iStatus AND (Instr(lCase(sStatus),"read") > 1)) Then
iResult = True
Exit For
ElseIf ((Not iStatus) AND (Instr(lCase(sStatus),"read") = 1)) Then
iResult = True
Exit For
End If

End If
Next intIndex

' Logoff from MAPI Session
OleDispatch(objSession,"Logoff")
'Set objSession = Nothing
OleReleaseObject(objSession)

If (iResult) Then
LogWrite("Successfully Mail is checked in the Outlook.",2)
Else
LogWrite("Failure to check Mail in the Outlook.",1)
'giTestResult = giTestResult + 10
End IF
CheckMail = iResult

Exit Sub

Error_Handler1:

If InStr(UCase(Error$), "LOGON") <> 0 And InStr(UCase(Error$), "FAIL") <> 0 Then
LogWrite("CheckMail : Username and password of Outlook information are incorrect. ", 1)
LogWrite("CheckMail : MS Outlook Login status is Failure.", 1)
End If


If Not (objSession = NULL) Then
OleReleaseObject(objSession)
End If

'PRINT "[Failed At] " ERF "(" ERL ") : " ERROR$ 'Actual location.
'PRINT "[Trapped At] " ERF(1) "(" ERL(1) ")" 'Trapped location.
LogWrite("[Failed At] " + ERF(0)+ "("+ str(ERL) +") : "+ ERROR$,1) 'Actual location.
Logwrite("[Trapped At] "+ ERF(1)+ "("+ str(ERL(1))+ ")",1) 'Trapped location.
LogWrite("Error Handling: Failure to check Mail in the Outlook.",1)

glCellRow = glCellRow + 1
setResultDetails("Error: " + Error$ ,glCellRow,3) 'Write into Excel

End Function