How does this script (sending email using Outlook) work?

19,252

What you are doing in this code above is actually using the installed version of Outlook to send the mail message. Outlook may very well be using MAPI, but the API interface you are actually using here is COM based automation of Outlook. Code such as this will utilize Outlook to send email through any mail transportation system that Outlook can be configured to use. However, that is a lot of overhead just to send an email message. Depending on what email server(s) you might have local to you on your network or via there may be much more efficient ways to send email. But if this works fine and meets your needs currently, don't take that to mean that there is anything wrong with doing it the way you have above. It is all about understanding the tools available to you and how to best apply them to your particular problem space.

In your code, you are controlling Outlook and telling it to create an email message. You are passing off the message to Outlook and then Outlook is actually using the configured profile it has to determine how to hand off the message to a configured Exchange server or other installed/configure mail transport agents (mta).

Share:
19,252
duper
Author by

duper

Updated on June 05, 2022

Comments

  • duper
    duper almost 2 years

    Can anyone help me understand how MAPI works? I have this simple vbscript that uses MAPI to send an email out. It works perfectly fine, but, I don't know how it is doing that...what's happening in the background?

    Const ForReading = 1
    
    Set args = WScript.Arguments
    sqlFile = args.Item(0)
    logFile = args.Item(1)
    
    Dim ToAddress
    Dim FromAddress
    Dim MessageSubject
    Dim MyTime
    Dim MessageBody
    Dim MessageAttachment
    Dim ol, ns, newMail
    MyTime = Now
    
    ToAddress = "[email protected]"
    MessageSubject = "Subject goes here"
    MessageBody = "Body message goes here." 
    MessageAttachment = ""&logFile&""
    Set ol = WScript.CreateObject("Outlook.Application")
    Set ns = ol.getNamespace("MAPI")
    Set newMail = ol.CreateItem(olMailItem)
    newMail.Subject = MessageSubject
    newMail.Body = MessageBody & vbCrLf & MyTime
    newMail.RecipIents.Add(ToAddress)
    newMail.Attachments.Add(MessageAttachment)
    newMail.Send
    

    Thanks in advance...