本文介绍了发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨。我在一个通过Outlook发送电子邮件的模块中有以下代码。 选项比较数据库 选项明确 ''声明模块级变量 Dim mOutlookApp As Outlook.Application Dim mNameSpace As Outlook.NameSpace Dim mFolder as mapiFolder Dim mItem As MailItem Dim fSuccess As Boolean ''模块只包含2种方法: ' '1)GetOutlook() ''2)SendMessage() '' 公共函数GetOutlook()As Boolean ''GetOutlook()函数设置Outlook应用程序 ''和Namespase对象并打开MS Outlook On Error Resume Next ''假设成功 fSuccess = True 设置mOutlookApp = GetObject(""," Outlook.application") ''如果Outlook未打开,则会出现错误。 ''尝试打开Outlook 如果Err.Number> 0然后 Err.Clear 设置mOutlookApp = CreateObject(" Outlook.application") 如果Err.Number> 0然后 MsgBox无法创建Outlook对象,vbCritical fSuccess = False 退出函数 结束如果 结束如果 ''如果我们已经做到这一点,我们有一个Outlook应用程序对象 ''现在,将NameSpace对象设置为MAPI命名空间 设置mNameSpace = mOutlookApp.GetNamespace(" MAPI") 如果Err.Number> 0然后 MsgBox无法创建NameSpace对象,vbCritical fSuccess = False 退出函数 结束如果 ''将成功标志返回为GetOutlook的值() GetOutlook = fSuccess 结束函数 公共函数SendMessage()As Boolean ''SendMessage()函数读取用户输入的值,并且 ''实际发送消息。 /> On Error Resume Next Dim strRecip As String Dim strSubject As String Dim strMsg As String Dim strAttachment As String Dim StrBody作为字符串 Dim dlgopenfile作为FileDialog strSubject =表格!frmsendmail!TxtSubject strRecip =表格!frmsendmail!TxtRecipient StrBody =表格!frmsendmail!TxtBody strAttachment = dlgopenfile.SelectedItems(0 ) strAttachment = Forms!frmsendmail!TxtAttachment ''此时可以进行任何数量的验证,但至少需要 '',您需要验证用户是否提供了 ''电子邮件地址对于收件人。 如果Len(strRecip)= 0则 strMsg ="您必须指定收件人。 FormattedMsgBox strMsg ,vbExclamation,错误 退出函数 ElseIf Len(strSubject)= 0然后 strMsg ="你的消息必须有一个主题。 FormattedMsgBox strMsg,vbExclamation,"错误" Forms!frmsendmail!TxtSubject.SetFocus 退出函数 ElseIf Len(StrBody)= 0然后 strMsg ="你的消息必须在正文中有一些文字。 FormattedMsgBox strMsg,vbExclamation," ;错误 表格!frmsendmail!TxtBody.SetFocus 退出功能 结束如果 ''假设成功 fSuccess = True ''这里'真正的Outlook自动化发生的地方 如果GetOutlook = True那么 设置mItem = mOutlookApp.CreateItem(olMailItem) mItem.Recipients。添加strRecip mItem.Subject = strSubject mItem.Body = StrBody mItem.Attachments = strAttachment ' '这段代码允许1个附件,但稍微修改一下 ',你可以提供多个文件。 如果Len(strAttachment)> 0然后 mItem.Attachments.Add strAttachment 结束如果 mItem.Send 结束如果 ''发布资源 设置mOutlookApp = Nothing 设置mNameSpace = Nothing 如果Err.Number> 0然后fSuccess = False SendMessage = fSuccess 结束功能 '' - 此处结束代码 - - > 这是我从命令按钮调用模块的方式: 如果GetOutlook = False那么 MsgBox Outlook已关闭,vbOKOnly 调用GetOutlook MsgBoxOutlook已打开,vbOKOnly 结束如果 调用SendMessage 如果SendMessage = True则 FormattedMsgBox"消息已成功发送。 结束如果 DoCmd.Close 结束子 除三个问题外,一切正常: 1.)当我单击表单上的发送邮件按钮时,代码会向指定的地址发送两封相同的电子邮件。 2.)I无法弄清楚如何添加我从 openfile对话框中选择的附件。我可以选择文件,但我没有看到它们列在 txtattachments对话框中,并且它们不会随电子邮件一起发送。 3.)Iam使用当我发送电子邮件时,Office System Outlook和我收到恼人的安全警告 。有没有办法压制警告? 感谢您的帮助 Colin PS 如果您认识到这段代码,请告诉我是谁编写的,所以 我可以正确地记下它们吗? ***通过开发人员指南 http://www.developersdex.com *** 不要只是参加USENET ......获得奖励! 解决方案 你两次调用sendMessage() 调用SendMessage 如果SendMessage = True则 FormattedMsgBox"消息发送成功。 结束如果 将其更改为 dim blnSent as boolean blnSent = sendmessage() if(blnSent)then FormattedMsgBox" The Message was successfully sent。 结束如果 ColinWard< je ********* @ hotmail.com>在消息新闻中写道:< 40 ********************** @ news.newsgroups.ws> ...Hi. I have the following code in a module which sends email via Outlook.Option Compare DatabaseOption Explicit'' Declare module level variablesDim mOutlookApp As Outlook.ApplicationDim mNameSpace As Outlook.NameSpaceDim mFolder As mapiFolderDim mItem As MailItemDim 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 OutlookOn Error Resume Next'' Assume successfSuccess = TrueSet mOutlookApp = GetObject("", "Outlook.application")'' If Outlook is NOT Open, then there will be an error.'' Attempt to open OutlookIf Err.Number > 0 ThenErr.ClearSet mOutlookApp = CreateObject("Outlook.application")If Err.Number > 0 ThenMsgBox "Could not create Outlook object", vbCriticalfSuccess = FalseExit FunctionEnd IfEnd If'' If we''ve made it this far, we have an Outlook App Object'' Now, set the NameSpace object to MAPI NamespaceSet mNameSpace = mOutlookApp.GetNamespace("MAPI")If Err.Number > 0 ThenMsgBox "Could not create NameSpace object", vbCriticalfSuccess = FalseExit FunctionEnd If'' Return the Success Flag as the value of GetOutlook()GetOutlook = fSuccessEnd FunctionPublic Function SendMessage() As Boolean'' The SendMessage() function reads user entered values and'' actually sends the message.On Error Resume NextDim strRecip As StringDim strSubject As StringDim strMsg As StringDim strAttachment As StringDim StrBody As StringDim dlgopenfile As FileDialogstrSubject = Forms!frmsendmail!TxtSubjectstrRecip = Forms!frmsendmail!TxtRecipientStrBody = Forms!frmsendmail!TxtBodystrAttachment = 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 ThenstrMsg = "You must designate a recipient."FormattedMsgBox strMsg, vbExclamation, "Error"Exit FunctionElseIf Len(strSubject) = 0 ThenstrMsg = "Your message must have a subject."FormattedMsgBox strMsg, vbExclamation, "Error"Forms!frmsendmail!TxtSubject.SetFocusExit FunctionElseIf Len(StrBody) = 0 ThenstrMsg = "Your message must have some text in the body."FormattedMsgBox strMsg, vbExclamation, "Error"Forms!frmsendmail!TxtBody.SetFocusExit FunctionEnd If'' Assume successfSuccess = True'' Here''s where the real Outlook Automation takes placeIf GetOutlook = True ThenSet mItem = mOutlookApp.CreateItem(olMailItem)mItem.Recipients.Add strRecipmItem.Subject = strSubjectmItem.Body = StrBodymItem.Attachments = strAttachment'' This code allows for 1 attachment, but with slight'' modification, you could provide for multiple files.If Len(strAttachment) > 0 ThenmItem.Attachments.Add strAttachmentEnd IfmItem.SendEnd If'' Release resourcesSet mOutlookApp = NothingSet mNameSpace = NothingIf Err.Number > 0 Then fSuccess = FalseSendMessage = fSuccessEnd Function'' -- End Code Here -->This is the way I call the Modules from a command button:If GetOutlook = False ThenMsgBox "Outlook is closed", vbOKOnlyCall GetOutlookMsgBox "Outlook is open", vbOKOnlyEnd IfCall SendMessageIf SendMessage = True ThenFormattedMsgBox "The Message was sent successfully."End IfDoCmd.CloseEnd SubEverything works with the exception of three problems:1.) When I click the Send Mail Button on the form, the code sends TWOidentical emails to the address specified.2.) I cannot figure out how to add attachments that I choose from theopenfile dialog. I can choose files but I do not see them listed in thetxtattachments dialog and they do not get sent with the email.3.) Iam using Office System Outlook and I get annoying security warningswhen I send the E-Mails. Is there a way to suppress the warnings?Thank you for your assistanceColinP.S.If you recognize this code, could you please tell me who wrote it sothat I can credit them properly?*** Sent via Developersdex http://www.developersdex.com ***Don''t just participate in USENET...get rewarded for it! 解决方案 这篇关于发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 00:46