Sending an e-mail via VBA with Outlook Exchange - ms-access

So for many years I've been using CDO to send e-mails via POP and have had no issues. However, we are going to be upgrading to an Outlook Exchange e-mail server and POP is no longer available, it also requires TLS which I've read isn't supported by CDO.
This is what I have been doing:
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "my smtpserver"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "email#email.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Update
End With
Set objEmail = CreateObject("CDO.Message")
Set objEmail.Configuration = cdoConfig
This has always worked in the past. I tried updating the smtp server to smtp.office365.com and the port to 587 but I get the following error:
The server rejected the sender address. The server response was: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [MWHPR08CA0030.namprd08.prod.outlook.com]
I'm currently using Access 2003, we will be upgrading to 2015 soon, but for now I need a solution that works in 2003.
Thanks.

Related

ms access module to add attachment from button

I am having the following code in a module, which it is send email with attachment to user.
Public Sub EmailToUser()
Dim mail As Object ' CDO.MESSAGE
Dim config As Object ' CDO.Configuration
Set mail = CreateObject("CDO.Message")
Set config = CreateObject("CDO.Configuration")
config.Fields(cdoSendUsingMethod).Value = cdoSendUsingPort
config.Fields(cdoSMTPServer).Value = "my smtp server"
config.Fields(cdoSMTPServerPort).Value = 465
config.Fields(cdoSMTPConnectionTimeout).Value = 10
config.Fields(cdoSMTPUseSSL).Value = "true"
config.Fields(cdoSMTPAuthenticate).Value = cdoBasic
config.Fields(cdoSendUserName).Value = "e=mail"
config.Fields(cdoSendPassword).Value = "password"
config.Fields.Update
Set mail.Configuration = config
With mail
.To = "e-mail"
.From = "e-mail"
.Subject = "subject"
.AddAttachment strPathReport & FileName '<== My question.
.Send
End With
Set config = Nothing
Set mail = Nothing
End Sub
I have a form with 8buttons and each button is send an email with an attachment.
Now, I have in my module 8 times the same code with different attachment.
Is it possible to have only one time the above code and from the button to add the attachment?
Thank you.
Basically you want to add parameters to the procedure. And then pass in the e-mail address, etc. as arguments.
This question should give you some ideas on how to do that: multiple argument subs vba

VB code to automatically send email in an ASP.NET app

I have a file in witch there is a mix of HTML, Javascript, and Visual Basic, it is actually a web site (when up and running).
On a click event, I'm able to reach a VB function (tested) but now, the email part.
Dim SmtpServer As New SmtpClient()
SmtpServer.UseDefaultCredentials = true
SmtpServer.Port = 25
SmtpServer.Host = "12345.com"
Dim mail As New MailMessage()
mail.From = "si#csnavigateurs.qc.ca"
mail.To.Add("pelletiera77#csnavigateurs.qc.ca")
mail.Subject = "Email Sending"
mail.Body = "Testing the 1 and 2"
SmtpServer.Send(mail)
I was thinking it would work by itself but no, surely there is something to config out in order to make this work. It throws a server error right at Dim SmtpServer As New SmtpClient() So I guess I have to include something?
The error is:
500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.
The problem is here (from property is not string):
mail.From = "si#csnavigateurs.qc.ca"
I used this and worked:
mail.From = new System.Net.Mail.MailAddress(emailAddress, name);// Email-ID of Sender
If you do not like use name of sender you can do this:
mail.From = new System.Net.Mail.MailAddress(emailAddress);
Try this:
Try
Dim SmtpServer As New System.Net.Mail.SmtpClient()
SmtpServer.UseDefaultCredentials = true
SmtpServer.Port = 25
SmtpServer.Host = "csnavigateurs-qc-ca.mail.protection.outlook.com"
Dim mail As New MailMessage()
mail.From = "si#csnavigateurs.qc.ca"
mail.To.Add("pelletiera77#csnavigateurs.qc.ca")
mail.Subject = "Email Sending"
mail.Body = "Testing the 1 and 2"
SmtpServer.Send(mail)
Response.Write("Success")
Catch ex As Exception
Response.Write(ex.Message)
End Try
This should give you a clearer error message if it fails.

asp formmail auto responder

I'm very bad at coding
i have a asp formmail for subscribe email. I do get emails but I want to send a autoresponse saying "Thank you for subscription". Also the same for my contact page formmail.
Below is my current code
can you tell me how do I go about the same.
<%
dim sEmailContent
sEmailContent = sEmailContent & "Subscribe : " & Request.Form("subscribe") & vbCrLf
Response.Write(sEmailContent)
'call send_email("here#herringboneandsui.com",sEmailContent)
call send_email("here#herringboneandsui.com",sEmailContent)
function send_email(sToEmail,sEmailBody)
on error resume next
Dim ObjSendMail
Set ObjSendMail = CreateObject("CDO.Message")
'Configuration for remote SMTP server
'Network Send
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name of SMTP server
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="relay-hosting.secureserver.net"
'SMTP port
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'MaxESP SMTP servers require authentication
'Basic Authentication
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'SMTP username as configured in the control panel
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="bla#bla.com"
'SMTP user password as configured in the control panel
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="blabla"
ObjSendMail.Configuration.Fields.Update
'Configuration for email message
'Email To address
'ObjSendMail.To = "bla#gmail.com"
'ObjSendMail.To = "bla#mac.com"
ObjSendMail.To = sToEmail
'Email Subject
ObjSendMail.Subject = "Newsletter Subscription"
'Email From address
ObjSendMail.From = "bla#herringboneandsui.com"
'Email Body
ObjSendMail.TextBody = sEmailBody
ObjSendMail.Send
Set ObjSendMail = Nothing
response.Redirect("thank-you.html")
if err.number <> 0 then
Response.Write(err.Description)
end if
on error goto 0
end function
%>
move your function function send_email(sToEmail,sEmailBody) into separate file, let's say into "Emails.asp".
in contact page and in subscribe asp pages add following line to include your "Email.asp"
<!-- #include file="Emails.asp" -->
then call send_email as before

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(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?
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#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
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
I cannot add this to the comments because I do not have enough reputation, so please don't axe me.
"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
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
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
'Exclude the following line
'emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "name#myaddress.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
emailConfig.Fields.Update
emailObj.Send
If Err.Number = 0 Then MsgBox "Done"
End Sub
At my company I used a other solution. I have created a C# Class Library with COM classes / objects. COM classes can be implemented in your Access application and this way you can use all the advantages of C# (Mailing for example) and still use it (calling it) in Access.
The only disadvantage is that you have to register your Class Library (DLL) at all the computers who use your access application. I have done that with a simple power-shell script which executes at the start of the Access application.
A good start for A COM based library is here: https://www.codeproject.com/Articles/7859/Building-COM-Objects-in-C
If you would like some more information about it then I am always happy to help you.

Microsoft Access 2007 and 2010: "Run-Time Error '429': ActiveX component can't create object"

I am trying to fix a Microsoft Access database that was imported from Access 97 format to Access 2007 format (.mdb to .accdb). The import was successful and I was able to make the database fully functional from my machine. It is also fully functional from my coworker's machine. However, when taken to another building that is part of our organization, we cannot get the database to run. We know that part of the problem is within opening a connection to the web server that holds the central database (there are several copies of this Access database that consist of the same code, but different data is input into them and uploaded to this central database). Here is the code.
Public Function updateSqlServer(TransType As String, SqlCommand As Variant) As Boolean
Dim xmldom As New MSXML2.DOMDocument40
Dim xmlhttp As New MSXML2.ServerXMLHTTP40
Const SoapServer = "http://www.example.com/webservice.asp"
'setup the XMLHTTP object and POST envelope to SoapServer
toResolve = 5 * 1000
toConnect = 5 * 1000
toSend = 15 * 1000
toReceive = 15 * 1000
xmlhttp.setTimeouts toResolve, toConnect, toSend, toReceive
xmlhttp.Open "POST", SoapServer, False 'YIELDS Run-Time Error 429 on this line: xmlhttp.Open
xmlhttp.setRequestHeader "Man", POST & " " & SoapServer & " HTTP/1.1"
xmlhttp.setRequestHeader "MessageType", "CALL"
xmlhttp.setRequestHeader "Content-Type", "text/xml"
xmlhttp.send (SoapEnvelope)
'synchronous wait for response; HTTP status other than 200 (OK) is an error
If xmlhttp.Status = 200 Then
Set xmldom = xmlhttp.responseXML 'get response into XML DOM document
Debug.Print (xmldom.xml) 'write soap response to screen
updateSqlServer = True
Else
'handle error
Debug.Print ("Didn't Work")
Debug.Print ("status=" & xmlhttp.Status) 'write soap return code
Debug.Print ("" & xmlhttp.statusText) 'write status text
updateSqlServer = False
End If
Set xmlhttp = Nothing
Set xmldom = Nothing
End Function
Things we have tried to fix this problem:
1. Added all necessary references (Ex. Microsoft XML)
2. Editing the permissions of the file and the folders
3. Registering ActiveX Controls
4. Made sure all options that can be changed match on both working and non-working machines
This old and is not my code or design. I just have to fix it.
Any help will be appreciated.
A coworker of mine figured out the answer. Those variables are objects of MSXML4, and Windows 7 comes stock with MSXML 6.0, which is evidently not compatible with MSXML 4.
Make sure to check install packages before anything else.
You can either install MSXML 4 or change the variables to MSXML 6 by:
Dim xmldom As New MSXML2.DOMDocument6.0
instead of
Dim xmldom As New MSXML2.DOMDocument40