Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB Script to send e-mail

Posted on 2009-04-10
4
Medium Priority
?
464 Views
Last Modified: 2012-06-27
I need a VB Script that will send e-mail, that is not dependent on CDO.  The script will run from desktops that have Outlook...I just do not want to depend on CDO.

Here is the current script:

I just want similar functionality without CDO.
strEmailDomainPos = instr(mailAdr, "@")
		GetEmailDomain = mid(mailAdr , strEmailDomainPos)
	End Function
 
 
	Sub GetSMTPServer 
		set cn = createobject("ADODB.Connection")
		set cmd = createobject("ADODB.Command")
		set rs = createobject("ADODB.Recordset")
 
		set objRoot = getobject("LDAP://RootDSE")
		configurationNC = objRoot.Get("configurationnamingcontext")
 
		cn.open "Provider=ADsDSOObject;"
		cmd.activeconnection = cn
 
		cmd.commandtext = "<LDAP://" & configurationNC & _
              ">;(objectCategory=msExchExchangeServer);name;subtree"
		set rs = cmd.execute
 
		while rs.eof<>true and rs.bof<>true
		      	strSMTPServer = rs(0)
			'msgbox rs(0)
		     	rs.movenext
		wend
	
		cn.close
	End sub
 
	Function GetEmail(strAccountName, strDomainName)
 
		Dim adoLDAPCon, adoLDAPRS, strLDAP
 
		Set adoLDAPCon = CreateObject("ADODB.Connection")
		adoLDAPCon.Provider = "ADsDSOObject"
		adoLDAPCon.Open "ADSI"
		strLDAP = "'LDAP://" & strDomainName & "'"
		Set adoLDAPRS = adoLDAPCon.Execute("select mail from " _
		 & strLDAP & " WHERE objectClass = 'user'"& _
		 " And samAccountName = '" & strAccountName & "'")
		With adoLDAPRS
			If Not .EOF Then
				GetEmail = .Fields("mail")
			Else
			        GetEmail = ""
			End If
		End With
		adoLDAPRS.Close
		Set adoLDAPRS = Nothing
		Set adoLDAPCon = Nothing
	End Function
 
	
	sub sendEmail
 
		Set objEmail = CreateObject("CDO.Message")
		objEmail.From = mailAdr
		objEmail.To = strEmailTo & strEmailDomain 
		msgbox objEmail.To
		objEmail.Subject = Summary.Value
		GetUserAddedInfo()
		objEmail.Textbody = Details.Value & vbCr & vbLf & strbaseInfo & vbCr & vbLf & GetUserAddedInfo 
		objEmail.Configuration.Fields.Item _
		 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		objEmail.Configuration.Fields.Item _
		 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
		     strSMTPServer
		objEmail.Configuration.Fields.Item _
		 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
		objEmail.Configuration.Fields.Update
		objEmail.Send
                ExitProgram
	End Sub

Open in new window

0
Comment
Question by:bbanis2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24118855
Hello bbanis2k,

        outlookMail strSubject:="Subject String", _
        strBody:="Body data is :", _
        strTo:="fred@fred.com", _
        SendYN:=False, _
        AttachYN:=attach <> "", _
        Attach1:=attach

Note:
    sendYN if true will send otherwise it will display the email
    attach is a comma seperated string of the filepaths for any attachmnets.

Any Help?

Regards,
Chris
Function outlookMail(strSubject As String, strBody As String, Optional strTo As String, _
                            Optional strCC As String, Optional strBCC As String, Optional SendYN As Boolean, _
                            Optional AttachYN As Boolean = False, Optional Attach1 As String) As Boolean
Dim olApp As Object
Dim olMsg As Object
Dim str() As String
Dim att As Variant 
    Set olApp = CreateObject("Outlook.Application")
    Set olMsg = olApp.CreateItem(0)
    With olMsg
        .To = strTo
        .CC = strCC
        .BCC = strBCC
        .subject = strSubject
        .body = strBody
        If AttachYN And Attach1 <> "" Then
            Attach1 = Replace(Attach1, ",", ";")
            str = Split(Attach1, ";")
            For Each att In str
                .Attachments.Add att
            Next
        End If
        If SendYN Then
            .Send
        Else
            .Display
        End If
    End With 
Set olMsg = Nothing
Set olApp = Nothing
End Function

Open in new window

0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 24120930
Apologies just realised you asked for script.

See as modified

Chris
attach = ""
outlookMail "Subject String", _
        "Body data is :", _
        "fred@fred.com", _
        "", _
        "", _
        False, _
        attach <> "", _
        attach
        
Sub outlookMail(strSubject, strBody, strTo, _
                            strCC, strBCC, SendYN, _
                            AttachYN, Attach1)
    Set olApp = CreateObject("Outlook.Application")
    Set olMsg = olApp.CreateItem(0)
    With olMsg
        .To = strTo
        .CC = strCC
        .BCC = strBCC
        .subject = strSubject
        .body = strBody
        If AttachYN And Attach1 <> "" Then
            Attach1 = Replace(Attach1, ",", ";")
            str = Split(Attach1, ";")
            For Each att In str
                .Attachments.Add att
            Next
        End If
        If SendYN Then
            .Send
        Else
            .Display
        End If
    End With 
Set olMsg = Nothing
Set olApp = Nothing
End Sub

Open in new window

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24903106
bbanis2k

What is the status of this Q from your perspective?

Chris
0
 

Author Closing Comment

by:bbanis2k
ID: 31569043
Thank you!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question