Forum Discussion

tcrompton's avatar
tcrompton
New Contributor
12 years ago

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

3 Replies

  • I also encountered the same issue, and I realize that it is related to the content type . 

    I was breaking my head trying to find a solution.

     finally I found a solution!





    below is my script, Please notice to the row in bold, in the attachment loop:

    "  mMessage.Attachments.Item(1).ContentMediaType="application/octet-stream"

    ********************************************************************************************


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


     


      Dim i, schema, mConfig, mMessage, mAttach


     


      Err.Clear


      On Error Resume Next


     



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


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


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


      'mConfig.Fields.Item(schema + "smtpserver") = "ServerName" ' SMTP server


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


      mConfig.Fields.Item(schema + "smtpauthenticate") = 1 ' Authentication mechanism


      ' 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.MimeFormatted=true


      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)


       mMessage.Attachments.Item(1).ContentMediaType="application/octet-stream"


        


      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

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Tyler,


     


    Do you mean your mailer gets the file with a different extension? Maybe your mail server does this?


     

  • tcrompton's avatar
    tcrompton
    New Contributor
    Yes, the extension on the receiving end is incorrect. After further research, I've discovered that the content type of the attachment is incorrect which makes the email clients think the file extension is incorrect thus changing it.



    To alter my question, how can I send the attachment with the correct content-type? My attempts at doing this are outlined in the link above.