Sending e-mails from MS Access

Y

y770

I am sending e-mail messages from Access form using CDO object. Bellow is a
sample code:

Sub SendMail(....)
Dim Msg As New CDO.Message
Dim Conf As New CDO.Configuration

Set flds = Conf.Fields
flds(cdoSendUsingMethod) = cdoSendUsingPort
flds(cdoSMTPServer) = Server
flds(cdoSMTPServerPort) = 25
flds(cdoSMTPAuthenticate) = cdoAnonymous
flds.Update

Recipient = "..."
Subject = "..."
Body = "..."

With Msg
Set .Configuration = Conf
.To = Recipient
.From = myMailAdress
.Subject = Subject
.HTMLBody = Body
.Send
End With
Set Msg = Nothing
Set Conf = Nothing
end Sub

This code used to work fine. But now I need to wait 30 - 50 seconds the
message to be sent out. It also hold the execution of the code that follows
the call to this function.

Is there anything that I should be aware in the code? or is this issue with
our mail server?

Thanks for your help.
 
R

ryguy7272

Access Email: Find all files in directory.

you could try this... it simply searches the directory specified (put the
const at the top of the module) and then builds your mail message and
attaches each item...

enjoy... i think it does what you want...

dovholuk



'**************************** START COPY '****************************
Const PATH_TO_LOOK_IN As String = "C:\"

Function AttachFiles()

Dim i As Long
Dim mi As Object
Dim out As Object

Set out = CreateObject("outlook.application")
Set mi = out.createitem(0)

With Application.FileSearch
.LookIn = PATH_TO_LOOK_IN
.FileName = "*.*"
.Execute

For i = 1 To .FoundFiles.Count
mi.attachments.Add .FoundFiles(i)
Next i
End With

mi.display

Set out = Nothing
Set mi = Nothing

End Function
'**************************** END COPY '****************************


Also, here are some notes I have about using Access to send emails:
Report-mailing

I can send a text by means of code mentioned below, but can I send a report
by means of e-mail. The body should be a report. How I can send my report by
mail?
--
If CInt(Forms!Rapporten!List61) = 32 Then
Dim myMail As Outlook.MailItem
Dim myOutlApp As Outlook.Application
Set myOutlApp = New Outlook.Application
Set myMail = myOutlApp.CreateItem(olMailItem)
With myMail
..To = "(e-mail address removed)"
..CC = ""
..Subject = "Recuperations"
..Body = "This is a test."
..Send
End With
Set myMail = Nothing
Set myOutlApp = Nothing
End If

Well Alright Then

The code below takes your MSAccess Report and places it in an outlook email
in the message. It will come up after a signature if autosignature is turned
on, I do not believe there is a way around that, but this allows you to edit
the email and send to whomever, or to a predetermined mail address in a form.

DoCmd.OutputTo acOutputReport, "MyAccessReport", acFormatRTF,
"FullPathOfMyReport.rtf"

Dim wdApp As Word.Application
Dim doc As Word.Document
Dim Report As MailItem

Set wdApp = New Word.Application
Set doc = wdApp.Documents.Open("FullPathOfMyReport.rtf ")
Set Report = doc.MailEnvelope.Item

Report.To = [Forms]![frm_Request].[txtEmail]
Report.Subject = "MyEmailSubject"


Report.Save
strID = Report.EntryID
Set itm = Nothing
Set OL = CreateObject("Outlook.Application")
Set ns = OL.GetNamespace("MAPI")
Set theitem = ns.GetItemFromID(strID)
If Not theitem Is Nothing Then
theitem.Forward.Display
theitem.Delete

Finally, you may find this interesting...
Outlook Messages to Several Recipients:

How to use a recordset to send Outlook e-mail to multiple recipients in
Microsoft Access
View products that this article applies to.
Article ID : 318881
Last Review : October 11, 2004
Revision : 3.3
This article was previously published under Q318881
Advanced: Requires expert coding, interoperability, and multiuser skills.
SUMMARY
This article describes how to use Automation to send a Microsoft Outlook
e-mail message to multiple recipients whose addresses are listed in a
Microsoft Access table. This article also uses Access form controls to add
the message body, to fill in the Cc field, and to fill in the Subject field.
Additionally, this article describes a programmatic method that you can use
to include e-mail attachments and to resolve e-mail addresses against the
Outlook Address Book.
Back to the top
MORE INFORMATION
To create an Outlook session from Access and to use a recordset to send
e-mail messages to multiple recipients, follow these steps:
1. Create a text file that you can use as an attachment. Save the text file
as "TestAttachment.txt" in the root of drive C. For example, you can save the
text file as C:\TestAttachment.txt.
2. Create a new database that is named Db1.mdb.
3. Create a new table with the following information in Db1.mdb:
Table: tblMailingList
----------------------
Field Name: EmailAddress
Data Type: text

4. Type several valid e-mail addresses in the EmailAddress field.
5. Create a form with the following information:
Form: frmMail
----------------------
RecordSource: tblMailingList

Text box
---------------------------
Name: CCAddress
ControlSource: Unbound
Label Caption: CC Address

Text box
---------------------------
Name: Subject
ControlSource: Unbound
Label Caption: Subject

Text box
---------------------------
Name: MainText
ControlSource: Unbound
Label Caption: Message Body

6. Open the form in Form view, and then type sample data in the appropriate
boxes. Press ENTER after each entry, and then leave the form open when you
are finished.
7. Create a new module. Add the following code sample to the new module.
Option Compare Database
Option Explicit

Sub SendMessages(Optional AttachmentPath)

Dim MyDB As Database
Dim MyRS As Recordset
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim TheAddress As String

Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset("tblMailingList")
MyRS.MoveFirst

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")

Do Until MyRS.EOF
' Create the e-mail message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
TheAddress = MyRS![EmailAddress]

With objOutlookMsg
' Add the To recipients to the e-mail message.
Set objOutlookRecip = .Recipients.Add(TheAddress)
objOutlookRecip.Type = olTo

' Add the Cc recipients to the e-mail message.
If (IsNull(Forms!frmMail!CCAddress)) Then
Else
Set objOutlookRecip = .Recipients.Add(Forms!frmMail!CCAddress)
objOutlookRecip.Type = olCC
End If

' Set the Subject, the Body, and the Importance of the e-mail message.
.Subject = Forms!frmMail!Subject
.Body = Forms!frmMail!MainText
.Importance = olImportanceHigh 'High importance

'Add the attachment to the e-mail message.
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If

' Resolve the name of each Recipient.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
End If
Next
.Send
End With
MyRS.MoveNext
Loop
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub

8. In the Visual Basic Editor, click References on the Tools menu.
9. In the Available References list, click Microsoft Outlook 8.0 Object
Library, and then click OK.

Note You have to select the correct Microsoft Outlook Object Library. The
Microsoft Outlook Object Library that you select depends on the version of
Outlook that is installed on your computer. For example, if you have Office
Outlook 2003 installed on your computer, click Microsoft Outlook 11.0 Object
Library.
10. Press CTRL+G to open the Debug window.
11. To send the e-mail message with an attachment, type the following
command in the Debug window:
SendMessages "C:\TestAttachment.txt"
Press ENTER.

Alternatively, to send the e-mail message without an attachment, omit the
argument when calling the procedure. Type the following command in the Debug
window:
SendMessages
Press ENTER.
 
R

ryguy7272

Access Email: Find all files in directory.

you could try this... it simply searches the directory specified (put the
const at the top of the module) and then builds your mail message and
attaches each item...

enjoy... i think it does what you want...

dovholuk



'**************************** START COPY '****************************
Const PATH_TO_LOOK_IN As String = "C:\"

Function AttachFiles()

Dim i As Long
Dim mi As Object
Dim out As Object

Set out = CreateObject("outlook.application")
Set mi = out.createitem(0)

With Application.FileSearch
.LookIn = PATH_TO_LOOK_IN
.FileName = "*.*"
.Execute

For i = 1 To .FoundFiles.Count
mi.attachments.Add .FoundFiles(i)
Next i
End With

mi.display

Set out = Nothing
Set mi = Nothing

End Function
'**************************** END COPY '****************************


Also, here are some notes I have about using Access to send emails:
Report-mailing

I can send a text by means of code mentioned below, but can I send a report
by means of e-mail. The body should be a report. How I can send my report by
mail?
--
If CInt(Forms!Rapporten!List61) = 32 Then
Dim myMail As Outlook.MailItem
Dim myOutlApp As Outlook.Application
Set myOutlApp = New Outlook.Application
Set myMail = myOutlApp.CreateItem(olMailItem)
With myMail
..To = "(e-mail address removed)"
..CC = ""
..Subject = "Recuperations"
..Body = "This is a test."
..Send
End With
Set myMail = Nothing
Set myOutlApp = Nothing
End If

Well Alright Then

The code below takes your MSAccess Report and places it in an outlook email
in the message. It will come up after a signature if autosignature is turned
on, I do not believe there is a way around that, but this allows you to edit
the email and send to whomever, or to a predetermined mail address in a form.

DoCmd.OutputTo acOutputReport, "MyAccessReport", acFormatRTF,
"FullPathOfMyReport.rtf"

Dim wdApp As Word.Application
Dim doc As Word.Document
Dim Report As MailItem

Set wdApp = New Word.Application
Set doc = wdApp.Documents.Open("FullPathOfMyReport.rtf ")
Set Report = doc.MailEnvelope.Item

Report.To = [Forms]![frm_Request].[txtEmail]
Report.Subject = "MyEmailSubject"


Report.Save
strID = Report.EntryID
Set itm = Nothing
Set OL = CreateObject("Outlook.Application")
Set ns = OL.GetNamespace("MAPI")
Set theitem = ns.GetItemFromID(strID)
If Not theitem Is Nothing Then
theitem.Forward.Display
theitem.Delete

Finally, you may find this interesting...
Outlook Messages to Several Recipients:

How to use a recordset to send Outlook e-mail to multiple recipients in
Microsoft Access
View products that this article applies to.
Article ID : 318881
Last Review : October 11, 2004
Revision : 3.3
This article was previously published under Q318881
Advanced: Requires expert coding, interoperability, and multiuser skills.
SUMMARY
This article describes how to use Automation to send a Microsoft Outlook
e-mail message to multiple recipients whose addresses are listed in a
Microsoft Access table. This article also uses Access form controls to add
the message body, to fill in the Cc field, and to fill in the Subject field.
Additionally, this article describes a programmatic method that you can use
to include e-mail attachments and to resolve e-mail addresses against the
Outlook Address Book.
Back to the top
MORE INFORMATION
To create an Outlook session from Access and to use a recordset to send
e-mail messages to multiple recipients, follow these steps:
1. Create a text file that you can use as an attachment. Save the text file
as "TestAttachment.txt" in the root of drive C. For example, you can save the
text file as C:\TestAttachment.txt.
2. Create a new database that is named Db1.mdb.
3. Create a new table with the following information in Db1.mdb:
Table: tblMailingList
----------------------
Field Name: EmailAddress
Data Type: text

4. Type several valid e-mail addresses in the EmailAddress field.
5. Create a form with the following information:
Form: frmMail
----------------------
RecordSource: tblMailingList

Text box
---------------------------
Name: CCAddress
ControlSource: Unbound
Label Caption: CC Address

Text box
---------------------------
Name: Subject
ControlSource: Unbound
Label Caption: Subject

Text box
---------------------------
Name: MainText
ControlSource: Unbound
Label Caption: Message Body

6. Open the form in Form view, and then type sample data in the appropriate
boxes. Press ENTER after each entry, and then leave the form open when you
are finished.
7. Create a new module. Add the following code sample to the new module.
Option Compare Database
Option Explicit

Sub SendMessages(Optional AttachmentPath)

Dim MyDB As Database
Dim MyRS As Recordset
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim TheAddress As String

Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset("tblMailingList")
MyRS.MoveFirst

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")

Do Until MyRS.EOF
' Create the e-mail message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
TheAddress = MyRS![EmailAddress]

With objOutlookMsg
' Add the To recipients to the e-mail message.
Set objOutlookRecip = .Recipients.Add(TheAddress)
objOutlookRecip.Type = olTo

' Add the Cc recipients to the e-mail message.
If (IsNull(Forms!frmMail!CCAddress)) Then
Else
Set objOutlookRecip = .Recipients.Add(Forms!frmMail!CCAddress)
objOutlookRecip.Type = olCC
End If

' Set the Subject, the Body, and the Importance of the e-mail message.
.Subject = Forms!frmMail!Subject
.Body = Forms!frmMail!MainText
.Importance = olImportanceHigh 'High importance

'Add the attachment to the e-mail message.
If Not IsMissing(AttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If

' Resolve the name of each Recipient.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
End If
Next
.Send
End With
MyRS.MoveNext
Loop
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub

8. In the Visual Basic Editor, click References on the Tools menu.
9. In the Available References list, click Microsoft Outlook 8.0 Object
Library, and then click OK.

Note You have to select the correct Microsoft Outlook Object Library. The
Microsoft Outlook Object Library that you select depends on the version of
Outlook that is installed on your computer. For example, if you have Office
Outlook 2003 installed on your computer, click Microsoft Outlook 11.0 Object
Library.
10. Press CTRL+G to open the Debug window.
11. To send the e-mail message with an attachment, type the following
command in the Debug window:
SendMessages "C:\TestAttachment.txt"
Press ENTER.

Alternatively, to send the e-mail message without an attachment, omit the
argument when calling the procedure. Type the following command in the Debug
window:
SendMessages
Press ENTER.
 

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