[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3630
  • Last Modified:

VBScript to Create Outlook 2010 Mapi Session

I was running Office 2003 then 2007 and now 2010. I had a VBScript that connected to my Outlook application and then sent an email out.

Upgrading from 2007 to 2010 broke the connection. I think it has something to do with Outlook 2010 not using CDO, but this is above my head.

I'm not sure how to rewrite this to handle Outlook 2010. Would someone better versed in this be able to help?

 On Error Resume Next
 Set objSession = CreateObject("mapi.session")
 objSession.Logon ("Outlook")
 Set objMsg = objSession.Outbox.Messages.Add
 Set objRecip = objMsg.Recipients.Add
 objMsg.Subject = "Subject - " & Month(Date) & "." & Day(Date) & "." & Year(Date)
 objMsg.Text = "Messge body goes here"
 With objMsg.Attachments.Add
  .Type = 1
  .Position = -1
  .Source = XLFile
  .Name = XLFile
  .ReadFromFile XLFile
 End With
 objRecip.Name = "email address goes here"
 objRecip.Type = 1
 objRecip.Resolve False
 Set objRecip = Nothing
 Set objMsg = Nothing
 Set objSession = Nothing

Open in new window

Thank you,

Shawn Janes
Shawn Janes
  • 3
  • 2
1 Solution
x-menIT super heroCommented:
does it have to connecto to outlook? why no just send it through smtp?
Shawn JanesAuthor Commented:
I guess not. The end result is all that matters. I need three emails to send with an attachment.
x-menIT super heroCommented:
here's a example:
Dim mailer : Set mailer = CreateObject("CDO.Message")
mailer.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
mailer.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "<Your SMTP server>" 
mailer.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
mailer.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendemailaddress") = "me@mydomain.com"
mailer.From = "me@mydomain.com"
mailer.To = "you@mydomain.com"
mailer.Subject = "my subject"
mailer.Textbody = "my text"
mailer.AddAttachment "C:\temp\myattach.txt"

Open in new window

Shawn JanesAuthor Commented:

Thank you for your help the code you provided worked perfectly. I ended up having to authenticate against our remote server due to need to send one of the emails outside of our domain, but I your code led me to the final solution. Thank you.

For others who come across this I have included a link below which includes detailed documentation on how to do this with and without authentication, local and remote services, and attachments, text, html format etc.

Shawn JanesAuthor Commented:
Thank you for your quick response and very good solution. I really appreciate you and your expertise.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now