vba MS Access 发送电子邮件(不是来自 Outlook 或用户的电子邮件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11579730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS Access send email (not from outlook or user's email)
提问by Scotch
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).
我知道这个问题在不同的背景下被问过几次,但我没有找到明确的答案。我已经使用 Outlook 为访问应用程序实现了电子邮件,但我想摆脱这一点。电子邮件的目的之一是在用户忘记密码时通过电子邮件将其/或密码发送给用户。他们可以为登录屏幕选择他们的用户名,如果他们单击“忘记密码”,则会发送包含他们登录信息的电子邮件(发送到与用户名关联的电子邮件地址)。
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.
这样做的问题是电子邮件功能从用户的机器发送了一封带有 Outlook 的电子邮件。因此,用户将能够“忘记密码”其他用户名并查看自己的 Outlook 发件箱(已发送邮件)以查看敏感信息。
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.
有没有办法像php的邮件功能一样发邮件,从服务器发送邮件?我希望从相同的电子邮件地址 ie([email protected]) 发送电子邮件,而不是在安全提示后从用户的 Outlook 地址发送。如果这是不可能的,我愿意接受任何其他解决方法的想法。
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?
这可能吗?
回答by Fionnuala
This works for me in MS Access 2010 / Windows 7
这在 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
更多信息:http: //msdn.microsoft.com/en-us/library/ms526318(v=exchg.10).aspx
回答by HK1
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).
Windows 包括一个称为协作数据对象或 CDO 的对象。此对象允许您使用任何 SMTP 服务器发送电子邮件,前提是满足其他先决条件(防火墙打开、ISP 未阻止端口、在 SMTP 服务器上配置帐户、SMTP 服务器允许中继等)。
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".
我发现的大多数示例都使用后期绑定,这是首选。在我对 XP 的测试中,如果您更喜欢使用早期绑定,那么正确的库参考似乎是“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
回答by UpTide
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? "
“好像这种方法可以让你欺骗我服务器上的任何东西。只是注意到有一个 addAttachment 方法。这可以用相对路径说,一个 excel 表吗?“
It works for me (Access 2010, Exchange 2010):
它对我有用(Access 2010、Exchange 2010):
.AddAttachment ("URL HERE")
.AddAttachment ("URL HERE")
https://msdn.microsoft.com/en-us/library/ms526453(v=exchg.10).aspxhttps://msdn.microsoft.com/en-us/library/ms526983(v=exchg.10).aspx
https://msdn.microsoft.com/en-us/library/ms526453(v=exchg.10).aspx https://msdn.microsoft.com/en-us/library/ms526983(v=exchg.10) .aspx
回答by pghcpa
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.
以下 MS-Access VBA 代码适用于 smtp.office365.com。您确实指出 smtpusessl=true,但您没有指定端口,否则您会收到错误 5.7.57。
Sub SMPTTest2()
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "[email protected]"
emailObj.To = "[email protected]"
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") = "[email protected]"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
emailConfig.Fields.Update
emailObj.Send
If Err.Number = 0 Then MsgBox "Done"
End Sub
回答by Nicolas
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.
在我的公司,我使用了其他解决方案。我创建了一个带有 COM 类/对象的 C# 类库。COM 类可以在您的 Access 应用程序中实现,这样您就可以使用 C#(例如邮件)的所有优点,并且仍然在 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.
唯一的缺点是您必须在使用您的访问应用程序的所有计算机上注册您的类库 (DLL)。我已经使用在 Access 应用程序开始时执行的简单的 power-shell 脚本完成了这项工作。
A good start for A COM based library is here: https://www.codeproject.com/Articles/7859/Building-COM-Objects-in-C
基于 COM 的库的良好开端在这里: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.
如果您想了解更多有关它的信息,那么我很乐意为您提供帮助。