Using Microsoft Outlook to Send Email

The code below illustrates two methods for sending email using code, and two different COM objects. The following examples can be found in SendingEmail.vbs file located in the <Installdir>\CodeSamplesPlus folder.

This example is written in VBScript. To write similar code in Python, replace programming language statements with Python ones and make other syntax adjustments as necessary.

Copy code
' Example 1
Function SendMail(SendTo, Subject, Body, Attachment)
    Set ol=CreateObject("Outlook.Application")
    Set Mail=ol.CreateItem(0)
    Mail.to=SendTo
    Mail.Subject=
    Mail.Body=
    If (Attachment <> "") Then
        Mail.Attachments.Add(Attachment)
    End If
    Mail.Send
    ol.Quit
    Set Mail = Nothing
    Set ol = Nothing
End Function

' Example 2
Function SendMail(SendFrom, SendTo, Subject, Body)
    Set objMail=CreateObject("CDONTS.Newmail")
    ObjMail.From = SendFrom
    ObjMail.To = SendTo
    ObjMail.Subject = Subject
    ObjMail.Body = Body
    ObjMail.Send
    Set objMail = Nothing
End Function