SMTP - Arrrgh (1 Viewer)

Smudger Smith

I like numbers me
Local time
Today, 09:36
Joined
May 30, 2007
Messages
25
I have some code (Below) that I have used to send e-mails via outlook.

Thanks to Mr Gates I can't do this any more - Looking at varioous threads it seems I have to use SMTP?

How the heck do i change this to SMTP

Guidance please as I have an internal customer on my back about this (And he is quite fat, so it hurts!)

Public Function SendEmailToError()

PROC_DECLARATIONS:
Dim olApp As Outlook.Application
Dim olnamespace As Outlook.NameSpace
Dim olMail As Outlook.MailItem
Dim dbs As Database
Dim rstEmailDets As Recordset
Dim strSender As String
Dim strRecipient As String
Dim strEmail As String

PROC_START:
On Error GoTo PROC_ERROR

PROC_MAIN:
DoCmd.SetWarnings False

Set dbs = CurrentDb

'Put on hourglass
DoCmd.Hourglass True

'Collect the email details in a recordset to use in the email loop below
Set rstEmailDets = dbs.OpenRecordset("SELECT * FROM TBL_0299_ERROR_FILE;")

With rstEmailDets
.MoveFirst
Do While Not rstEmailDets.EOF

'Set parameters for email: recipient name, recipient email
strRecipient = Nz(rstEmailDets("NAME"), "")
strEmail = Nz(rstEmailDets("E_MAIL1"), "")
strBody = Nz(rstEmailDets("SUPRESS_REASON"), "")
strSite = Nz(rstEmailDets("RESTOID"), "")

'Create a new instance of an Outlook Application object
Set olApp = New Outlook.Application
Set olnamespace = olApp.GetNamespace("MAPI")
Set olMail = olApp.CreateItem(olMailItem)

With olMail

'Email Details
.To = strEmail
.Subject = "MTI message for site " & strSite
.Body = "An error has been detected in association with " & strRecipient _
& ". The cause of the error is: " & strBody
.Importance = olImportanceNormal
.Send

End With
.MoveNext
Loop

End With

MsgBox "All Emails have now been sent, Thank you for your patience"

PROC_EXIT:
' Perform cleanup code here, set recordsets to nothing, etc.
On Error Resume Next
DoCmd.RunCommand acCmdWindowHide '(hides the database window)
Set olApp = Nothing
Set olnamespace = Nothing
Set olMail = Nothing
Set dbs = Nothing
Set rstEmailDets = Nothing
DoCmd.Hourglass False
Exit Function

PROC_ERROR:
If Err = -2147467259 Then
MsgBox "You have exceeded the storage limit on your mail box. Please delete some items before clicking OK", vbOKOnly
Resume
End If

If Err = 2501 Then
MsgBox "You have attempted to cancel the output of the emails." & vbCrLf & _
"This will cause major problems." & vbCrLf & _
"Please be Patient"
Resume
End If

End Function


Thanks in advance

Smudger
 

MarkK

bit cruncher
Local time
Today, 01:36
Joined
Mar 17, 2004
Messages
8,186
I use code like this to bypass any email client and send mail directly to the outgoing server...
Code:
Public Sub Send(recipients As String, from As String, subject As String, smtpServer As String, Optional msg As String, Optional attachPath As String)
On Error GoTo handler
   Dim iMsg As New CDO.Message
   Dim iConf As New CDO.Configuration
   
   'set up configuration
   With iConf
      With .fields
         .item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
         .item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer
         .item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
         .item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous
         .Update
      End With
   End With
   
   'configure message
   With iMsg
       Set .Configuration = iConf
       .To = recipients
       .from = from
       .subject = subject
       If msg <> "" Then .TextBody = msg
       If attachPath <> "" Then .AddAttachment attachPath
       .Send
   End With
   
   'tidy up
   Set iMsg = Nothing
   Set iConf = Nothing
   Exit Sub

handler:
   Err.Raise Err, Err.Source & " - Lib.Mail.Send()"
End Sub

The 'smtpServer' parameter is the same string you use for the SMTP Server when you set up your email account, commonly something like 'mail.isp.com'
This code also requires that you set a reference to Microsoft's Collaboration Data Objects module, but you can also late bind.
Given that it bypasses email clients, there is no record that the email was sent. If you need a record, send yourself a copy.

Hope this provides a reasonable starting place to get things going.
Cheers,
 

Smudger Smith

I like numbers me
Local time
Today, 09:36
Joined
May 30, 2007
Messages
25
Lagbolt - You are a star - Thanks so much for this - You have saved me from a certain Ulser

Cheers

Smudger
 

Pleasure

Registered User.
Local time
Today, 11:36
Joined
Jul 26, 2005
Messages
44
I use code like this to bypass any email client and send mail directly to the outgoing server...

The 'smtpServer' parameter is the same string you use for the SMTP Server when you set up your email account, commonly something like 'mail.isp.com'
This code also requires that you set a reference to Microsoft's Collaboration Data Objects module, but you can also late bind.
Given that it bypasses email clients, there is no record that the email was sent. If you need a record, send yourself a copy.

Hope this provides a reasonable starting place to get things going.
Cheers,

Where is that Microsoft Collaboration Data Module ???? I want to select it under references but cannot find it ...
 

Pleasure

Registered User.
Local time
Today, 11:36
Joined
Jul 26, 2005
Messages
44
hey guys. Doesn't anyone know about that Microsoft Collaboration Data Module ????
 

Smudger Smith

I like numbers me
Local time
Today, 09:36
Joined
May 30, 2007
Messages
25
Pleasure,

I have not found it either - In the end I cheated, stayed with Outlook 2003 and installed Express click yes. Works fine but obviously not the best solution.
 

Rabbie

Super Moderator
Local time
Today, 09:36
Joined
Jul 10, 2007
Messages
5,906
I don't use this module myself but I think you will find it in the References list in the Visual Basic Editor as Microsoft CDO library.

Hope this helps
 

Pleasure

Registered User.
Local time
Today, 11:36
Joined
Jul 26, 2005
Messages
44
IT has CDO 1.21 and CDO for WINDOWS 2000. Which one ?
 

quest4

Registered User.
Local time
Today, 04:36
Joined
Oct 12, 2004
Messages
246
Thank you very much for all of these great responses. Forgive me here I have not done any code writing in over a year and I am very rusty. Lagbolt your code looks pretty good and I am going to try and us it but I have a couple of quick questions, first do I need to turn on any references? Second, where do I put it, like in a moduale and in my macro use RunCode or what? Thanks again for all of the help.
 

Users who are viewing this thread

Top Bottom