473,513 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sending Mail



Hi. I have the following code in a module which sends email via Outlook.

Option Compare Database
Option Explicit

' Declare module level variables
Dim mOutlookApp As Outlook.Application
Dim mNameSpace As Outlook.NameSpace
Dim mFolder As mapiFolder
Dim mItem As MailItem
Dim fSuccess As Boolean

' Module contains only 2 methods:
' 1) GetOutlook()
' 2) SendMessage()
'
Public Function GetOutlook() As Boolean
' The GetOutlook() function sets the Outlook Application
' and Namespase objects and opens MS Outlook
On Error Resume Next

' Assume success
fSuccess = True

Set mOutlookApp = GetObject("", "Outlook.application")

' If Outlook is NOT Open, then there will be an error.
' Attempt to open Outlook
If Err.Number > 0 Then
Err.Clear
Set mOutlookApp = CreateObject("Outlook.application")
If Err.Number > 0 Then
MsgBox "Could not create Outlook object", vbCritical
fSuccess = False
Exit Function
End If
End If

' If we've made it this far, we have an Outlook App Object
' Now, set the NameSpace object to MAPI Namespace
Set mNameSpace = mOutlookApp.GetNamespace("MAPI")

If Err.Number > 0 Then
MsgBox "Could not create NameSpace object", vbCritical
fSuccess = False
Exit Function
End If

' Return the Success Flag as the value of GetOutlook()
GetOutlook = fSuccess
End Function
Public Function SendMessage() As Boolean
' The SendMessage() function reads user entered values and
' actually sends the message.

On Error Resume Next
Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String
Dim StrBody As String
Dim dlgopenfile As FileDialog

strSubject = Forms!frmsendmail!TxtSubject
strRecip = Forms!frmsendmail!TxtRecipient
StrBody = Forms!frmsendmail!TxtBody
strAttachment = dlgopenfile.SelectedItems(0)
strAttachment = Forms!frmsendmail!TxtAttachment
' Any amount of validation could be done at this point, but
' at a minimum, you need to verify that the user supplied an
' Email address for a recipient.
If Len(strRecip) = 0 Then
strMsg = "You must designate a recipient."
FormattedMsgBox strMsg, vbExclamation, "Error"
Exit Function
ElseIf Len(strSubject) = 0 Then
strMsg = "Your message must have a subject."
FormattedMsgBox strMsg, vbExclamation, "Error"
Forms!frmsendmail!TxtSubject.SetFocus
Exit Function
ElseIf Len(StrBody) = 0 Then
strMsg = "Your message must have some text in the body."
FormattedMsgBox strMsg, vbExclamation, "Error"
Forms!frmsendmail!TxtBody.SetFocus
Exit Function
End If
' Assume success
fSuccess = True
' Here's where the real Outlook Automation takes place
If GetOutlook = True Then
Set mItem = mOutlookApp.CreateItem(olMailItem)
mItem.Recipients.Add strRecip
mItem.Subject = strSubject
mItem.Body = StrBody
mItem.Attachments = strAttachment
' This code allows for 1 attachment, but with slight
' modification, you could provide for multiple files.
If Len(strAttachment) > 0 Then
mItem.Attachments.Add strAttachment
End If
mItem.Send
End If

' Release resources
Set mOutlookApp = Nothing
Set mNameSpace = Nothing

If Err.Number > 0 Then fSuccess = False

SendMessage = fSuccess

End Function
' -- End Code Here -->
This is the way I call the Modules from a command button:

If GetOutlook = False Then
MsgBox "Outlook is closed", vbOKOnly
Call GetOutlook
MsgBox "Outlook is open", vbOKOnly
End If
Call SendMessage
If SendMessage = True Then
FormattedMsgBox "The Message was sent successfully."
End If
DoCmd.Close
End Sub

Everything works with the exception of three problems:

1.) When I click the Send Mail Button on the form, the code sends TWO
identical emails to the address specified.

2.) I cannot figure out how to add attachments that I choose from the
openfile dialog. I can choose files but I do not see them listed in the
txtattachments dialog and they do not get sent with the email.

3.) Iam using Office System Outlook and I get annoying security warnings
when I send the E-Mails. Is there a way to suppress the warnings?

Thank you for your assistance

Colin

P.S.

If you recognize this code, could you please tell me who wrote it so
that I can credit them properly?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #1
11 2830
I figured out another method from Microsoft and it solves all my
problems except that I still get two identical email messages going to
the same address. This is happening because the code is looping around
the sendmessage methiod, but I cannot figure out why.

Thanks

Colin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #2
what was the method??
---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.693 / Virus Database: 454 - Release Date: 31/05/2004
Nov 13 '05 #3
you're calling sendMessage() twice
Call SendMessage
If SendMessage = True Then
FormattedMsgBox "The Message was sent successfully."
End If

change it to
dim blnSent as boolean
blnSent = sendmessage()
if (blnSent) then
FormattedMsgBox "The Message was sent successfully."
End If

ColinWard <je*********@hotmail.com> wrote in message news:<40**********************@news.newsgroups.ws> ...
I figured out another method from Microsoft and it solves all my
problems except that I still get two identical email messages going to
the same address. This is happening because the code is looping around
the sendmessage methiod, but I cannot figure out why.

Thanks

Colin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #4
That worked perfectly!! Thanks Roger

Colin

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #5

Well it seems jumped the gun a wee bit. Yesyerday I thought I had
figured out my attachment problem but today I was testing and although I
can select files and see them listed on my form, they are not being
attached to my email.
Here is the module that composes and actually sends the E-Mail. Am I
somehow attaching the attachments wrong?
Public Function SendMessage() As Boolean
' The SendMessage() function reads user entered values and
' actually sends the message.

On Error Resume Next
Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String
Dim StrBody As String
Dim dlgopenfile As FileDialog

strSubject = Forms!frmsendmail!TxtSubject
strRecip = Forms!frmsendmail!TxtRecipient
StrBody = Forms!frmsendmail!TxtBody
strAttachment = Forms!frmsendmail!LstAttachment
' Any amount of validation could be done at this point, but
' at a minimum, you need to verify that the user supplied an
' Email address for a recipient.
If Len(strRecip) = 0 Then
strMsg = "You must designate a recipient."
FormattedMsgBox strMsg, vbExclamation, "Error"
Exit Function
ElseIf Len(strSubject) = 0 Then
strMsg = "Your message must have a subject."
FormattedMsgBox strMsg, vbExclamation, "Error"
Forms!frmsendmail!TxtSubject.SetFocus
Exit Function
ElseIf Len(StrBody) = 0 Then
strMsg = "Your message must have some text in the body."
FormattedMsgBox strMsg, vbExclamation, "Error"
Forms!frmsendmail!TxtBody.SetFocus
Exit Function
End If
' Assume success
fSuccess = True
' Here's where the real Outlook Automation takes place
If GetOutlook = True Then
Set mItem = mOutlookApp.CreateItem(olMailItem)
mItem.Recipients.Add strRecip
mItem.Subject = strSubject
mItem.Body = StrBody
mItem.Attachments.Add (strAttachment)
mItem.Send
End If
' Release resources
Set mOutlookApp = Nothing
Set mNameSpace = Nothing

If Err.Number > 0 Then fSuccess = False

SendMessage = fSuccess

End Function
' -- End Code Here -->

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #6
Hi again.

It appears I am still having problems sending mail. Below is the code
that actually composes the message and sends the email. Everything works
except for the attachments. The attachments are chosen from the openfile
dialog and they display in a listbox on a form. This works, but the
files themselves are not being attached to the mail. strattachment is
always null. I also want to send mutiple attachments.

Public Function SendMessage() As Boolean
' The SendMessage() function reads user entered values and
' actually sends the message.

21090 On Error Resume Next
Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String
Dim StrBody As String
Dim dlgopenfile As FileDialog
21100 strSubject = Forms!frmsendmail!TxtSubject
21110 strRecip = Forms!frmsendmail!TxtRecipient
21120 StrBody = Forms!frmsendmail!TxtBody
21130 strAttachment = Forms!frmsendmail!TxtAttachments
' Any amount of validation could be done at this point, but
' at a minimum, you need to verify that the user supplied an
' Email address for a recipient.
21140 If Len(strRecip) = 0 Then
21150 strMsg = "You must designate a recipient."
21160 FormattedMsgBox strMsg, vbExclamation, "Error"
21170 Exit Function
21180 ElseIf Len(strSubject) = 0 Then
21190 strMsg = "Your message must have a subject."
21200 FormattedMsgBox strMsg, vbExclamation, "Error"
21210 Forms!frmsendmail!TxtSubject.SetFocus
21220 Exit Function
21230 ElseIf Len(StrBody) = 0 Then
21240 strMsg = "Your message must have some text in the body."
21250 FormattedMsgBox strMsg, vbExclamation, "Error"
21260 Forms!frmsendmail!TxtBody.SetFocus
21270 Exit Function
21280 End If
' Assume success
21290 fSuccess = True
' Here's where the real Outlook Automation takes place
21300 If GetOutlook = True Then
21310 Set mItem = mOutlookApp.CreateItem(olMailItem)
21320 mItem.Recipients.Add strRecip
21330 mItem.Subject = strSubject
21340 mItem.Body = StrBody
21350 mItem.Attachments.AddstrAttachment
21360 mItem.Send
21370 End If
' Release resources
21380 Set mOutlookApp = Nothing
21390 Set mNameSpace = Nothing

21400 If Err.Number > 0 Then fSuccess = False

21410 SendMessage = fSuccess

End Function
' -- End Code Here -->

Thank you

Colin

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #7


Hi again.

Everything works as intended now except that I can only send one
attachment. How would I modify the module so that I could send multiple
files? I have posted the module so that it is easier for you to see what
it is I am trying to do.

--Code Start--
Option Compare Database
Option Explicit

' Declare module level variables
Dim mOutlookApp As Outlook.Application
Dim mNameSpace As Outlook.NameSpace
Dim mFolder As mapiFolder
Dim mItem As MailItem
Dim fSuccess As Boolean
' Module contains only 2 methods:
' 1) GetOutlook()
' 2) SendMessage()
'
Public Function GetOutlook() As Boolean
' The GetOutlook() function sets the Outlook Application
' and Namespase objects and opens MS Outlook
On Error Resume Next

' Assume success
fSuccess = True

Set mOutlookApp = GetObject("", "Outlook.application")

' If Outlook is NOT Open, then there will be an error.
' Attempt to open Outlook
If Err.Number > 0 Then
Err.Clear
Set mOutlookApp = CreateObject("Outlook.application")
If Err.Number > 0 Then
MsgBox "Could not create Outlook object", vbCritical
fSuccess = False
Exit Function
End If
End If

' If we've made it this far, we have an Outlook App Object
' Now, set the NameSpace object to MAPI Namespace
Set mNameSpace = mOutlookApp.GetNamespace("MAPI")

If Err.Number > 0 Then
MsgBox "Could not create NameSpace object", vbCritical
fSuccess = False
Exit Function
End If

' Return the Success Flag as the value of GetOutlook()
GetOutlook = fSuccess
End Function
Public Function SendMessage() As Boolean
' The SendMessage() function reads user entered values and
' actually sends the message.

On Error Resume Next
Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String
Dim StrBody As String
Dim dlgopenfile As FileDialog
strSubject = Forms!frmsendmail!TxtSubject
strRecip = Forms!frmsendmail!TxtRecipient
StrBody = Forms!frmsendmail!TxtBody
strAttachment = Forms!frmsendmail!TxtAttachment
' Any amount of validation could be done at this point, but
' at a minimum, you need to verify that the user supplied an
' Email address for a recipient.
If Len(strSubject) = 0 Then
strMsg = "Your message must have a subject."
FormattedMsgBox strMsg, vbExclamation, "Error"
Forms!frmsendmail!TxtSubject.SetFocus
Exit Function
ElseIf Len(StrBody) = 0 Then
strMsg = "Your message must have some text in the body."
FormattedMsgBox strMsg, vbExclamation, "Error"
Forms!frmsendmail!TxtBody.SetFocus
Exit Function
End If
' Assume success
fSuccess = True
' Here's where the real Outlook Automation takes place
If GetOutlook = True Then
Set mItem = mOutlookApp.CreateItem(olMailItem)
mItem.Recipients.Add strRecip
mItem.Subject = strSubject
mItem.Body = StrBody
this is what needs to be modified(I think!)--> ' This code allows for
1 attachment, but with slight
' modification, you could provide for multiple files.
If Len(strAttachment) > 0 Then
mItem.Attachments.Add strAttachment
End If
mItem.Send
End If
' Release resources
Set mOutlookApp = Nothing
Set mNameSpace = Nothing

If Err.Number > 0 Then fSuccess = False

SendMessage = fSuccess

End Function
' -- End Code Here -->


thank you

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #8
Modify the lines you specified...

' modification, you could provide for multiple files.
If Len(strAttachment) > 0 Then
mItem.Attachments.Add strAttachment
End If

you could do any number of things...

wrap the section in a Do...Loop and prompt the user for more...

do until intReply=vbno
mItem.Attachments.Add strAttachment
intReply=Msgbox("Add more attachments?",vbyesno)
if intReply = vbyes then
'--prompt user for next file.
'--You could use something like the FileOpen API from the API
section at mvps.org
strAttachment= GetOpenFile()... 'or whatever the function is
called!
loop
Nov 13 '05 #9
Thanks for your advice Pieter. However, it doesn't work properly because
it simply sends multiple copies of the last attachment the user chose.
so the loop is working but it is only looping around the one file.

The way it works now is this:

1) The user clicks the send E-Mail button which opens a form which I
designed which has recipient, subject, body and attachment fields.

2) The recipient email is automatically filled in by the program.

3) the user fills in the subject and body fields which are required.

4) the user can choose to click the Add attachment button or not. If
they click it, then,

5) the windows file browser window opens and the user can
choose the file they want to attach.

6) The user clicks the send mail button. The message is then composed by
the SendMessage function and sent.

sorry if this redundant. I just want to make sure that you understand
how I see the process working.

Thanks pieter

Colin




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #10
ColinWard <je*********@hotmail.com> wrote in message news:<41**********************@news.newsgroups.ws> ...
Thanks for your advice Pieter. However, it doesn't work properly because
it simply sends multiple copies of the last attachment the user chose.
so the loop is working but it is only looping around the one file.

The way it works now is this:

1) The user clicks the send E-Mail button which opens a form which I
designed which has recipient, subject, body and attachment fields.

2) The recipient email is automatically filled in by the program.

3) the user fills in the subject and body fields which are required.

4) the user can choose to click the Add attachment button or not. If
they click it, then,

5) the windows file browser window opens and the user can
choose the file they want to attach.

6) The user clicks the send mail button. The message is then composed by
the SendMessage function and sent.

sorry if this redundant. I just want to make sure that you understand
how I see the process working.

Thanks pieter

Colin


Okay, so the ONLY issue here is how to control what attachments are
added? Braindead it's so easy. Create a variable to hold the name of
the attachment file. use the OpenFile API from the API section of
www.mvps.org. (it's the first article in the API's section). stuff
the return value from the GetOpenFile function into the string
variable. pass that result to the

olMsg.Attachments.Add(strAttachment)

part of your message. Since you're only calling this once or calling
the Add stuff once per attachment, you can just keep adding them.
Does that solve the problem?
Oh, and be sure to destroy all your variables once you're done with
them so you don't have some stupid global attachment variable that's
keeping its value after the routine is run. Make everything as local
as possible.
Nov 13 '05 #11
ColinWard <je*********@hotmail.com> wrote in message news:<41**********************@news.newsgroups.ws> ...
Thanks for your advice Pieter. However, it doesn't work properly because
it simply sends multiple copies of the last attachment the user chose.
so the loop is working but it is only looping around the one file.

The way it works now is this:

1) The user clicks the send E-Mail button which opens a form which I
designed which has recipient, subject, body and attachment fields.

2) The recipient email is automatically filled in by the program.

3) the user fills in the subject and body fields which are required.

4) the user can choose to click the Add attachment button or not. If
they click it, then,

5) the windows file browser window opens and the user can
choose the file they want to attach.

6) The user clicks the send mail button. The message is then composed by
the SendMessage function and sent.

sorry if this redundant. I just want to make sure that you understand
how I see the process working.

Thanks pieter

Colin


Okay, so the ONLY issue here is how to control what attachments are
added? Braindead it's so easy. Create a variable to hold the name of
the attachment file. use the OpenFile API from the API section of
www.mvps.org. (it's the first article in the API's section). stuff
the return value from the GetOpenFile function into the string
variable. pass that result to the

olMsg.Attachments.Add(strAttachment)

part of your message. Since you're only calling this once or calling
the Add stuff once per attachment, you can just keep adding them.
Does that solve the problem?
Oh, and be sure to destroy all your variables once you're done with
them so you don't have some stupid global attachment variable that's
keeping its value after the routine is run. Make everything as local
as possible.
Nov 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
522
by: praba kar | last post by:
Dear All, I have doubt regarding mail sending smtplib module. The below code is I used to send a mail. ########################################## import email.Message import email.Utils...
10
4944
by: Stuart Mueller | last post by:
I have an exchange server, that I sometimes use to perform mail shots to clients on our database, these can be upwards of 1000 at a time. As we don't want different clients to see who we are...
2
9207
by: Mr. x | last post by:
Hello, I am sending emails with Hebrew contents. When receiving emails - I cannot see the Hebrew characters (it is not outlook express configuration, because when receiving emails from friends -...
3
6806
by: VB Programmer | last post by:
I have an ASPX page where I send out emails through my mail server mail.MyDomain.com. When I send emails to MyName@MyDomain.com it sends PERFECTLY. When I try sending an email to any other address...
3
57586
by: HoustonComputerGuy | last post by:
I am working on getting my web applications moved to .Net 2.0 and am having some problems with System.Net.Mail. I get the following error when sending the mail: System.Net.Mail.SmtpException was...
1
8154
by: Eric Sheu | last post by:
Greetings, I have been searching the web like mad for a solution to my SMTP problem. I am using Windows Server 2003 and ASP.NET 2.0 w/ C# to send out e-mails from a web site I have created to...
2
10129
by: HK | last post by:
In VB.NET, I'm getting the exception "failure sending mail". I'm running VS 2005 on XP Home. This is a new install on a new PC. I've never had email problems with VS 2003, and there I could...
3
1966
by: mfleet1973 | last post by:
Hello Again. I have a program that sends e-mails as follows: Try Dim mail As New MailMessage mail.To = "me@comp.com" mail.From = "me@comp.com mail.Subject = "Test" mail.Body = "Testing123"
9
3434
by: JoeP | last post by:
Hi All, How can I find the reason for such an error: Failure sending mail. Some Code... oMailMessage.IsBodyHtml = False oMailMessage.Body = cEmailBody Dim oSMTP As New SmtpClient...
4
2380
by: =?Utf-8?B?R3V5IENvaGVu?= | last post by:
Hi all I use: Dim message As New MailMessage(txtTo.Text, txtFrom.Text, txtSubject.Text, txtBody.Text) Dim emailClient As New SmtpClient(txtSMTPServer.Text) emailClient.Send(message) And its...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.