Hi,
I am here again with a bit of challenge to tackle.
I want to send email from ms Access without Outlook, so I copied the following code from this forum, but my problem is how do I implement it on my application. Where do I place the code and how do I call it from my application?
I used "Call SendEmail" on a button, it is raising error. I also used "ModuleName.SendEmail", it is also raising error.
Here's the code:
Private Const URL_CDOCONFIG As String = "http://schemas.microsoft.com/cdo/configuration/"
Public Function SendEmail(ByVal sTo As String, ByVal sFrom As String, _
Optional ByVal sCC As String = "", Optional ByVal sBCC As String = "johnmc71@hotmail.com", _
Optional ByVal sSubject As String = "Just a Test", Optional ByVal sBody As String = "My Testing email body", _
Optional ByVal sServer As String = "smtp.gmail.com", Optional ByVal iPort As Integer = 587, _
Optional ByVal sUsername As String = "myEmail@gmail.com", Optional ByVal sPassword As String = "myPassword", _
Optional ByVal iSendUsing As Integer = 2, Optional ByVal bAuthenticate As Boolean = True, _
Optional ByVal bUseSSL As Boolean = True, Optional ByVal iTimeout As Integer = 60) As Boolean
On Error Resume Next
Err.Clear
Dim cdoConfig As Object
Dim cdomsg As Object
Set cdoConfig = CreateObject("CDO.Configuration")
Set cdomsg = CreateObject("CDO.Message")
'Dim cdomsg As CDO.Message
'Set cdomsg = CreateObject("CDO.message")
If Err Then
Debug.Print Err.Description
SendEmail = False 'Message not sent
Else
With cdomsg
With .Configuration.Fields
.Item(URL_CDOCONFIG & "sendusing") = iSendUsing
.Item(URL_CDOCONFIG & "smtpserver") = sServer
.Item(URL_CDOCONFIG & "smptserverport") = iPort
.Item(URL_CDOCONFIG & "smtpauthenticate") = IIf(bAuthenticate, 1, 0)
.Item(URL_CDOCONFIG & "smtpusessl") = bUseSSL
.Item(URL_CDOCONFIG & "smtpconnectiontimeout") = iTimeout
.Item(URL_CDOCONFIG & "sendusername") = sUsername
.Item(URL_CDOCONFIG & "sendpassword") = sPassword
.Update
End With
.To = sTo
.From = sFrom
.CC = sCC
.BCC = sBCC
.Subject = sSubject
.TextBody = sBody
If Err Then
Debug.Print Err.Description
SendEmail = False 'Message not sent
Else
DoCmd.Hourglass True
.Send
DoCmd.Hourglass False
If Err Then
Debug.Print Err.Description
SendEmail = False 'Message not sent
Else
SendEmail = True 'Message sent
End If
End If
End With
Set cdomsg = Nothing
End If
End Function
Thanks for quick reply,
Moore71
I am here again with a bit of challenge to tackle.
I want to send email from ms Access without Outlook, so I copied the following code from this forum, but my problem is how do I implement it on my application. Where do I place the code and how do I call it from my application?
I used "Call SendEmail" on a button, it is raising error. I also used "ModuleName.SendEmail", it is also raising error.
Here's the code:
Private Const URL_CDOCONFIG As String = "http://schemas.microsoft.com/cdo/configuration/"
Public Function SendEmail(ByVal sTo As String, ByVal sFrom As String, _
Optional ByVal sCC As String = "", Optional ByVal sBCC As String = "johnmc71@hotmail.com", _
Optional ByVal sSubject As String = "Just a Test", Optional ByVal sBody As String = "My Testing email body", _
Optional ByVal sServer As String = "smtp.gmail.com", Optional ByVal iPort As Integer = 587, _
Optional ByVal sUsername As String = "myEmail@gmail.com", Optional ByVal sPassword As String = "myPassword", _
Optional ByVal iSendUsing As Integer = 2, Optional ByVal bAuthenticate As Boolean = True, _
Optional ByVal bUseSSL As Boolean = True, Optional ByVal iTimeout As Integer = 60) As Boolean
On Error Resume Next
Err.Clear
Dim cdoConfig As Object
Dim cdomsg As Object
Set cdoConfig = CreateObject("CDO.Configuration")
Set cdomsg = CreateObject("CDO.Message")
'Dim cdomsg As CDO.Message
'Set cdomsg = CreateObject("CDO.message")
If Err Then
Debug.Print Err.Description
SendEmail = False 'Message not sent
Else
With cdomsg
With .Configuration.Fields
.Item(URL_CDOCONFIG & "sendusing") = iSendUsing
.Item(URL_CDOCONFIG & "smtpserver") = sServer
.Item(URL_CDOCONFIG & "smptserverport") = iPort
.Item(URL_CDOCONFIG & "smtpauthenticate") = IIf(bAuthenticate, 1, 0)
.Item(URL_CDOCONFIG & "smtpusessl") = bUseSSL
.Item(URL_CDOCONFIG & "smtpconnectiontimeout") = iTimeout
.Item(URL_CDOCONFIG & "sendusername") = sUsername
.Item(URL_CDOCONFIG & "sendpassword") = sPassword
.Update
End With
.To = sTo
.From = sFrom
.CC = sCC
.BCC = sBCC
.Subject = sSubject
.TextBody = sBody
If Err Then
Debug.Print Err.Description
SendEmail = False 'Message not sent
Else
DoCmd.Hourglass True
.Send
DoCmd.Hourglass False
If Err Then
Debug.Print Err.Description
SendEmail = False 'Message not sent
Else
SendEmail = True 'Message sent
End If
End If
End With
Set cdomsg = Nothing
End If
End Function
Thanks for quick reply,
Moore71