+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, VBS Script to send email using specified exchange account in Coding and Web Development; Hello all I have a vbs script which sends an email using the default outlook account on the machine. The ...
  1. #1

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    581
    Thank Post
    13
    Thanked 55 Times in 34 Posts
    Rep Power
    262

    VBS Script to send email using specified exchange account

    Hello all

    I have a vbs script which sends an email using the default outlook account on the machine. The problem i have is that not everyone who uses the script will have an outlook profile setup beforehand, but they will have an exchange profile. I need a way to send the email by specifying the accound details to use.

    This is what i have so far:

    Code:
    Dim ToAddress
    Dim MessageSubject
    Dim MessageBody
    Dim MessageAttachment
    
    Dim ol, ns, newMail
    
    ToAddress = "Recipients Address"
    MessageSubject = "Subject"
    MessageBody = "Message Body"
    MessageAttachment = "Attchment Path"
    
    Set ol = WScript.CreateObject("Outlook.Application")
    Set ns = ol.getNamespace("MAPI")
    ns.logon "","",true,false
    Set newMail = ol.CreateItem(olMailItem)
    newMail.Subject = MessageSubject
    newMail.Body = MessageBody & vbCrLf
    
    ' validate the recipient, just in case...
    Set myRecipient = ns.CreateRecipient(ToAddress)
    myRecipient.Resolve
    If Not myRecipient.Resolved Then
       MsgBox "unknown recipient"
    Else
       newMail.Recipients.Add(myRecipient)
       newMail.Send
    End If
    
    Set ol = Nothing
    I just need help specifying the exchange account (i am making the vbs script on the fly with another application so the username and password for the account can be written in if required)

    Does anyone know if this is possible??

    Thanks
    Last edited by LeightonJames; 18th March 2010 at 10:02 AM. Reason: found the attachment code!!

  2. #2
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    429
    Thank Post
    70
    Thanked 90 Times in 75 Posts
    Rep Power
    61
    Have you looked at CDO?

    Code:
    Set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = "Example Message"
    objMessage.From = "user@mydomain.com"
    objMessage.To = "recipient@them.com"
    objMessage.TextBody = "Sample text."
    objMessage.Send
    Use variables for the user and domain parts.

    no need for outlook profiles this way.

    BoX

  3. #3

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    581
    Thank Post
    13
    Thanked 55 Times in 34 Posts
    Rep Power
    262
    i thought CDO only worked for smtp?? It needs to be sent using MAPI as it's going through an exchange server.

    If this works then great but how can i add an attachment using this method?

  4. #4
    apeo's Avatar
    Join Date
    Sep 2005
    Location
    Lost
    Posts
    1,612
    Thank Post
    95
    Thanked 115 Times in 111 Posts
    Rep Power
    42
    Quote Originally Posted by LeightonJames View Post
    i thought CDO only worked for smtp?? It needs to be sent using MAPI as it's going through an exchange server.

    If this works then great but how can i add an attachment using this method?
    Why cant you send it via smtp when going through an exchange server?

    To add attachment:

    Code:
    objMessage.AddAttachment "c:\temp\attachment.txt"

  5. #5

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    581
    Thank Post
    13
    Thanked 55 Times in 34 Posts
    Rep Power
    262
    because i know zip about vbs coding and how to structure the server details. if anyone can help me then it would be greatly appreciated.

    EDIT: just a thought but using this method results in the email not actually being authenticated as such and going through a "back door" on the server. If i use this method for sending the emails in my Application i think my Bosses might have something to say about it.

    Thanks for all the help but i will stick with my having to need a profile method for now.
    Last edited by LeightonJames; 23rd March 2010 at 11:33 AM.

  6. #6
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    429
    Thank Post
    70
    Thanked 90 Times in 75 Posts
    Rep Power
    61
    maybe try something like:

    Code:
    ns.Logon("outlook-username", "outlook-password", false, true)

    BoX

  7. #7

    LeightonJames's Avatar
    Join Date
    Sep 2009
    Posts
    581
    Thank Post
    13
    Thanked 55 Times in 34 Posts
    Rep Power
    262
    stupid question i know but what does that do??

  8. #8
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    429
    Thank Post
    70
    Thanked 90 Times in 75 Posts
    Rep Power
    61
    Code:
    ns.logon "user@thing.co.uk","password",true,false
    seems to work for me.

    you already have

    Code:
    ns.logon "","",true,false
    in your code.

    try it, see what happens.

    BoX

SHARE:
+ Post New Thread

Similar Threads

  1. Zimbra AD Account Email Account Link
    By kmount in forum *nix
    Replies: 10
    Last Post: 4th June 2011, 03:14 PM
  2. Send email to one address only
    By rhyds in forum Windows
    Replies: 8
    Last Post: 3rd March 2008, 01:56 PM
  3. Replies: 9
    Last Post: 10th May 2007, 10:13 AM
  4. Replies: 3
    Last Post: 19th October 2006, 01:31 PM
  5. Script to change user email in AD (or Exchange)
    By SpuffMonkey in forum Scripts
    Replies: 16
    Last Post: 8th November 2005, 10:31 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •