Sie sind auf Seite 1von 6

MS Access send email (not from outlook or

user's email)
I know this question has been asked a few times in various context, but I have not
found a clear answer. I have email implemented for an access application using
outlook, but I'd like to move away from this. One of the purposes of the email is
to email a user his/or password if he forgot it. They can select their username for
the login screen, and if they click 'forgot password' and email is sent containing
their login information (to the email address associated with the user name).
The problem with this is that the email function as is sends an email with outlook
from the user's machine. So, users would be able to 'forgot password' other
usernames and view their own outlook outbox(sent items) to see the sensitive
information.
up vote 7
down vote
favorite
8

Is there a way to e-mail like php's mail function, sending mail from the server? I
would like the emails to be sent from the same email address
i.e(support@company.com), instead of from the user's outlook address after a
security prompt. If this is not possible, I am open to the idea of any other
workarounds.
I will also add that installing any software that would have to be installed on
every potential user's machine is not feasible.
Is this possible?
ms-access vba outlook
asked Jul 20 '12 at 12:49
shareimprove this question

edited Jul 20 '12 at 12:59

Scotch
2,202102137
Access really can't do what you want. It is a desktop database. Andrew
Barber Jul 20 '12 at 12:52
I'm open to the idea of work arounds: idea for an example workaround could
be: only send the 'forgot password' email if the outlook email for the
user('from') matches the email for the username in the database(to). Scotch
Jul 20 '12 at 12:57
I'm not really familiar with Access code, but if there's some function that
sends mail to an SMTP service then you should be able to use any SMTP
service you want instead of the user's local Outlook. David Jul 20 '12 at
12:57
users would be able to 'forgot password' other usernames and
view their own outlook outbox(sent items) to see the sensitive

sounds like a design issue that you're trying to solve by fixing


something else instead. JimmyPena Jul 22 '12 at 16:34
add a comment
information

4 Answers
active oldest votes
up vote 6
down vote Windows includes an object called Collaborative Data Objects or CDO. This
accepted object allows you to send emails using any SMTP server assuming that other
prerequisites are met (firewall open, ISP not blocking ports, account is configured
on the SMTP server, SMTP server allows relaying, etc).
Most of the examples I've found use late binding, which is preferred. In my testing
on XP it appeared that the correct library reference, if you prefer to use early
binding, is "Microsoft CDO for Windows 2000 Library".
It's important to know that any time you send email you will have to send it
through (or out of) some kind of email server. This means you will have to
authenticate with that email server and also usually means that you need to send
the email out using a "From" email address that exists on that very email server.
Here's some code using late binding:
Const
Const
Const
' Use
Const
' Use
Const

cdoSendUsingPickup = 1
cdoSendUsingPort = 2
cdoAnonymous = 0
basic (clear-text) authentication.
cdoBasic = 1
NTLM authentication
cdoNTLM = 2 'NTLM

Public Sub SendEmail()


Dim imsg As Object
Dim iconf As Object
Dim flds As Object
Dim schema As String
Set imsg = CreateObject("CDO.Message")
Set iconf = CreateObject("CDO.Configuration")
Set flds = iconf.Fields
' send one copy with SMTP server (with autentication)
schema = "http://schemas.microsoft.com/cdo/configuration/"
flds.Item(schema & "sendusing") = cdoSendUsingPort
flds.Item(schema & "smtpserver") = "mail.myserver.com"
flds.Item(schema & "smtpserverport") = 25
flds.Item(schema & "smtpauthenticate") = cdoBasic
flds.Item(schema & "sendusername") = "email@email.com"
flds.Item(schema & "sendpassword") = "password"
flds.Item(schema & "smtpusessl") = False
flds.Update
With imsg
.To = "email@email.com"

.From = "email@email.com"
.Subject = "Test Send"
.HTMLBody = "Test"
'.Sender = "Sender"
'.Organization = "My Company"
'.ReplyTo = "address@mycompany.com"
Set .Configuration = iconf
.Send
End With
Set iconf = Nothing
Set imsg = Nothing
Set flds = Nothing
End Sub

answered Jul 20 '12 at 13:58


shareimprove this answer
HK1
6,20274483
"From" email address that exists on that very email server - this does not
work for me in my locale, and from my reading I understand that you must
use the server provided by your ISP. This is to prevent spam. Fionnuala Jul
20 '12 at 14:01
That just all depends on your ISP. Some only allow port 25 to be used to talk
to their own SMTP server and some do not have this limitation. Some of your
big cable providers do this but many of your smaller ISP's do not have this
limitation. For example, neither Windstream or CenturyLink have this
limitation. You can also get around this by using an alternate port (assuming
your SMTP server is exposing an alternate port) since most ISP's only impose
this limitation on port 25. As an example of this, you can probably use GMail
as your SMTP server - GMail uses 587 or 465 with SSL. HK1 Jul 20 '12 at
14:20
add a comment

up
vote This works for me in MS Access 2010 / Windows 7
11
dow sMailServer = "myISPsmtp" 'Not just any old smtp
n sMailFromAddress = "me"
sMailToAddress = "me"
vote
Set ObjMessage = CreateObject("CDO.Message")
sToAddress = sMailToAddress
sSubject = "Subject"
sBody = "MailBody"
ObjMessage.Subject = sSubject
ObjMessage.From = sMailFromAddress
ObjMessage.To = sToAddress

'ObjMessage.cc = sCCAddress
ObjMessage.TextBody = sBody
'ObjMessage.AddAttachment sMailAttachment
ObjMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/
configuration/sendusing") = 2
ObjMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/
configuration/smtpserver") = sMailServer
ObjMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/
configuration/smtpserverport") = 25
ObjMessage.Configuration.Fields.Update
ObjMessage.send

More info: http://msdn.microsoft.com/en-us/library/ms526318(v=exchg.10).aspx


answered Jul 20 '12 at 13:43
shareimprove this answer

edited Jul 20 '12 at 16:04

Fionnuala
76.9k665110
So would I just be setting sMailServer to my office's email server? and the
mailFromAddress to be the dummy account that I create? ('support@company.com')
and sMailTo to be a userName's email address from the databasE? Scotch Jul 20
'12 at 13:47
+1 I'm too slow ^_^ but this is a great method. I shall focus on one of my concerns
instead. You should not be storing un-encrypted passwords within your database,
and definately should not be sending that information via e-mail (even if to the
1
correct person). Instead you need to encrypt your passwords and also offer user's the
ability to reset their password rather than just sending it out. Matt Donnan Jul 20
'12 at 13:48
I am by no means sure which mail server will work for you, I had to use the server
supplied by my ISP, apparently this is a security measure to stop spam. Fionnuala
Jul 20 '12 at 13:48
As for from, I reckon pretty nearly anything will do. Fionnuala Jul 20 '12 at 13:50
Seems like this method lets you spoof about anything on my server. Just noticed that
2 there's an addAttachment method. Could that work with just a relative path to say, an
excel sheet? Scotch Jan 22 '13 at 23:17
show 5 more comments
up
vote I cannot add this to the comments because I do not have enough reputation, so please
0
don't axe me.
down
vote "Seems like this method lets you spoof about anything on my server. Just noticed that
there's an addAttachment method. Could that work with just a relative path to say, an
excel sheet? "
It works for me (Access 2010, Exchange 2010):
.AddAttachment ("URL HERE")

https://msdn.microsoft.com/en-us/library/ms526453(v=exchg.10).aspx
https://msdn.microsoft.com/en-us/library/ms526983(v=exchg.10).aspx
answered Jun 10 '15 at 18:27
shareimprove this answer
UpTide
608
add a comment
The following MS-Access VBA code works for smtp.office365.com. You DO indicate
smtpusessl=true, but you do NOT specify the port, otherwise you get error 5.7.57.
Sub SMPTTest2()
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "name@myaddress.com"
emailObj.To = "name@youraddress.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
'emailObj.AddAttachment "c:\windows\win.ini"
Set emailConfig = emailObj.Configuration

up
vote
0
dow
n
vote

emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/send
using") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtp
authenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtp
server") = "smtp.office365.com"
'Exclude the following line
'emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smt
pserverport") = 587
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtp
usessl") = True
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/send
username") = "name@myaddress.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/send
password") = "mypassword"
emailConfig.Fields.Update
emailObj.Send
If Err.Number = 0 Then MsgBox "Done"
End Sub

answered Feb 13 at 22:27


shareimprove this answer
pghcpa
548721

add a comment

Das könnte Ihnen auch gefallen