简体   繁体   中英

Using a VBScript to send an email through Outlook

I've recently wrote a VBScript to send an email using Outlook. Everything works great untill it gets to the sending point. I'm assuming since it only opens a single message, and since the message was sent... the Window closes and Outlook is killed.

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.Display   'To display message
objMail.Recipients.Add ("address@example.com")
objMail.Subject = "Mail Subject"
objMail.Body = "This is Email Body"
objMail.Send
objOutlook.Quit
Set objMail = Nothing
Set objOutlook = Nothing

All the information is entered into the email correctly. However, once the send command is executed, the message is put into the outbox and never sent unless Outlook is opened manually after the fact. Is there a methodology I could use to perhaps run a Send/Recieve before it ends the script? Or a delay that would keep the Outlook process alive for it to actually Send?

I cant seem to think of anything so all help is appreciated.

Thanks

Similar to Application.Wait, except there is no extra delay, nor the possibility of not waiting long enough.

Dim origSentMailCount as Long

origSentMailCount = objOutlook.GetDefaultFolders(olFolderSentMail).Count

objMail.Send

do until objOutlook.GetDefaultFolders(olFolderSentMail).Count > origSentMailCount
doevents
loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM