Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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([email protected]), 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?

like image 865
Scotch Avatar asked Jul 20 '12 12:07

Scotch


2 Answers

Windows includes an object called Collaborative Data Objects or CDO. This 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 cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0
' Use basic (clear-text) authentication.
Const cdoBasic = 1
' Use NTLM authentication
Const 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 protected]"
    flds.Item(schema & "sendpassword") = "password"
    flds.Item(schema & "smtpusessl") = False
    flds.Update

    With imsg
        .To = "[email protected]"
        .From = "[email protected]"
        .Subject = "Test Send"
        .HTMLBody = "Test"
        '.Sender = "Sender"
        '.Organization = "My Company"
        '.ReplyTo = "[email protected]"
        Set .Configuration = iconf
        .Send
    End With

    Set iconf = Nothing
    Set imsg = Nothing
    Set flds = Nothing
End Sub
like image 165
HK1 Avatar answered Oct 26 '22 06:10

HK1


This works for me in MS Access 2010 / Windows 7

sMailServer = "myISPsmtp" 'Not just any old smtp
sMailFromAddress = "me"
sMailToAddress = "me"

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

like image 41
Fionnuala Avatar answered Oct 26 '22 08:10

Fionnuala