发送邮件 [英] Sending Mail

查看:96
本文介绍了发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




嗨。我在一个通过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 ......获得奖励!

I想出了微软的另一种方法,它解决了我所有的问题,除了我仍然得到两个完全相同的地址的电子邮件。这种情况正在发生,因为代码循环使用了发送消息,但我无法弄明白为什么。


谢谢


Colin


***通过开发人员指南
http: //www.developersdex.com ***

不要只是参加USENET ......获得奖励!


方法是什么?

---

如果(a)这封电子邮件,请立即告知我们(通过电话或回复邮件)

包含病毒

(b)您不是预定的收件人

(c)您认为此电子邮件是垃圾邮件。

我们已尽最大努力确保

以上都不适用。谢谢

由AVG反病毒系统检查( http:// www .grisoft.com)

版本:6.0.693 /病毒库:454 - 发布日期:31/05/2004


< blockquote>你两次调用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> ...

我想出了微软的另一种方法,它解决了我的所有问题,除了我仍然得到两个完全相同的地址的电子邮件。发生这种情况是因为代码循环播放了sendmessage方法,但我无法弄清楚原因。
谢谢

科林

***通过开发人员指南 http://www.developersdex.com 发送***





Hi. I have the following code in a module which sends email via Outlook.

Option Compare Database
Option Explicit

'' Declare module level variables
Dim mOutlookApp As Outlook.Application
Dim mNameSpace As Outlook.NameSpace
Dim mFolder As mapiFolder
Dim mItem As MailItem
Dim 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 Outlook
On Error Resume Next

'' Assume success
fSuccess = True

Set mOutlookApp = GetObject("", "Outlook.application")

'' If Outlook is NOT Open, then there will be an error.
'' Attempt to open Outlook
If Err.Number > 0 Then
Err.Clear
Set mOutlookApp = CreateObject("Outlook.application")
If Err.Number > 0 Then
MsgBox "Could not create Outlook object", vbCritical
fSuccess = False
Exit Function
End If
End If

'' If we''ve made it this far, we have an Outlook App Object
'' Now, set the NameSpace object to MAPI Namespace
Set mNameSpace = mOutlookApp.GetNamespace("MAPI")

If Err.Number > 0 Then
MsgBox "Could not create NameSpace object", vbCritical
fSuccess = False
Exit Function
End If

'' Return the Success Flag as the value of GetOutlook()
GetOutlook = fSuccess
End Function
Public Function SendMessage() As Boolean
'' The SendMessage() function reads user entered values and
'' actually sends the message.

On Error Resume Next
Dim strRecip As String
Dim strSubject As String
Dim strMsg As String
Dim strAttachment As String
Dim StrBody As String
Dim dlgopenfile As FileDialog

strSubject = Forms!frmsendmail!TxtSubject
strRecip = Forms!frmsendmail!TxtRecipient
StrBody = Forms!frmsendmail!TxtBody
strAttachment = 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 Then
strMsg = "You must designate a recipient."
FormattedMsgBox strMsg, vbExclamation, "Error"
Exit Function
ElseIf Len(strSubject) = 0 Then
strMsg = "Your message must have a subject."
FormattedMsgBox strMsg, vbExclamation, "Error"
Forms!frmsendmail!TxtSubject.SetFocus
Exit Function
ElseIf Len(StrBody) = 0 Then
strMsg = "Your message must have some text in the body."
FormattedMsgBox strMsg, vbExclamation, "Error"
Forms!frmsendmail!TxtBody.SetFocus
Exit Function
End If
'' Assume success
fSuccess = True
'' Here''s where the real Outlook Automation takes place
If GetOutlook = True Then
Set mItem = mOutlookApp.CreateItem(olMailItem)
mItem.Recipients.Add strRecip
mItem.Subject = strSubject
mItem.Body = StrBody
mItem.Attachments = strAttachment
'' This code allows for 1 attachment, but with slight
'' modification, you could provide for multiple files.
If Len(strAttachment) > 0 Then
mItem.Attachments.Add strAttachment
End If
mItem.Send
End If

'' Release resources
Set mOutlookApp = Nothing
Set mNameSpace = Nothing

If Err.Number > 0 Then fSuccess = False

SendMessage = fSuccess

End Function
'' -- End Code Here -->
This is the way I call the Modules from a command button:

If GetOutlook = False Then
MsgBox "Outlook is closed", vbOKOnly
Call GetOutlook
MsgBox "Outlook is open", vbOKOnly
End If
Call SendMessage
If SendMessage = True Then
FormattedMsgBox "The Message was sent successfully."
End If
DoCmd.Close
End Sub

Everything works with the exception of three problems:

1.) When I click the Send Mail Button on the form, the code sends TWO
identical emails to the address specified.

2.) I cannot figure out how to add attachments that I choose from the
openfile dialog. I can choose files but I do not see them listed in the
txtattachments dialog and they do not get sent with the email.

3.) Iam using Office System Outlook and I get annoying security warnings
when I send the E-Mails. Is there a way to suppress the warnings?

Thank you for your assistance

Colin

P.S.

If you recognize this code, could you please tell me who wrote it so
that I can credit them properly?

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

解决方案

I figured out another method from Microsoft and it solves all my
problems except that I still get two identical email messages going to
the same address. This is happening because the code is looping around
the sendmessage methiod, but I cannot figure out why.

Thanks

Colin

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


what was the method??
---
Please immediately let us know (by phone or return email) if (a) this email
contains a virus
(b) you are not the intended recipient
(c) you consider this email to be spam.
We have done our utmost to make sure that
none of the above are applicable. THANK YOU
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.693 / Virus Database: 454 - Release Date: 31/05/2004


you''re calling sendMessage() twice
Call SendMessage
If SendMessage = True Then
FormattedMsgBox "The Message was sent successfully."
End If

change it to
dim blnSent as boolean
blnSent = sendmessage()
if (blnSent) then
FormattedMsgBox "The Message was sent successfully."
End If

ColinWard <je*********@hotmail.com> wrote in message news:<40**********************@news.newsgroups.ws> ...

I figured out another method from Microsoft and it solves all my
problems except that I still get two identical email messages going to
the same address. This is happening because the code is looping around
the sendmessage methiod, but I cannot figure out why.

Thanks

Colin

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



这篇关于发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆