0

In UFT, I wrote the Email function using VBScript but Email is not triggering from my mailbox. It is trigerring from SBGalert@.co.za, Whereas I mentioned From address as well.

Kindly assist me to fix the email to trigger from my mailbox to stakeholders. I'm using Outlook 15.

Please find my code below to check and assist.

Function Email
'Dim ToAddress
'Dim Subject
'Dim Body
Dim Attachment
Dim oUtlookApp, nAmeSpace, newMail
Dim fso, TextFile

SystemUtil.Run "C:\Program Files\Microsoft Office 15\root\office15\OUTLOOK.EXE" 'This line should be enabled if the Outlook on the desktop is not running
If strMailID_From <> "abc@<abc>.co.za" Then
FromAddress = strMailID_From
Else
FromAddress = "abc@<abc>.co.za"
End If

ToAddress = "abc@<abc>.co.za" ' Message recipient Address

Set oUtlookApp = CreateObject("Outlook.Application")
Set nAmeSpace = oUtlookApp.GetNamespace("MAPI")
Set newMail = oUtlookApp.CreateItem(1)

Subject = "This is a test mail" 'Message Subject you can update

'For Creating File System Object
Set fso = Createobject ("Scripting.FileSystemObject")

'For Opening the Report File in Append Mode
Set TextFile = fso.OpenTextFile(strPath & "results.html" , 8, True)
strExecutionTimeFormate=Replace(strExecutionTime,"-",":")
TextFile.Write "<html><head><title>VAF -S2K - Automation Execution Consolidated Result</title></head>"
TextFile.Write "<body bgcolor=#3D5FA3><table width=100% bgcolor=#FFFFFF border=2 cellpadding=2 cellspacing=1>"
TextFile.Write "<tr bgcolor=#CBD9F4></td><td bgcolor=#CBD9F4 width = 100%><div align=center><font color=purple><font size=5><p><strong>Automation Execution Consolidated Results</strong></p><font color=purple><font size=5><p><strong><font size=3>Execution Date -"&strExecutionDate&"  Execution Time :"&strExecutionTimeFormate&" [HH:MM:SS]</strong></p></td></div></tr>"
TextFile.Write "<body bgcolor=#3D5FA3><table width=100% bgcolor=purple border=1 cellpadding=3 cellspacing=1>"
TextFile.Write "<tr bgcolor=#CBD9F4></td><td bgcolor=#CBD9F4 width = 100%><div align=center><font color=purple><font size=5><font size=3><p><strong>Portfolio : VAF &nbsp || &nbsp Application  : S2K &nbsp || &nbsp Environment : SIT 1 &nbsp || &nbsp Project : Sanity Testing</strong></td></Table>"
TextFile.Write "<body bgcolor=#3D5FA3><table width=100% bgcolor=purple border=1 cellpadding=3 cellspacing=1>"
TextFile.Write "<tr><td bgcolor=white width = 20%><div align=Center><font color=Black><strong>APPLICATION</td><td bgcolor=white width = 45%><div align=Center><font color=Black><strong>TEST CASE NAME</td><td bgcolor=white width = 20%><div align=Center><font color=Black><strong>TEST CASE STATUS</td><td bgcolor=white width = 15%><div align=Center><font color=Black><strong>RUN TIME</td></Table>"
TextFile.Write "<body bgcolor=#3D5FA3><table width=100% bgcolor=purple border=1 cellpadding=3 cellspacing=1>"
'|| &nbsp Cycle : 1 &nbsp
'TextFile.Write "<tr bgcolor=#IBD9F4></td><td bgcolor=#IBD9F4 width = 100%><div align=center><font color=purple><font size=5><p><strong>Automation Execution  Consolidated Results</strong></p><font color=purple><font size=5><p><strong><font size=3>Execution Date -"&strExecutionDate&"  Execution Time :"&strExecutionTimeFormate & "</strong></p><font color=purple><font size=3><p><strong>Portfolio : Self Service Channel     Application  : Internet Banking     Cycle : 1     Project : Regression Testing</strong></td></div></tr>"
TextFile.Close

newMail.Subject = Subject
newMail.Body = Body
newMail.Recipients.Add(ToAddress)
newMail.Attachments.Add(strPath & "results.html") 'You can update attachment file name
newMail.Send
Set nAmeSpace = Nothing
Set oUtlookApp = Nothing

End Function
SAUMARS
  • 73
  • 1
  • 11

1 Answers1

0

If you don't specify the account to send from, it will default to your first mailbox.

I use a dictionary object EmailData to specify the details for the emails I send from UFT. If you want the mail to be sent from a specific mailbox you need to identify the relevant one and set the MailItem's .SendUsingAccount to be the one you want.

If the entry in my incoming dictionary is blank then it defaults to the first account the sending user has access to (usually their personal one). Otherwise it loops through the accounts to find the mailbox it's supposed to use. If it doesn't exist, then it exits and reports that the user doesn't have access to the required mailbox.

Otherwise it sets the address to send from as the mailitem is being constructed.

Let me know if you need any further assistance in this?

If EmailData("SendFrom") = "" Then
    ' default to first email account the user has access to
    LOG_Write "Send From value is not set; defaulting to user's personal mail account to send from."
    iAccount = 1
Else
    LOG_Write "Checking to see if the account to send from is accessible by this user..."
    iAccount = 0
    For iLoop = 1 To oOutlook.Session.Accounts.Count
        If UCase(Trim(oOutlook.Session.Accounts.Item(iLoop))) = UCase(Trim(EmailData("SendFrom"))) Then
            iAccount = iLoop
            Exit For
        End If
    Next
    If iAccount = 0 Then
        sErrorMsg = "Cannot send email from specified account: " & EmailData("SendFrom") & " as this user doesn't appear to have access to it in Outlook!"
        LOG_Write sErrorMsg
        OL_SendMail = False
        Exit Function
    End If
End If

Dim oMailItem : Set oMailItem = oOutlook.CreateItem(olMailItem)
With oMailItem
    Set .SendUsingAccount = oOutlook.Session.Accounts.Item(iAccount)
    .To = EmailData("To")
    .CC = EmailData("CC")
    .BCC = EmailData("BCC")
    .Subject = EmailData("Subject")
    .Body = EmailData("Body")
    sAttachArray = Split(EmailData("AttachmentPaths"), ";")
    For Each sAttachment In sAttachArray
        .Attachments.Add(sAttachment)
    Next
    .Recipients.ResolveAll
    .Display    ' debug mode - uncomment this to see email before it's sent out
End With
Dave
  • 4,328
  • 2
  • 24
  • 33
  • Hi Dave, After my test script ran successful, I'm getting an error in Email function as (Type mismatch: 'EmailData') . And also Email should have to be sent from my mailbox. Even I specified my email ID in place of "SendFrom". It didn't work. Please assist. – SAUMARS Jun 29 '17 at 13:01
  • Did you set it up as a dictionary object? I wasn't suggesting you just copy this section of my code (my function for sending email does a bit more work than is shown), but to show you what you needed to do in order to set a "send from this account" value on a mail item. – Dave Jun 29 '17 at 13:21