Ask a Question

Sending EMail Through Script

mesg2anil
Regular Contributor

Sending EMail Through Script

Hi,



Below is the script written to send an email through script, but I'm not getting any email... any suggestions here please?



Sub MainTest

  If SendEmail("anil.yadrami@in4velocity.com", "anil.yadrami@in4velocity.com", "Subject", _

             "Message body", "E:\Anil-Backup\Test Complete\Projects_Old\In4Suite Project\TC_Command_Line.txt") Then

    ' Message was sent

    Log.Message "sent"

  Else

    ' Message was not sent

    Log.Message "not sent"

  End If

End Sub



'This function is called above

Function SendEmail(mFrom, mTo, mSubject, mBody, mAttachment)


  Err.Clear

  On Error Resume Next


  schema = "http://webmail.in4velocity.com/"

  Set mConfig = Sys.OleObject("CDO.Configuration")

'  mConfig.Fields.Item(schema + "sendusing") = 587 ' cdoSendUsingPort  'commented this, dont know what exactly this do

  mConfig.Fields.Item(schema + "smtpserver") = "smtp.gmail.com" ' SMTP server

  mConfig.Fields.Item(schema + "smtpserverport") = 587 ' Port number

  mConfig.Fields.Item(schema + "sendusername") = "anil" ' User name (if needed)

  mConfig.Fields.Item(schema + "sendpassword") = "Pwd123456" ' User password (if needed)

  mConfig.Fields.Update()


  Set mMessage = Sys.OleObject("CDO.Message")

  mMessage.Configuration = mConfig

  mMessage.From = "anil.yadrami@in4velocity.com"

  mMessage.To = "anil.yadrami@in4velocity.com"

  mMessage.Subject = "Automation: Testing"

  mMessage.HTMLBody = "Email Received"


  aqString.ListSeparator = ","

  For i = 0 To aqString.GetListLength(mAttachment) - 1

    mMessage.AddAttachment aqString.GetListItem(mAttachment, i)

  Next


  mMessage.Send


  If Err.Number > 0 Then

    Log.Error "E-mail cannot be sent", Err.Description

    SendEMail = False

  Else

    Log.Message "Message to <" + mTo + "> was successfully sent"

    SendEMail = True

  End If

End Function

12 REPLIES 12
anna_v
Staff

Hi Anil,

Could you please clarify what happens when you are replaying this script? Do any errors occur or is the script replayed successfully?

I also see that there are several log messages in the script code. Does the script post any of these messages to the test log during the execution?



Thanks in advance.

Ann
I’m not a member of the SmartBear Support Team, I’m just helping users in this community.
Any views or opinions expressed do not necessarily represent those of SmartBear Software.
mesg2anil
Regular Contributor

Hi Ann,



The script is not erroring. Script executes successfully and it is posting below messages in the log...



"Message to anil.yadrami@in4velocity.com was successfully sent"

"Sent"



above two lines are shown in the test results, but I dint receive any email. We have gmail which is linked to microsoft outlook, for this do I need to write any additional script?

Do I need to login into my gmail account using IE and keep it open?
anna_v
Staff

Hi,

As far as I can see, this script works with Outlook using the functionality provided by the Collaboration Data Objects library. By the way, CDO is not supported with Outlook 2010. What version of Microsoft Outlook are you currently using?



Thanks in advance.

Ann
I’m not a member of the SmartBear Support Team, I’m just helping users in this community.
Any views or opinions expressed do not necessarily represent those of SmartBear Software.
mesg2anil
Regular Contributor

Hi Ann,



Thank you for replying...

I'm using Microsoft Outlook 2010 Ver 14.0.6112.5000 (32-bit). If everything is fine... why am I not getting the email? :(((



Thanks in advance!!
anna_v
Staff

Hi Anil,

Perhaps, the problem is caused by server settings in your scripts.

Please refer to the TestComplete FAQ. This topic contains a sample script with the actual GMail server settings. Try using this script.



I hope this information helps you.

Ann
I’m not a member of the SmartBear Support Team, I’m just helping users in this community.
Any views or opinions expressed do not necessarily represent those of SmartBear Software.
mesg2anil
Regular Contributor

Hi Ann,



As per the example, I modified the code as below, it says email is sent successfully but I dont see any email in the inbox nor in sent items 😞

Where am I going wrong?




Sub Test()


Test()

Call SendEmail("anil.yadrami@in4velocity.com", "anil.yadrami@in4velocity.com", "Test", "TestMessage", "")



End Sub



 



Function SendEmail(mFrom, mTo, mSubject, mBody, mAttachment)


SendEmail(mFrom, mTo, mSubject, mBody, mAttachment)

Dim smtpServer, portNumber, userName, userPassword


Dim useAutentification, useSSL, connectionTimeout


Dim schema, mConfig, mMessage


 


smtpServer = "smtp.gmail.com"


smtpPort = 587


userLogin = "anil.yadrami@in4velocity.com" ' e.g. abc@gmail.com


userPassword = "Pwd123456"


autentificationType = 1 ' cdoBasic


connectionTimeout = 30


' Required by GMail


useSSL = True


 


Err.Clear()


On Error Resume Next


 


schema = https://www.google.com/a/in4velocity.com/



Set mConfig = Sys.OleObject("CDO.Configuration")


mConfig.Fields.Item(schema & "sendusing") = 2 ' cdoSendUsingPort


mConfig.Fields.Item(schema & "smtpserver") = smtpServer


mConfig.Fields.Item(schema & "smtpserverport") = smtpPort


mConfig.Fields.Item(schema & "sendusername") = userLogin


mConfig.Fields.Item(schema & "sendpassword") = userPassword


mConfig.Fields.Item(schema & "smtpauthenticate") = autentificationType


mConfig.Fields.Item(schema & "smtpusessl") = useSSL


mConfig.Fields.Item(schema & "smtpconnectiontimeout") = connectionTimeout


mConfig.Fields.Update()


 


Set mMessage = Sys.OleObject("CDO.Message")


Set mMessage.Configuration = mConfig


mMessage.From = mFrom


mMessage.To = mTo


mMessage.Subject = mSubject


mMessage.HTMLBody = mBody


 


If 0 < Len("E:\Anil-Backup\Test Complete\Projects_Old\In4Suite Project\TC_Command_Line.txt") Then


mMessage.AddAttachment("E:\Anil-Backup\Test Complete\Projects_Old\In4Suite Project\TC_Command_Line.txt")


End If


 


mMessage.Send()


 


If Err.Number > 0 Then


Call Log.Error("E-mail cannot be sent", Err.Description)


SendEMail = False


Err.Clear()


Else


Log.Message("Message to <" & mTo & "> was successfully sent")


SendEMail = True


End If

End Function


anna_v
Staff

Hi,

Please try using
schema = "http://schemas.microsoft.com/cdo/configuration/"
instead of your custom scheme (schema = https://www.google.com/a/in4velocity.com/).





Let me know your results.

Thanks in advance.

Ann
I’m not a member of the SmartBear Support Team, I’m just helping users in this community.
Any views or opinions expressed do not necessarily represent those of SmartBear Software.
mesg2anil
Regular Contributor

Hi Ann,



I'm really sorry for troubling you with this issue...

I changed the schema settings as you said, I'm not getting any email...

After changing the schema in the below code, I'm getting "Not Sent" in the test result, below is the code...




Sub MainTest


MainTest

If SendEmaill("anil.yadrami@in4velocity.com", "anil.yadrami@in4velocity.com", "Subject", _


"Message body", "") Then


' Message was sent


Log.Message "sent"


Else


' Message was not sent


Log.Message "not sent" 'I'm getting this message in the log test results this time


End If



End Sub




'================================================================================================

Function SendEmaill(mFrom, mTo, mSubject, mBody, mAttachment)




Err.Clear


On Error Resume Next


schema = "http://schemas.microsoft.com/cdo/configuration/"


Set mConfig = Sys.OleObject("CDO.Configuration")


mConfig.Fields.Item(schema + "sendusing") = 587 ' cdoSendUsingPort

 



mConfig.Fields.Item(schema + "smtpserver") = "smtp.gmail.com" ' SMTP server

 



mConfig.Fields.Item(schema + "smtpserverport") = 80 ' Port number

 



mConfig.Fields.Item(schema + "sendusername") = "anil.yadrami" ' User name (if needed)

 



mConfig.Fields.Item(schema + "sendpassword") = "adsf" ' User password (if needed)

 



mConfig.Fields.Update()


Set mMessage = Sys.OleObject("CDO.Message")


mMessage.Configuration = mConfig


mMessage.From = "anil.yadrami@in4velocity.com"


mMessage.To = "anil.yadrami@in4velocity.com"


mMessage.Subject = "Automation: Testing"


mMessage.HTMLBody = "Email Received"


aqString.ListSeparator = ","


For i = 0 To aqString.GetListLength(mAttachment) - 1


mMessage.AddAttachment aqString.GetListItem(mAttachment, i)


Next


mMessage.Send


If Err.Number > 0 Then


Log.Error "E-mail cannot be sent", Err.Description


SendEMail = False


 


Else


Log.Message "Message to <" + mTo + "> was successfully sent"


SendEMail = True


 


End If End Function

anna_v
Staff

Hi,


I guess that the test log must contain the error description that is posted by the following code line:

Log.Error "E-mail cannot be sent", Err.Description
Could you please select the corresponding message in the Test Log and find out the description in the Additional Info panel and post it here?


Also, I want to notice that this script is working on my computer with my certain parameters. I guess that you specify incorrect parameters in your script. Could you ask your system administrator for the name of your SMTP server and the port number that you can use to send your mail? I suppose that the problem is in these parameters.


Let me know your results




Ann

I’m not a member of the SmartBear Support Team, I’m just helping users in this community.
Any views or opinions expressed do not necessarily represent those of SmartBear Software.
cancel
Showing results for 
Search instead for 
Did you mean: