tcrompton
12 years agoNew Contributor
Adding an attachment to an email sent via CDO
I followed the instructions in this example to send an email via CDO, but when I add an attachment, the file extension is changed. Does anybody know why it does this and/or how this can be avoided?
Here is my code.
Sub SendResults()
WorkingDirectory = Project.ConfigPath & "Log\ExportedResults\"
FileName = WorkingDirectory & "Log.mht"
Log.SaveResultsAs FileName, 2
Call SendEmail("example@example.com", "example@commandalkon.com", "Test Results", ProjectSuite.Variables.EmailBody, FileName)
End Sub
Function SendEmail(FromAddress, ToAddress, Subject, Body, Attachment)
Call Err.Clear
On Error Resume Next
Set Config = Sys.OleObject("CDO.Configuration")
Config.Fields.Item(Schema + "sendusing") = 2
Config.Fields.Item(Schema + "smtpserver") = "SMTPSERVER"
Config.Fields.Item(Schema + "smtpserverport") = 25
Config.Fields.Item(Schema + "smtpauthenticate") = 1
' Config.Fields.Item(schema + "sendusername") = ""
' Config.Fields.Item(schema + "sendpassword") = ""
Call Config.Fields.Update
Set Message = Sys.OleObject("CDO.Message")
Set Message.Configuration = Config
Message.From = FromAddress
Message.To = ToAddress
Message.Subject = Subject
Message.HTMLBody = Body
Call Message.AddAttachment(Attachment)
Call Message.Send
If Err.Number = 0 Then
SendEmail = True
Else
Call Log.Error("Email could not be sent.", Err.Description)
End If
End Function
Here is my code.
Sub SendResults()
WorkingDirectory = Project.ConfigPath & "Log\ExportedResults\"
FileName = WorkingDirectory & "Log.mht"
Log.SaveResultsAs FileName, 2
Call SendEmail("example@example.com", "example@commandalkon.com", "Test Results", ProjectSuite.Variables.EmailBody, FileName)
End Sub
Function SendEmail(FromAddress, ToAddress, Subject, Body, Attachment)
Call Err.Clear
On Error Resume Next
Set Config = Sys.OleObject("CDO.Configuration")
Config.Fields.Item(Schema + "sendusing") = 2
Config.Fields.Item(Schema + "smtpserver") = "SMTPSERVER"
Config.Fields.Item(Schema + "smtpserverport") = 25
Config.Fields.Item(Schema + "smtpauthenticate") = 1
' Config.Fields.Item(schema + "sendusername") = ""
' Config.Fields.Item(schema + "sendpassword") = ""
Call Config.Fields.Update
Set Message = Sys.OleObject("CDO.Message")
Set Message.Configuration = Config
Message.From = FromAddress
Message.To = ToAddress
Message.Subject = Subject
Message.HTMLBody = Body
Call Message.AddAttachment(Attachment)
Call Message.Send
If Err.Number = 0 Then
SendEmail = True
Else
Call Log.Error("Email could not be sent.", Err.Description)
End If
End Function