473,408 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Send Email with button click on Access Form using CDO

I have read through many posts that are relevant to my topic and have not had any success.

I want to click a button on my form and send an email to a list of people I have stored in a table with email addresses.

My SMTP server uses many defaults(ie port, timeout) I have been asked to leave the defaults alone, if possible.

Here is a copy of my code that I am trying to run. In my button click I call the first function:
Expand|Select|Wrap|Line Numbers
  1. Public Function EmailAlert() As Boolean
  2. On Error Resume Next
  3. Stop
  4. Dim db As DAO.Database, rst As DAO.Recordset, sSQL As String
  5. Dim Subjectstr As String, Bodystr As String, SendToVar As String
  6.  
  7. Set db = CurrentDb
  8.  
  9. Set rst = db.OpenRecordset("EmailAlertQuery", dbOpenForwardOnly)
  10.  
  11. If Not rst.EOF And Not rst.BOF Then
  12.    rst.MoveFirst
  13.  
  14.     Bodystr = "DATE : " & Format(rst!AlertDate, "Long date") & vbCrLf & vbCrLf
  15.     Bodystr = Bodystr & "Duty Officer: " & rst!DutyOfficer & vbCrLf & vbCrLf
  16.     Subjectstr = "Alert For " & Format(rst!AlertDate, "Long Date")
  17.  
  18.     Do Until rst.EOF
  19.  
  20.     Bodystr = Bodystr & "TankerBase: " & rst!TankerBase & vbCrLf
  21.     If rst!FCPC <> "" And Not IsNull(rst!FCPC) Then
  22.        Bodystr = Bodystr & "Fire Center PC: " & rst!FCPC & vbCrLf
  23.     End If
  24.     If rst!BirddogID <> "" And Not IsNull(rst!BirddogID) Then
  25.         Bodystr = Bodystr & "Birddog: " & rst!BirddogID & vbCrLf
  26.     End If
  27.     If rst!AirtankerID <> "" And Not IsNull(rst!AirtankerID) Then
  28.         Bodystr = Bodystr & "Airtankers: " & rst!AirtankerID & vbCrLf
  29.         Bodystr = Bodystr & "Type: " & rst!Type & vbCrLf
  30.     End If
  31.     If rst!Green <> "" And Not IsNull(rst!Green) Then
  32.         Bodystr = Bodystr & "Green: " & rst!Green & vbCrLf
  33.     End If
  34.     If rst!Blue <> "" And Not IsNull(rst!Blue) Then
  35.         Bodystr = Bodystr & "Blue: " & rst!Blue & vbCrLf
  36.     End If
  37.     If rst!Yellow <> "" And Not IsNull(rst!Yellow) Then
  38.         Bodystr = Bodystr & "Yellow: " & rst!Yellow & vbCrLf
  39.     End If
  40.     If rst!Red <> "" And Not IsNull(rst!Red) Then
  41.         Bodystr = Bodystr & "Red: " & rst!Red & vbCrLf
  42.     End If
  43.     If rst!AlertEnd <> "" And Not IsNull(rst!AlertEnd) Then
  44.         Bodystr = Bodystr & "Alert End: " & rst!AlertEnd & vbCrLf
  45.     End If
  46.     Bodystr = Bodystr & vbCrLf
  47.     rst.MoveNext
  48.     Loop
  49.  
  50. Else
  51.     MsgBox " You have not entered an Alert for Today yet."
  52. End If
  53.  
  54. Set rst = Nothing
  55. Set rst = db.OpenRecordset("EmailAddressListQuery", dbOpenForwardOnly)
  56.  
  57. If Not rst.EOF And Not rst.BOF Then
  58.     rst.MoveFirst
  59.  
  60.     Do Until rst.EOF
  61.         SendToVar = SendToVar & rst!EmailAddress & ";"
  62.         rst.MoveNext
  63.     Loop
  64. End If
  65.  
  66. Call SendEMail_CDO("PATCCORP", SendToVar, "chriwill", Subjectstr, Bodystr)
  67. End Function
  68.  
  69. Public Function SendEMail_CDO(strFrom, strTo, strCC, strSubject, strBody)
  70.  
  71. Dim mail
  72. Dim config
  73. Dim fields
  74.  
  75.  
  76. Set mail = CreateObject("CDO.Message")
  77. Set config = CreateObject("CDO.Configuration")
  78. Set fields = config.fields
  79.  
  80. With fields
  81.  
  82. .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  83.  
  84. .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail server name" ' IP OF SERVER
  85.  
  86. commented out for now would like to leave as default
  87. '.Item"http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
  88. .Update
  89. End With
  90.  
  91. Set mail.Configuration = config
  92.  
  93. With mail
  94. .From = strFrom
  95. .To = strTo
  96. .CC = strCC
  97. .Subject = strSubject
  98. .TextBody = strBody
  99. .Send
  100. End With
  101.  
  102. Set mail = Nothing
  103. Set fields = Nothing
  104. Set config = Nothing
  105. Set rst = Nothing
  106. Set db = Nothing
  107.  
  108. End Function
  109.  
The first function works perfect with no issues.


Please help !!! I have struggled with this for days!!!

Thanks in advance
Jul 12 '07 #1
4 2995
JConsulting
603 Expert 512MB
I have read through many posts that are relevant to my topic and have not had any success.

I want to click a button on my form and send an email to a list of people I have stored in a table with email addresses.

My SMTP server uses many defaults(ie port, timeout) I have been asked to leave the defaults alone, if possible.

Here is a copy of my code that I am trying to run. In my button click I call the first function:

Public Function EmailAlert() As Boolean
On Error Resume Next
Stop
Dim db As DAO.Database, rst As DAO.Recordset, sSQL As String
Dim Subjectstr As String, Bodystr As String, SendToVar As String

Set db = CurrentDb

Set rst = db.OpenRecordset("EmailAlertQuery", dbOpenForwardOnly)

If Not rst.EOF And Not rst.BOF Then
rst.MoveFirst

Bodystr = "DATE : " & Format(rst!AlertDate, "Long date") & vbCrLf & vbCrLf
Bodystr = Bodystr & "Duty Officer: " & rst!DutyOfficer & vbCrLf & vbCrLf
Subjectstr = "Alert For " & Format(rst!AlertDate, "Long Date")

Do Until rst.EOF

Bodystr = Bodystr & "TankerBase: " & rst!TankerBase & vbCrLf
If rst!FCPC <> "" And Not IsNull(rst!FCPC) Then
Bodystr = Bodystr & "Fire Center PC: " & rst!FCPC & vbCrLf
End If
If rst!BirddogID <> "" And Not IsNull(rst!BirddogID) Then
Bodystr = Bodystr & "Birddog: " & rst!BirddogID & vbCrLf
End If
If rst!AirtankerID <> "" And Not IsNull(rst!AirtankerID) Then
Bodystr = Bodystr & "Airtankers: " & rst!AirtankerID & vbCrLf
Bodystr = Bodystr & "Type: " & rst!Type & vbCrLf
End If
If rst!Green <> "" And Not IsNull(rst!Green) Then
Bodystr = Bodystr & "Green: " & rst!Green & vbCrLf
End If
If rst!Blue <> "" And Not IsNull(rst!Blue) Then
Bodystr = Bodystr & "Blue: " & rst!Blue & vbCrLf
End If
If rst!Yellow <> "" And Not IsNull(rst!Yellow) Then
Bodystr = Bodystr & "Yellow: " & rst!Yellow & vbCrLf
End If
If rst!Red <> "" And Not IsNull(rst!Red) Then
Bodystr = Bodystr & "Red: " & rst!Red & vbCrLf
End If
If rst!AlertEnd <> "" And Not IsNull(rst!AlertEnd) Then
Bodystr = Bodystr & "Alert End: " & rst!AlertEnd & vbCrLf
End If
Bodystr = Bodystr & vbCrLf
rst.MoveNext
Loop

Else
MsgBox " You have not entered an Alert for Today yet."
End If

Set rst = Nothing
Set rst = db.OpenRecordset("EmailAddressListQuery", dbOpenForwardOnly)

If Not rst.EOF And Not rst.BOF Then
rst.MoveFirst

Do Until rst.EOF
SendToVar = SendToVar & rst!EmailAddress & ";"
rst.MoveNext
Loop
End If

Call SendEMail_CDO("PATCCORP", SendToVar, "chriwill", Subjectstr, Bodystr)
End Function

Public Function SendEMail_CDO(strFrom, strTo, strCC, strSubject, strBody)

Dim mail
Dim config
Dim fields


Set mail = CreateObject("CDO.Message")
Set config = CreateObject("CDO.Configuration")
Set fields = config.fields

With fields

.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail server name" ' IP OF SERVER

commented out for now would like to leave as default
'.Item"http://schemas.microsoft.com/cdo/configuration/ smtpconnectiontimeout") = 10
.Update
End With

Set mail.Configuration = config

With mail
.From = strFrom
.To = strTo
.CC = strCC
.Subject = strSubject
.TextBody = strBody
.Send
End With

Set mail = Nothing
Set fields = Nothing
Set config = Nothing
Set rst = Nothing
Set db = Nothing

End Function

The first function works perfect with no issues.


Please help !!! I have struggled with this for days!!!

Thanks in advance

give this a go
j
Expand|Select|Wrap|Line Numbers
  1. Public Function SendEmailCDO(ByVal strTo As String, _
  2.                           ByVal strMessage As String, _
  3.                           ByVal strSubject As String, _
  4.                           Optional ByVal strAttach As String)
  5.     Dim objEmail As Object
  6.     On Error Resume Next
  7.     Set objEmail = CreateObject("CDO.Message")
  8.     '**** email address of sender
  9.     objEmail.FROM = "fred@smith.com"
  10.     objEmail.To = strTo
  11.     objEmail.Subject = strSubject
  12.     objEmail.TextBody = strMessage
  13.     If strAttach <> "" Then objEmail.AddAttachment strAttach
  14.     objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/ configuration/sendusing") = 2
  15.     '**** smtp.xxx.com - here u enter your smtp server name, whatever that is
  16.     objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/ cdo/configuration/smtpserver") = "smtp.xx.com"
  17.     objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/ cdo/configuration/smtpserverport") = 25
  18.     objEmail.Configuration.Fields.Update
  19.     objEmail.Send
  20.     If err.Number <> 0 Then
  21.         MsgBox "Error in sending. " & err.Description
  22.     Else
  23.         MsgBox "Sent"   'remove this if u dont want confirmation
  24.     End If
  25.     Set objEmail = Nothing
  26.  
  27. End Function
  28.  
  29.  
  30.  
The code above is a working model.
Jul 13 '07 #2
Thank you so much for the help J !!!

Works like a dream. I have little knowledge on port assignment and was wary of sending to a wrong port, if that is possible, and the repercussions that might follow if I did.

My breadth of knowledge on ports is limited to port 80 is used for web..... I think? :p

Any insight would be appreciated

Thanks again
Jul 13 '07 #3
JConsulting
603 Expert 512MB
Thank you so much for the help J !!!

Works like a dream. I have little knowledge on port assignment and was wary of sending to a wrong port, if that is possible, and the repercussions that might follow if I did.

My breadth of knowledge on ports is limited to port 80 is used for web..... I think? :p

Any insight would be appreciated

Thanks again

We aren't the best source on what YOUR ports are set up like. You will need to check with YOUR administrator to verify port assignments. We have no way of knowing those for you.
J
Jul 13 '07 #4
Understandable, No problem. Was more interested if there was code to use that just picked up the default settings on the mailserver, rather than explicitly setting it?

Side Note : Do I close the thread when done or do one of the guru"s ?
(Sorry reletivley new to the site)

Thanks again for all your help !!
Jul 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: MLH | last post by:
How can I hit www.someplace.com from an Access form? I'd like to... 1) open user's default browser (if not already open) 2) type in a URL 3) press enter (or click Go - or - whatever it takes) ...
2
by: rcmail14872 | last post by:
I have a form that lists several different categories for labels. I made a form that will let the user add a new category. On the Form the user can type in the name for the new category, then I...
0
by: Qajussi | last post by:
Hi! I am trying to create data entry forms like MS access forms using asp.net. I am talking about a form like MS access forms which let you enter a new record, edit, navigate to previous and next...
1
by: mike11d11 | last post by:
If someone could help me, I need to be able to send attachments from my access database that I have created. This database runs queries then generates a report off the queries from underlying...
2
by: diwadube2003 | last post by:
I get a Warning message while automatically sending email through MS Access, using Outlook. Can anybody help me out with this.
7
by: cassey14 | last post by:
Hi Everyone.. I hope someone help me on this.. Is it possible that we can attach an email messages inside an access form??please help me.. tanx much in advance.. cassey
3
by: HelloWorldHelloWorldHello | last post by:
How to send email to another person with using Microsoft Visual Basic 6.0.?
2
by: HyBry | last post by:
I am quite new to this and my VB knowledge is close to zero. I have created a for that shows info about applications and it includes a subform with all the people responsible for the application....
4
by: sam12 | last post by:
Hi I wana send Email with attached pdf files from my ms access 2000 ,os win 2000 via outlook express without confirmation . I googled a lot Tried so many codes but that didn't work Thank in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.