Forum Discussion

anirshar's avatar
anirshar
Occasional Contributor
2 years ago

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

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

However I wish the contents of .mht file to be in email body. How can I do that?

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.Attachments.Item[1].ContentMediaType="application/octet-stream"
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 

7 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You will need to set MIME header fields, MIME-Version, Content-Type etc. similar to what you have already done.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If it does not work, then it's not supported by CDO.

     

    A while back, I tried to attach the .mht Test Execution Summary to Outlook Office 365. After researching, I found out that Outlook does not support it.

     

    I have found an alternative way of sending results via email, using XML. I convert the XML results into HTML which looks like this,

     

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi,

       

      To add to rraghvani : internally, .mht is compressed set of web pages, scripts, etc. It might be that such content is prohibited to be sent as attachment in your system. Try to manually send e-mail with .mht attachment and talk to your system administrators depending on what result you will get.

       

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You need to first verify whether your email system supports Multipurpose Internet Mail Extensions.