Attaching a CSV file to an Outlook Express Email from Access

G

Guest

Hi - I've been transmitting orders to a supplier by using SendObject with a
string of text that contains the order info. Now, the supplier would like an
attached comma-delimited file instead. But SendObject doesn't seem to allow
me to send a table comma delimited. I can use TransferText to make an ASCII
comma-delimited text file, but then I would have to attach it manually to the
email. Is there a way to preserve my one-button order process and send the
data comma-delimited?

Thanks
 
J

John Nurick

This should get you started. It uses the Microsoft CDO Library for
Windows 2000.


Sub SendMailCDO(ToWhom As String, FromWhom As String, _
Subject As String, Body As String, _
Optional AttachmentPath As String = "")

Const cdoDefaults = -1
Const cdoIIS = 1
Const cdoOutlookExpress = 2

Dim Message As Object 'CDO.Message

Set Message = CreateObject("CDO.Message")

With Message
'Use default configuration from system
'until I learn otherwise
.Configuration.Load -1

.To = ToWhom
If Len(FromWhom) > 0 Then
.From = FromWhom
End If
.Subject = Subject
.TextBody = Body
If Len(AttachmentPath) > 0 Then
.AddAttachment AttachmentPath
End If

.Send
End With
End Sub
 
G

Guest

Thanks, John, for that info. Just one question - will this work in Access
2003 under XP; I should have said that in my original question that I have XP
and not 2000.

Thanks again,
dave
 
J

John Nurick

I haven't tested it seriously, but it should work in just about all
recent versions of all Office applications providing you're using
Windows 2000 or later. However, depending on your email and security
setup it may trigger a security warning about a program trying to send
emails behind your back.
 
G

Guest

Hi John - The PC this will run on has Windows XP Home.

I typed in the routine and tested it from the immediate window using the
command line:
Call SendMailCDO("(e-mail address removed)","(e-mail address removed)","test of
send","see attachment","c:\phonesrc\www\v8page\v8orders\TDOrder.txt")

It bombs on the send command with the message:

"The message could not be sent to the SMTP server. The transport error code
was 0x80040217. The server response was not available"

Any thoughts on this? Is it a result of not having Win2000? I do have the
CDO DLL in the system.

Thanks again,
dave
 
S

SurendranSNV via AccessMonster.com

Hi,

Why your code has not worked is for any internet mail messaging, SMTP
server/service should be available. CDO uses IIS SMTP service which is an
optional component in Windows operating system. If it is not installed in a
machine, then code written for CDO in that machine will fail.

When a message is sent through mail programs like Exchange or Outlook, they
use the SMTP service built in their programs.

I have used CDO in automating to send mail messages programatically in Access.


Initially I had some difficulty in successfully sending mail using CDO.

Later after understanding to some extent how CDO works, I was able to send
messages successfully.

In this regard I would like to share my experiences. What I have given below
is just a glimpse of various resources I have come across.

What is CDO ?

In windows operating system, there is a component CDO called as Collaborative
Data Objects. CDO has various versions which has been evolved during the past
several years.

This component provides the developer of an application using VB, VBA, ASP,
VB Script etc. to provide simple messaging services like sending and
receiving mail messages. CDO is designed in such a way that there are no user
interface elements.

Before 2000, CDO was available as OLE Messaging. Then version 1.1 was
released with Exchange server. To use with Windows NT server, CDONTS was
released in service pack 4.

CDONTS provides a subset of the functionality of CDO.It is intended to
provide reliable,fast,scalable messaging for applications that don't need to
make use of the more advanced calendaring and workflow capabilities that
Exchange Server and CDO provide.

After 2003, CDO has become part of the Windows operating system 2000, XP and
2003 which is installed as CDOSYS.DLL along with OS.

Collaboration Data Objects for Windows 2000/XP
CDO for Microsoft® Windows® 2000/XP (CDOSYS), which implements the 2.0
version of the Collaboration Data Objects API specification, is a Component
Object Model (COM) component designed to simplify writing programs that
create or manipulate Internet messages. CDOSYS is one in a suite of
collaborative COM components referred to collectively as CDO.

CDOSYS is implemented in CDOSYS.DLL.

Latest versions of Exchange Server and Outlook has versions 1.2. When
Exchange server is installed, it replaces CDOSYS.DLL with CDOEX Version 1.2.
Apart from having all features of CDOSYS.DLL, CDOEX is having additional
features to be able to utilise the Exchange Server capabilities like
Calendaring, collaborating and workflow functionality. So code written for
CDOSYS.DLL will work in CDOEX with out any need for change.

In order to use CDO, a SMTP server or service should be available to forward
the message.When a message is sent through mail programs like Exchange or
Outlook, they use the SMTP service built in their programs.

Similarly CDOSYS is utilising the IIS SMTP service for sending and receiving
messages.

How to install IIS SMTP Service ?

Installing the SMTP service for Windows 2000/XP is a quick and
straightforward process. Because the service is included as part of the
Microsoft® Internet Information Services (IIS) installation, you can
instruct the system to add it to your machine either when you first install
the operating system or at a later date. If IIS SMTP service is not installed,
simply open the Control Panel, launch Add/Remove Programs, and click on the
Add/Remove Windows Components button. In the Windows Components Wizard,
select the IIS item and click on the Details button. A new dialog box will
open listing all the services of IIS, including SMTP. Enable the
corresponding checkbox and the Windows Installer will add the files and
registry keys needed by the service.


For Windows NT user, you can install it from Option Pack 4.0 of Windows NT.
After installed, you can start it from "Control Panel->Administrative Tools-
Internet Information Service

After installation of IIS SMTP in 2000/XP,open windows explorer and check
whether you are able to see the following main and sub folders.

=======================
C:\Inetpub\mailroot\

Badmail

Drop

Mailbox

Pickup

Queue

Route

SortTemp
=======================

If you see the above, then IIS with SMTP is successfully installed.

After installed IIS SMTP Service, there is a directory named "Pickup" ("C:\
Inetpub\mailroot\Pickup"). IIS SMTP Service keeps on monitoring this folder,
once an email is saved in this directory, IIS SMTP Service will queue this
email and send it automatically in background.(This is analogus to Outlook
which keeps in Outbox first and then in Sent box).

==================================================================
How does IIS SMTP Service manage undeliverable emails? It saves those emails
to specified folder
("C:\Inetpub\mailroot\Badmail"). If you want to receive those undeliverable
emails, you can add your email address in "Control Panel->Administrative
Tools->Control Panel->Administrative Tools->Internet Information Service-
Default SMTP Virtual Server->Properties->Messages"

If an email couldn't be sent to specified email address in first trial, IIS
SMTP Service would move it to "Queue" folder and IIS SMTP Service would keep
on retrying later until expired. You can set expiration timeout of IIS SMTP
Service in "Control Panel->Administrative Tools->Control Panel-
Administrative Tools->Internet Information Service->Default SMTP Virtual
Server->Properties->Delivery"

The last two features i.e. undeliverable mails and expiration time out is yet
to be explored by me. I have just given to impress that there are various
configuration possibilities. There are many more configuration possibilities
which can be explored based on various scenarios in production setting.
==================================================================
We as Access programmers are just concerned with simply sending a message
with or without attachment/s in VBA code without any user intervention and
also without any security alerts which we usually get when we use Outlook.

For sending messages, again there are two options.

1. Using local SMTP Service installed in the machine.

2. Using some remote SMTP server with or without authentication.
(Sometimes you need to send email using another server. It may be required by
your company, or your ISP may be blocking the SMTP port in the local machine).


For first method i.e. using local SMTP, I used the following code.
I used a form with record source to a table having fields
msgFrom,msgTo,msgSubject,msgText,FileAttachment
and a command button cmdSendMail.
=====================================================
Private Sub cmdSendMail_Click()
Dim objMessage As Object
Set objMessage = CreateObject("CDO.Message")
objMessage.From = Me.msgFrom
objMessage.To = Me.msgTo
objMessage.Subject = Me.msgSubject
objMessage.TextBody = Me.msgText
If Not IsNull(Me.FileAttachment) Or Not Me.FileAttachment = vbNullString
Then
objMessage.AddAttachment Me.txtFileAttachment
End If

objMessage.Send

MsgBox "Mail is sent"
End Sub
=======================================================
This code works simply as long as IIS SMTP installed and needs no
configuration at all.
================================================
Second method.
Sending a text email using authentication against a remote SMTP server.

More and more administrators are restricting access to their servers to
control spam or limit which users may utilize the server. This example shows
you how to use basic authentication, the most commonly used authentication
method, when the SMTP server you are using requires it.

This code is slightly more complex but not very difficult to understand or
work with.

I used a remote SMTP server where I have a user name and password.This
remote SMTP server is being used by my client for internet messaging.

When I used for first time, the code failed.

Then I read somewhere that CDO uses outlook express(OE) to get some
configuration
information before sending mail.For this to work, atleast one account in
Outlook Express
should have been configured.Then I just created one account in Outlook
Express.
This account is my personal account given by a local ISP here.

After creating this account in OE, remote SMTP method started working.
But still I am not sure whether working of remote SMTP server
is due to the creation of the account in OE.
================================================================

Private Sub cmdSendMail_Click()
Dim objMessage As Object
Set objMessage = CreateObject("CDO.Message")

objMessage.From = Me.msgFrom
objMessage.To = Me.msgTo
objMessage.Subject = Me.msgSubject
objMessage.TextBody = Me.msgText
If Not IsNull(Me.FileAttachment) Or Not Me.FileAttachment = vbNullString
Then
objMessage.AddAttachment Me.FileAttachment
End If

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


' "This is some sample message text.." & vbCrLf & "It was sent using SMTP
authentication."

'==This section provides the configuration information for the remote
SMTP server.

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

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.
myclientservername.com"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") =
cdoBasic

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") =
"(e-mail address removed)" 'your userid

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") =
"password of
Surendran" 'yourpassword

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to
establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")
= 60

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send
MsgBox "Mail is sent"
End Sub
==========================================================================
In the above code, text starting with "http://schemas ........" are actually
field
names in Configuration.Item object. Why CDO designers have given names
reflecting
web page URL is not known. May be while sending messges, SMTP is using the
above field names
to create some header information in the messages being sent ?

Why using CDO code instead of Outlook automation or SendMail in VBA.

1: It doesn't matter what Mail program you are using (It only uses the SMTP
server).
2: It doesn't matter what Office version you are using (97…2003)
3: You can send a sheet in the body of the mail (some mail programs can’t do
this)
4: You can send any file you like (Word, PDF, PowerPoint, TXT files,….)
5: No Outlook Security warning anymore, really great if you are sending a
lot of mail in a loop.

Apart from the above, imagine a scenario.

say you have developed an Access application for use
in a multiuser networking setting.

The front end application is installed in say 5 machines.

Also assume there are 10 employess Emp1 to Emp10 are using the application.

Now you have written code to send mail using outlook.

Now for the code to work, all the 10 employess mail id should be configured

in Outlook in each individual machine because any employee may log in any
machine

and try to send the mail from the application.

Suppose some employess leave and new employess join, just imagine the
work involved.

Also in Outlook, it is not possible to set the Sender Address programatically.


I will be happy if some one can clarify whether what I have said about
Outlook is correct.

My conclusion:

Considering the various factors, CDO is definitely advantageous due to its
simplicity and reliable way of sending mail.

Although there are a good number of resources about CDO, personally
I found the following two resources helpful to me.

VBScript To Send Email Using CDO
http://www.paulsadowski.com/WSH/cdo.htm

Chapter 21 and 22 of
Professional Active Server Pages 3.0
(Programmer to Programmer)
By Alex Homer and .....others
(By Wrox publication).
(I have understood what is CDO and how it works only from this book.
One need not be a web developer.
I am only an Access VBA programmer but still followed the book.
Any one with programming back ground can follow the
chapters I mentioned as above).


Other resources:

Manage Your Company's E-mail with the Windows 2000 SMTP Service
http://www.microsoft.com/mind/1299/smtp2000/smtp2000.asp

Where to acquire the CDO Libraries (all versions)
http://support.microsoft.com/kb/q171440/

Collaboration Data Objects Roadmap
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncdsys/html/cdo_roadmap.asp



Regards,
Surendran
Hi John - The PC this will run on has Windows XP Home.

I typed in the routine and tested it from the immediate window using the
command line:
Call SendMailCDO("(e-mail address removed)","(e-mail address removed)","test of
send","see attachment","c:\phonesrc\www\v8page\v8orders\TDOrder.txt")

It bombs on the send command with the message:

"The message could not be sent to the SMTP server. The transport error code
was 0x80040217. The server response was not available"

Any thoughts on this? Is it a result of not having Win2000? I do have the
CDO DLL in the system.

Thanks again,
dave
I haven't tested it seriously, but it should work in just about all
recent versions of all Office applications providing you're using
[quoted text clipped - 13 lines]
Please respond in the newgroup and not by email.
 

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