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.