Use Default Email Client and Attach file

craigachan

Registered User.
Local time
Yesterday, 19:57
Joined
Nov 9, 2007
Messages
285
I've looked all over the place for this, but I haven't found any solution related to Ac2007.

I want to use a default email client and attach a file (in this case a word doc) to it, the automate send and close the email. I've seen code using sendkeys but it doesn't sound like this is a good idea.

Can someone give me a sample code on how to do this? I've tried 'SendObject' but don't know how to attach. I've tried ShellExecute and also have the same problem - Don't know all the options for these.

Any ideas?
 
I think part of the difficulty you will have here is the variety of email clients out there. Are you targeting a particular client that should be the default?
 
i have posted a FAQ in the Access faq section about sending emails from outlook. if you can get libraries for the email client you are using, the same code might be useful to you.
 
Thanks for your help. I'm starting with Window Mail and Outlook Express for now. But I'll want to add to the code eventually so that everyone is included. Not sure where to start.

I went to Access FAQ but did not see anything. I'm not sure what FAQ you were referring to. There are only 4 threads there.
 
It probably will not be possible to reliable automate all email clients. In my experience, allowing an email client to be used via automation opens a huge security hole. That is probably why some do not allow it.

I found it better to program a solution once and never have to worry about any email client ever again.

That is why I use this:
vbSendMail.dll Version 3.65-- Easy E-mail Sending in VB, with Attachments

It does not require a email client to even be installed. You just have to have access to an SMTP server. Basically is allow Access to act line a email client for sending only.
 
Last edited:
Thanks HiTech for your suggestion. I've looked at the files and scanned the .doc file, and holy smokes, it's going to be over my head. But I can follow instructions. Is there a tutorial that you know of on how to install all of this properly? I think once I get it installed, I'll be able to figure the details. Nothing like learning by trial and error.
 
To install the DLL, simple copy it to your windows\system folder and register it.

In the VBA editor, add a reference to the DLL

Using this DLL is not really any different that automating any other email client, like Outlook.

AFAIK, you can not use automation to send email with Access through Outlook Express or Windows Mail. That is why I suggested that you use this DLL.
 
Here is some SMTP that does the attachment and also uses Word as the body of the email. The Word doc has to be saved as the HTM file, which VBA will do. I originally got most of this code from HiTechCoach. However, in my own case I found that anyone I ended up doing business with has MS Office and hence Outlook and Outlook does not need to be the default email to use it. Outlook is also great for deal with incoming main and saving attachments, body of the email, linking bounce backs to client list etc and etc.

I ran the following with a RunCode macro action. I am not 100% sure if the following is what I finally used but I think it is. If there is a problem then I will go back and look in my tool box.

With SMTP there is nothing in the Send Box. But what I did was have it store within Access etc.



Code:
Public Function EMTester1()
 

Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
 
Set objmessage = CreateObject("CDO.Message")
objmessage.Subject = [Forms]![PrintAndClose]![Text446]
objmessage.From = """Mike McGuire"" <[EMAIL="Mike375@optusnet.com.au"]Mike375@optusnet.com.au[/EMAIL]>"

objmessage.To = [Forms]![PrintAndClose]![EMStore]
 
objmessage.CreateMHTMLBody "[URL="file:///\\c:\Letters\Softwaresolutions3.htm"]file:\\c:\Letters\Softwaresolutions3.htm[/URL]"
 
 
 

'==This section provides the configuration information for the remote SMTP server.
objmessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/sendusing[/URL]") = 2
'Name or IP of Remote SMTP Server
objmessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserver[/URL]") = "mail.optusnet.com.au"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objmessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpauthenticate[/URL]") = cdoBasic
'Your UserID on the SMTP server
objmessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/sendusername[/URL]") = "Mike375"
'Your password on the SMTP server
objmessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/sendpassword[/URL]") = [Forms]![PrintAndClose]![Text459]
'Server port (typically 25)
objmessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpserverport[/URL]") = 25
'Use SSL for the connection (False or True)
objmessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpusessl[/URL]") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objmessage.Configuration.Fields.Item _
("[URL]http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout[/URL]") = 60
 

      
   
 
'==End remote SMTP server configuration section==
 
  objmessage.AddAttachment ("C:\Letters\SoftwareAttach.doc")
 
objmessage.Send
 
  
End Function
 
Thanks for both of your suggestions.

HiTechCoach, I tried the vbSendMail, but I'm afraid it's too far over my head to figure out. So after 2 days, I'm giving up. Got stuck on connecting to the server.

Mike375 - I'm trying your code as above but am also getting stuck with an error code 0x80040217, 'the server was not available'

I do know my server requires authentication and this might be where I get stuck with the vbSendEmail. I'm not sure whether the above error code is the same think or not.

Any ideas?
 
Thanks for both of your suggestions.

HiTechCoach, I tried the vbSendMail, but I'm afraid it's too far over my head to figure out. So after 2 days, I'm giving up. Got stuck on connecting to the server.

Mike375 - I'm trying your code as above but am also getting stuck with an error code 0x80040217, 'the server was not available'

I do know my server requires authentication and this might be where I get stuck with the vbSendEmail. I'm not sure whether the above error code is the same think or not.

Any ideas?

craigachan,

I have found that using vdSendmail is very equal to programming Outlook or CDO/SMTP.

I find vbSendMail to be much faster that using CDO/SMTP.

I will put together an Access example using vbSendMail. I will let you know as soon as it is ready.
 
Here is some code from a working Access database:

Install the DLL and register it

Set a reference in the VBA editor Tools > References to "SMTP Send Mail for VB6.0" (or browse for the vbsendmail.dll)

Code:
' **** SEND THE EMAIL MESSAGE DIRECTLY THROUGH THE SMTP SERVER  ****

          Set poSendmail = New vbSendMail.clsSendMail

         poSendmail.SMTPHost = "mail.yourdomainname.com"
         poSendmail.from = "user@yourdomainname.com"
         poSendmail.FromDisplayName = "HiTechCoach"
         poSendmail.Recipient = "user@otherdomainname.com"
         poSendmail.ReplyToAddress = "user@yourdomainname.com"
         poSendmail.Subject = "sending with vbSendMail"
         poSendmail.Message = "this is a test"
         
         poSendmail.Password = "password"
         poSendmail.Username = "username"
         poSendmail.UseAuthentication = True
         
         poSendmail.SMTPPort = 587   ' or 25 is the default
         
         poSendmail.Attachment = "C:\MyReport.PDF"
         
         
         If poSendmail.Connect Then

                 poSendmail.Send

                 poSendmail.Disconnect

                 Debug.Print "Success!"

         Else

               MsgBox "Not Successful"

        End If
         

         Set poSendmail = Nothing

' ************************************************************************

Note: I have not been able to successfully use a gmail account I think, you will need to enable pop3 before it will work.
 
A big THANKS HiTech, IT WORKED! I just had to tweek it a bit but this code worked great. Thanks again. I can send with an attachement. That's a big step for me.

I haven't been able to get the status and progress bars to work for vbsendmail. Any ideas?
 
this vbSendMail works great
thank you :)

can I keep it in the database directory, instead of puting it in the System32 one ?
is there an option to declare it in code, rather than register it in the system and in VB ?
 
this vbSendMail works great
thank you :)

can I keep it in the database directory, instead of puting it in the System32 one ?
is there an option to declare it in code, rather than register it in the system and in VB ?

You do not have to place the DLL file in the system32 folder. It can go anywhere.

AFAIK, you do have to register it.

I do have VBA code to register the DLL.

It is also possible to create a reference using VBA code.

I have not tried it with the vbsend DLL but it may be possible to:
1) place the DLL in the same folder with your front end database.
2) Using startup code you could register the DLL
3) Add a referenced to it.

I have not tried to use Late binding with this control. I do not know if that is possible.

I currently use a setup exe that installs the required DLLs for my front ends that must be registered.
 
thanks

I guess I'll have to create an installer for my application sooner or later, so it can also take care for registering the DLL in the system.
already started to look for free installer. any recomendation ?
 
thanks.
is Inno also free for commercial use ?

here is what I need for my installation:
1. two different style of install:
- for stand alone user, or for main network user (include BE db)
- for networked user (no BE db), and point for the network to the BE db and write this in the registery (now I do it in the FE db on first run)

2. not overwriting the BE db, if exist

3. writing data to registery (I already saw it can)

4. registering DLL in the system

5. create folders and put some data in them, under the installation folder


do I need to reregister the DLL in Access if it's installed in different folder on each machine, or do registering it in the System is enough ?
 
thanks.
is Inno also free for commercial use ?

here is what I need for my installation:
1. two different style of install:
- for stand alone user, or for main network user (include BE db)
- for networked user (no BE db), and point for the network to the BE db and write this in the registery (now I do it in the FE db on first run)

2. not overwriting the BE db, if exist

3. writing data to registery (I already saw it can)

4. registering DLL in the system

5. create folders and put some data in them, under the installation folder


do I need to reregister the DLL in Access if it's installed in different folder on each machine, or do registering it in the System is enough ?


Note: Since you have hijacked an old thread you are lucky to have got any response to you posts.. I only responded since it was related to the original topic.

You are really getting off topic and outside the scope of this old thread.

To help keep this site organized, please post these questions about creating an installer in a new post in the appropriate forum.
 
sorry. I moved my question to the General forum.

now back to topic.

the vbSendMail works great but when I receive the mail I get all kind of funny characters (I'm using Hebrew). even the name of the attached file is wrong.
is this because of vbSendMail or something I need to change on the receivng client system?
 

Users who are viewing this thread

Back
Top Bottom