Forum Discussion

anirshar's avatar
anirshar
Occasional Contributor
2 years ago

How to use CDO objects to send email with .mht file as attachment but not as Outlook file

I am using CDO as below, to email the output .mht file upon execution of project.

However my problem is that email is received with the attachment as output.mht.msg (Outlook item file) 

def SendEmail(mFrom, mTo, mSubject, mBody, mAttach):
try:
schema = "http://schemas.microsoft.com/cdo/configuration/"
mConfig = Sys.OleObject["CDO.Configuration"]
mConfig.Fields.Item[schema + "sendusing"] = 2
mConfig.Fields.Item[schema + "smtpserver"] = "something.com"
mConfig.Fields.Item[schema + "smtpserverport"] = 25
mConfig.Fields.Update();
mMessage = Sys.OleObject["CDO.Message"]
mMessage.Configuration = mConfig;
mMessage.From = "someone@somewhere.com"
mMessage.To = "anotherone@somewhere.com"
mMessage.Subject = "Subject"
mMessage.HTMLBody = "Body"
mMessage.AddAttachment("file:///C:/Report.mht")
mMessage.Send();
except Exception as e:
Log.Error("E-mail cannot be sent", str(e))
return False
Log.Message("Message was sent successfully to anotherone@somewhere.com")
return True 

2 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Have you tried with,

    mMessage.AddAttachment("C:\Report.mht")

    ?

     

    • anirshar's avatar
      anirshar
      Occasional Contributor

      Actually it worked with below command

       

      mMessage.Attachments.Item[1].ContentMediaType="application/octet-stream"