Automatic Email using a field on a form

P

Paul Worsnop

Hi,

i am attempting to automatically send an email once a button has been
clicked on a requisition form. We originally set this up using the SendObject
command but this proved very flaky and was not always working correctly.

Instead I am using the code below but this delivers the following error
message:

The Microsoft Jet Database Engine cannot find the input table or query
"(e-mail address removed)". Make sure it exists and that its name
is spelt correctly.

Dim OutlookMessage As Object

Set OutlookMessage = CreateObject("Outlook.application").createitem(0)
Set MyRS = Mydb.OpenRecordset(Me.SupplierEmail)

'Repeat the following line for every file you want to attach to the Message
OutlookMessage.attachments.Add "S:\Shared Maint\Planned Maint\Temp\Woodhead
Req.rtf"
OutlookMessage.subject = "Requisition" ' Optional line
OutlookMessage.body = "Add text here" ' Optional line
OutlookMessage.Recipients.Add = MyRS 'Recipient or list of recipients of
the message
OutlookMessage.display ' Optional line, shows the message without sending it
(only if you want to check it manually). If not, use instead:
OutlookMessage.send 'That will send the message automatically
Set OutlookMessage = Nothing

I have a field on an open form and want to always use that field as the
email address to send the email and attachment to.

Can anyone help?

Thanks
 
P

Peter Hibbs

Paul,

What exactly does "automatically send an email" mean? Do you want to
open MS Outlook with the relevant fields (To: Subject: Body Message:
etc) filled in so that the user can then send the email by clicking
the Send button -
or
do you want to automatically send the email without the user seeing
your email client (MS Outlook or whatever) at all?

If the latter then you will get security prompts from Outlook each
time. There are alternatives depending on what you are trying to do.

BTW, in your code the OpenRecordset function should be followed with
the name of a table (or query) within double quotes not a control on
your form, i.e. OpenRecordset("MyTable").

Peter Hibbs.
 
P

Paul Worsnop

Thanks for your reply Peter.

I want to send an email automatically without the user having to do anything
(Click Send etc) and this needs to contain an attachment.

To get around the security prompt problems you pointed out I found a tool
called "Click Yes". This allows the mail to be sent without the need to click
"allow" on the security warning screen that appears.

I have managed to reach a solution using the following code which may be of
use to other people attempting the same as me:

On Error GoTo Error_Handler

Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = Me![SupplierEmail]
.Subject = "Look at this sample attachment"
.Body = "The body doesn't matter, just the attachment"
.Attachments.Add "S:\Shared Maint\Planned Maint\Temp\Woodhead Req.rtf"
.Send
'.ReadReceiptRequested
End With

Exit_Here:
Set objOutlook = Nothing
Exit Sub

Error_Handler:
MsgBox Err & ": " & Err.Description
Resume Exit_Here

Thanks again.

Paul
 
P

Peter Hibbs

Paul,

OK. If you find that the 'Click Yes' solution does not quite do what
you want then an alternative is use some free software which does not
use Outlook at all. See this site for more info :-
http://www.ostrosoft.com/

The SMTP Component file allows you to send emails (with attachments)
directly from within Access with no annoying security pop ups. It
does, however, require a DLL file to be installed on the PC. If this
is of interest to you I have some simple code which demonstrates how
to use it in a database.

HTH

Peter Hibbs.

Thanks for your reply Peter.

I want to send an email automatically without the user having to do anything
(Click Send etc) and this needs to contain an attachment.

To get around the security prompt problems you pointed out I found a tool
called "Click Yes". This allows the mail to be sent without the need to click
"allow" on the security warning screen that appears.

I have managed to reach a solution using the following code which may be of
use to other people attempting the same as me:

On Error GoTo Error_Handler

Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = Me![SupplierEmail]
.Subject = "Look at this sample attachment"
.Body = "The body doesn't matter, just the attachment"
.Attachments.Add "S:\Shared Maint\Planned Maint\Temp\Woodhead Req.rtf"
.Send
'.ReadReceiptRequested
End With

Exit_Here:
Set objOutlook = Nothing
Exit Sub

Error_Handler:
MsgBox Err & ": " & Err.Description
Resume Exit_Here

Thanks again.

Paul

Peter Hibbs said:
Paul,

What exactly does "automatically send an email" mean? Do you want to
open MS Outlook with the relevant fields (To: Subject: Body Message:
etc) filled in so that the user can then send the email by clicking
the Send button -
or
do you want to automatically send the email without the user seeing
your email client (MS Outlook or whatever) at all?

If the latter then you will get security prompts from Outlook each
time. There are alternatives depending on what you are trying to do.

BTW, in your code the OpenRecordset function should be followed with
the name of a table (or query) within double quotes not a control on
your form, i.e. OpenRecordset("MyTable").

Peter Hibbs.
 
P

Paul Worsnop

Thanks for all your help Peter.

I am happy with the solution I have found, however if you could post the
simple code I would like to see it as I am sure at some point in the future I
might well need to use this!

Thanks again

Paul

Peter Hibbs said:
Paul,

OK. If you find that the 'Click Yes' solution does not quite do what
you want then an alternative is use some free software which does not
use Outlook at all. See this site for more info :-
http://www.ostrosoft.com/

The SMTP Component file allows you to send emails (with attachments)
directly from within Access with no annoying security pop ups. It
does, however, require a DLL file to be installed on the PC. If this
is of interest to you I have some simple code which demonstrates how
to use it in a database.

HTH

Peter Hibbs.

Thanks for your reply Peter.

I want to send an email automatically without the user having to do anything
(Click Send etc) and this needs to contain an attachment.

To get around the security prompt problems you pointed out I found a tool
called "Click Yes". This allows the mail to be sent without the need to click
"allow" on the security warning screen that appears.

I have managed to reach a solution using the following code which may be of
use to other people attempting the same as me:

On Error GoTo Error_Handler

Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.To = Me![SupplierEmail]
.Subject = "Look at this sample attachment"
.Body = "The body doesn't matter, just the attachment"
.Attachments.Add "S:\Shared Maint\Planned Maint\Temp\Woodhead Req.rtf"
.Send
'.ReadReceiptRequested
End With

Exit_Here:
Set objOutlook = Nothing
Exit Sub

Error_Handler:
MsgBox Err & ": " & Err.Description
Resume Exit_Here

Thanks again.

Paul

Peter Hibbs said:
Paul,

What exactly does "automatically send an email" mean? Do you want to
open MS Outlook with the relevant fields (To: Subject: Body Message:
etc) filled in so that the user can then send the email by clicking
the Send button -
or
do you want to automatically send the email without the user seeing
your email client (MS Outlook or whatever) at all?

If the latter then you will get security prompts from Outlook each
time. There are alternatives depending on what you are trying to do.

BTW, in your code the OpenRecordset function should be followed with
the name of a table (or query) within double quotes not a control on
your form, i.e. OpenRecordset("MyTable").

Peter Hibbs.

On Fri, 4 Sep 2009 01:28:01 -0700, Paul Worsnop

Hi,

i am attempting to automatically send an email once a button has been
clicked on a requisition form. We originally set this up using the SendObject
command but this proved very flaky and was not always working correctly.

Instead I am using the code below but this delivers the following error
message:

The Microsoft Jet Database Engine cannot find the input table or query
"(e-mail address removed)". Make sure it exists and that its name
is spelt correctly.

Dim OutlookMessage As Object

Set OutlookMessage = CreateObject("Outlook.application").createitem(0)
Set MyRS = Mydb.OpenRecordset(Me.SupplierEmail)

'Repeat the following line for every file you want to attach to the Message
OutlookMessage.attachments.Add "S:\Shared Maint\Planned Maint\Temp\Woodhead
Req.rtf"
OutlookMessage.subject = "Requisition" ' Optional line
OutlookMessage.body = "Add text here" ' Optional line
OutlookMessage.Recipients.Add = MyRS 'Recipient or list of recipients of
the message
OutlookMessage.display ' Optional line, shows the message without sending it
(only if you want to check it manually). If not, use instead:
OutlookMessage.send 'That will send the message automatically
Set OutlookMessage = Nothing

I have a field on an open form and want to always use that field as the
email address to send the email and attachment to.

Can anyone help?

Thanks
 
P

Peter Hibbs

Paul,

To use this software first go to this site :-
http://www.ostrosoft.com/OSSMTP6.asp
Click on smtp_component.zip (in the Installation box) to download the
zip file to your PC.
Unzip the ossmtp.dll file into your C:\WINDOWS\System32 folder.
For Windows XP (don't know about Vista) click on Start -> Run
and enter :-
regsvr32.exe OSSMTP.dll
in the command-line and press ENTER. You should see a message to say
the control has been registered successfully.

Note that this is version 6 of this dll, version 7 is now available
(ossmpt_Plus.dll) but that uses .NET framework and I could not get
that to work on Win XP, maybe it works on Vista, I don't know. There
are also some demo database files in the zip file which may give a bit
more info about other options.

In your database project, open any code module and click Tools ->
References, find Ostrosoft SMTP Component in the list and tick the
box alongside it to set up a reference to the control.

Next create a new table called tblSMTPInformation (or whatever naming
convention you use) with the following fields :-

ID (AutoNumber)
SMTPServer (Text)
UserName (Text)
EMailPassword (Text)
Authentication (Text)
Pop3Server (Text)

Add one record and enter the data in the fields like this :-

SMTPServer = the name of your ISP Server (in MS Outlook it is the text
that shows under E mail Account name, mine is mail.btinternet.com).

UserName = the email address for your email account.

EMailPassword = the password for your email account.

Authentication = None or POP3 or LogIn or Plain. I use LogIn, POP3 is
used for a different type of account and if that is used, you also
need some text in the Pop3Server field, otherwise this can be left
blank. The None and Plain options would not normally be used (I
assume). See the Web site for more info on these settings, if
necessary.

If you have more than one email account then you should add a new
record in the table for each one but if you do, you will need some
method in the code below to choose which account is being used to send
the email. In this version I am assuming there is only ONE record in
this table.

Create a new standard module and paste the code below into it.
'----------------------------------------------------------------------------------------
Option Compare Database
Option Explicit

Public oSMTP As OSSMTP.SMTPSession


Public Function SendMailOstro(Optional vTo As String, _
Optional vCC As String = "", _
Optional vBCC As String = "", _
Optional vSubject As String = "", _
Optional vBody As String = "", _
Optional vAttachments As String = "") As Boolean

'Send an automatic email via OSSSMTP Component. Returns True if OK.
'Entry (vTo) = EMail addresses of recipients (separated with commas)
' (vCC) = Email addresses to be entered in CC field
' (vBCC) = Email addresses to be entered in BCC field
' (vSubject) = EMail subject text
' (vBody) = Body of the EMail message (HTML or Plain text)
' (vAttachments) = List of files to attach (separated by commas)
'Exit (SendMailOstro) = True if no error or False if error

Dim vSMTPServer As String, vUserName As String
Dim vPassword As String, vPOP3Server As String
Dim vAuthentication As String
Dim rst As Recordset
Dim vArray() As String
Dim vCount As Long

On Error GoTo ErrorCode

'Fetch Sender informaton
Set rst = CurrentDb.OpenRecordset("SELECT * FROM
tblSMTPInformation")
vSMTPServer = Nz(rst!SMTPServer) 'sender's Server ID
vUserName = Nz(rst!UserName) 'sender's email addr
vPassword = Nz(rst!EMailPassword) 'sender's password
vAuthentication = Nz(rst!Authentication) 'authentication mode
vPOP3Server = Nz(rst!POP3Server) 'POP3 code (if reqd)
rst.Close
Set rst = Nothing

'Check sender info is valid
If vSMTPServer = "" Or vUserName = "" _
Or vPassword = "" Or vAuthentication = "" Then Exit Function

Set oSMTP = New OSSMTP.SMTPSession

'Authentication
oSMTP.UserName = vUserName
oSMTP.Password = vPassword
If vAuthentication = "POP3" Then
oSMTP.POPServer = vPOP3Server
oSMTP.AuthenticationType = 1
Else
oSMTP.AuthenticationType = 2
End If

'Set parameters
oSMTP.Server = vSMTPServer
oSMTP.MailFrom = vUserName
oSMTP.SendTo = vTo
oSMTP.CC = vCC
oSMTP.BCC = vBCC
oSMTP.MessageSubject = vSubject
' oSMTP.MessageText = vBody 'use plain text
oSMTP.MessageHTML = vBody 'use HTML text
' oSMTP.Notification = 0 'set Notification (0-3)

'Add attachments (if any)
vArray = Split(vAttachments, ",")
For vCount = 0 To UBound(vArray)
oSMTP.Attachments.Add (vArray(vCount))
Next

'Send email and close
oSMTP.SendEmail
Set oSMTP = Nothing
SendMailOstro = True 'return True if OK
Exit Function

ErrorCode:
MsgBox Err.Description

End Function
'----------------------------------------------------------------------------------------
Save the module as modEMailCode (or whatever).

In your code just call the function something like this :-

Dim vReportNames As String, vSubject As String
Dim vBody As String, vEMailAddress As String

vEMailAddress = "email address1,email address2,email address3"
vSubject = "Subject of email"
vBody = "<HTML><H2>The body of this message will appear in
HTML.</H2><BODY>Enter the message text here. </BODY></HTML>"

If SendMailOstro(vEMailAddress, , , vSubject, vBody) = True Then
MsgBox "DONE"
Else
Beep
MsgBox "ERROR"
End If

Multiple email addresses should be separated by commas (not
semi-colons as in Outlook).

To send file attachments, add the full pathname and filesnames to the
function call like this :-

Dim vAttach As String

vAttach = "C:\Temp\Filename.txt,C:\WINDOWS\Filename.txt"
If SendMailOstro(vEMailAddress, , , vSubject, vBody, vAttach) =
True Then

Filenames should be in one string, again separated by commas.

To see more information on other parameters (such as the Notification
value, Importance level, Sensitivity, etc, etc) see the References
page at :- http://www.ostrosoft.com/smtp_component/help.asp

If you should get around to trying this out I would be interested to
hear how you get on.

Peter Hibbs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top