Using CDOSYS To Send Outlook E-mails Without User Input

JingleRock

New Member
Joined
Aug 1, 2005
Messages
1
My macro file is stored on a shared drive and folder. The macro is digitally signed, by me, and each of the users (all in my domain) has accepted and stored on his/her computer the appropriate digital certificate. The main procedure is named 'Auto_Open'; this is triggered by a Windows XP Pro 'Scheduled Task' for each user.

My objective is for each user to view zero pop-ups when the Scheduled Task is processing. I have 'pretty much' accomplished this via use of 'Application.Visible = False' at the beginning of my code (and NOT using 'Application.Visible = True' anywhere in my code).

I have "given up" trying to use 'olMail.Display', 'olMail.Send', and 'Application.SendKeys "%S", True' to send e-mails, with .xls files attached. I could overcome problems with the first two stmts above (via use of products like 'ClickYes'), but then SendKeys would still be a problem when a user is accessing a macro digitally signed by me (e-mail to be sent is displayed on user's machine; for macro to continue, user is required to click on 'Send'). My objective is for macro to run on users' machines with no pop-ups (actually, e-mail display pop-ups are OK) and no user interaction. [Actually, I could use 'olMail.Send' and a product like 'ClickYes'; however, I do not wish to install 'ClickYes', or any other similar third-party products, on each of the users' machines.]

Which brings me to using CDO 1.21, CDO 3.O, or CDOSYS (our e-mail server is currently MS Exchange Server 5.5, with all of the Service Packs; I believe that we are upgrading to MS Exchange Server 2003) using VBA code.

As a first step, I am exploring use of CDOSYS -- I was able to find the following code using Google which has an objective to send e-mails via use of Port 25; immediately preceding this code, the site indicated it was "Visual Basic" code -- I am assuming that means VB6. I made a few modifications: I named the macro 'Sub SendMailviaPort25' (that is before I ‘commented-out’ this stmt); I moved the 'Dim' stmts above 'Private Sub ...'; I ‘commented-out’ all of the HTML references; I removed the comment character from '.TextBody = ...'; I added 'Option Explicit' at the top of the code; and I defined 'strTo' and 'strFrom' using my valid e-mail address. (It's a mystery to me why the code I found had the reference to Secure Sockets Layer = True 'commented-out' -- did the author want this stmt to be included or not???? -- why not set it to 'False'????

Also, I had recently learned that "mail.example.com" (used as name of SMTPServer in my code below) is frequently used as a default for an outgoing (smtp) server.

I thought that VB6 and Excel VBA 6.3 were essentially identical, but maybe not???? I copied the code below into a regular Module in VBA and I also copied it into 'ThisWorkbook'; in both locations, it is doing nothing. Perhaps I am supposed to put the code behind some type of UserForm, or in a Class Module. I have a bunch of references selected for my VBA Project: VBA; Excel 10.0 Obj Lib; OLE Automation; Office 10.0 Obj Lib; Messenger Private Type Lib; Outlook 10.0 Obj Lib; Outlook View Control; the 3 ActiveX Data Obj Libs; and CDO for Exchange 2000 Lib.

Here is the code:

Rich (BB code):
Option Explicit 
 'Send using the Port on a SMTP server
Dim iMsg As New CDO.Message 
Dim iConf As New CDO.Configuration 
Dim Flds As ADODB.Fields 
 'Dim strHTML
 
 'BELOW WAS A TEMPORARY STMT (USED TO ELIMINATE COMPILE ERRORS DUE TO NOT HAVING SELECTED REQUIRED PROJECT REFERENCES)
 'Sub SendMailviaPort25()
 
 'Sending SMTP mail via port 25 using CDOSYS
 'This VB sample uses CDOSYS to send SMTP mail using the cdoSendUsingPort option and specifying a SMTP host.
 
 'Note: It is recommended that all input parameters be validated when they are
 'first obtained from the user or user interface.
Private Sub SendMessage(strTo As String, strFrom As String) 
     
     'Send using the Port on a SMTP server
     'Dim iMsg As New CDO.Message
     'Dim iConf As New CDO.Configuration
     'Dim Flds As ADODB.Fields
     'Dim strHTML
     
    Set Flds = iConf.Fields 
     
    strTo = "Chuck.Grob@aiminvestments.com" 
    strFrom = "Chuck.Grob@aiminvestments.com" 
     
    With Flds 
        .Item(cdoSendUsingMethod) = cdoSendUsingPort 
        .Item(cdoSMTPServer) = "mail.example.com" 
         '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>??????
         'Use SSL to connect to the SMTP server:
         '.Item(cdoSMTPUseSSL) = True
        .Item(cdoSMTPConnectionTimeout) = 10 
        .Update 
    End With 
     
     'Build HTML for message body
     'strHTML = "<HTML>"
     'strHTML = strHTML & "<HEAD>"
     'strHTML = strHTML & "<BODY>"
     'strHTML = strHTML & " This is the test HTML message body</br>"
     'strHTML = strHTML & "</BODY>"
     'strHTML = strHTML & "</HTML>"
     
    With iMsg 
        Set .Configuration = iConf 
        .To = strTo 
        .From = strFrom 
        .Subject = "This is a test CDOSYS message (Sent by Port)" 
         '.HTMLBody = strHTML
        .TextBody = "This is the text body of the message..." 
         
        .Send 
    End With 
     
     'cleanup of variables
    Set iMsg = Nothing 
    Set iConf = Nothing 
    Set Flds = Nothing 
     
End Sub
I appreciate all help.
JingleRock
[/code]
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,215,263
Messages
6,123,957
Members
449,135
Latest member
jcschafer209

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top