Forum Discussion
rmanning
15 years agoContributor
Alright, so the comma thing at the begining was causing an issue, and I've managed to move it to the end of the array and now it works (after some other tweaks, like adding the full path to the array as well).
Anyways, like I said, I'm really new to VBScript so this may be a conveluted way to achieve this but it is working so I could care less :)
Here is my working script:
Function SendEmail(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") = 2 ' cdoSendUsingPort
mConfig.Fields.Item(schema + "smtpserver") = "my.smtp.server" ' SMTP server
mConfig.Fields.Item(schema + "smtpserverport") = 25 ' Port number
' mConfig.Fields.Item(schema + "sendusername") = "" ' User name (if needed)
' mConfig.Fields.Item(schema + "sendpassword") = "" ' User password (if needed)
mConfig.Fields.Update()
Set mMessage = Sys.OleObject("CDO.Message")
mMessage.Configuration = mConfig
mMessage.From = mFrom
mMessage.To = mTo
mMessage.Subject = mSubject
mMessage.HTMLBody = mBody
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
Sub MainTest
Set oFolder = aqFileSystem.GetFolderInfo("C:\Documents and Settings\User\My Documents\TestComplete 8 Projects\Automation\Screenshots")
Set colFiles = oFolder.Files
strFileList = ""
aqString.ListSeparator = ","
While colFiles.HasNext
Set f = colFiles.Next
strFileList = aqString.AddListItem("C:/Documents and Settings/User/My Documents/TestComplete 8 Projects/Automation/Screenshots/" + f.Name, strFileList)
Wend
ListFiles = strFileList
If SendEmail("sent-from@email.com", "send-to@email.com", "Automated Testing", _
"This is a test message.", "" + ListFiles + "") Then
' Message was sent
Else
' Message was not sent
End If
End Sub
If someone knows a cleaner way to achieve this, or a way to remove the additional comma at the end of the ListFiles array (which causes a "noname" attachement to be attached along with the entire folder contents) then I'd be interested in hearing your solutions. Otherwise, I'm happy that it at least works and is doing what I require :)
Thanks for all your help! Much appreciated!
Anyways, like I said, I'm really new to VBScript so this may be a conveluted way to achieve this but it is working so I could care less :)
Here is my working script:
Function SendEmail(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") = 2 ' cdoSendUsingPort
mConfig.Fields.Item(schema + "smtpserver") = "my.smtp.server" ' SMTP server
mConfig.Fields.Item(schema + "smtpserverport") = 25 ' Port number
' mConfig.Fields.Item(schema + "sendusername") = "" ' User name (if needed)
' mConfig.Fields.Item(schema + "sendpassword") = "" ' User password (if needed)
mConfig.Fields.Update()
Set mMessage = Sys.OleObject("CDO.Message")
mMessage.Configuration = mConfig
mMessage.From = mFrom
mMessage.To = mTo
mMessage.Subject = mSubject
mMessage.HTMLBody = mBody
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
Sub MainTest
Set oFolder = aqFileSystem.GetFolderInfo("C:\Documents and Settings\User\My Documents\TestComplete 8 Projects\Automation\Screenshots")
Set colFiles = oFolder.Files
strFileList = ""
aqString.ListSeparator = ","
While colFiles.HasNext
Set f = colFiles.Next
strFileList = aqString.AddListItem("C:/Documents and Settings/User/My Documents/TestComplete 8 Projects/Automation/Screenshots/" + f.Name, strFileList)
Wend
ListFiles = strFileList
If SendEmail("sent-from@email.com", "send-to@email.com", "Automated Testing", _
"This is a test message.", "" + ListFiles + "") Then
' Message was sent
Else
' Message was not sent
End If
End Sub
If someone knows a cleaner way to achieve this, or a way to remove the additional comma at the end of the ListFiles array (which causes a "noname" attachement to be attached along with the entire folder contents) then I'd be interested in hearing your solutions. Otherwise, I'm happy that it at least works and is doing what I require :)
Thanks for all your help! Much appreciated!
Related Content
- 7 years ago
- 13 years ago
Recent Discussions
- 17 hours ago
- 18 hours ago
- 4 days ago