使用 VB.Net 通过 Outlook 发送电子邮件时出现 COMException 未处理错误
我有以下代码来向人们发送电子邮件,其详细信息是从数据库中检索的:
Dim oMsg As Outlook._MailItem
Dim objOL As Outlook.Application
objOL = New Outlook.Application()
oMsg = objOL.CreateItem(Outlook.OlItemType.olMailItem)
con = New OleDbConnection("provider=SQLOLEDB;data source=pc;initial catalog=DB1;integrated security=SSPI")
cmd = New OleDbCommand("select column1, column2, column3, column4 from table1 where <condition>", con)
con.Open()
r = cmd.ExecuteReader
While r.Read
oMsg.Subject = "Subject"
oMsg.Body = "Hello " & r.Item(0) & vbLf & vbCr & "How are " & r.Item(1) & " and" & r.Item(2) & vbLf & vbCr & " ? "
oMsg.To = r.Item(3).ToString
oMsg.Send()
End While
con.Close()
oMsg = Nothing
objOL = Nothing
问题是,在发送第一封电子邮件后,它给我一个 COMException 未处理的错误,指出该项目已被移动或删除。这里有什么问题吗?
I have the following code to send emails to people, whose details are retrieved from a database:
Dim oMsg As Outlook._MailItem
Dim objOL As Outlook.Application
objOL = New Outlook.Application()
oMsg = objOL.CreateItem(Outlook.OlItemType.olMailItem)
con = New OleDbConnection("provider=SQLOLEDB;data source=pc;initial catalog=DB1;integrated security=SSPI")
cmd = New OleDbCommand("select column1, column2, column3, column4 from table1 where <condition>", con)
con.Open()
r = cmd.ExecuteReader
While r.Read
oMsg.Subject = "Subject"
oMsg.Body = "Hello " & r.Item(0) & vbLf & vbCr & "How are " & r.Item(1) & " and" & r.Item(2) & vbLf & vbCr & " ? "
oMsg.To = r.Item(3).ToString
oMsg.Send()
End While
con.Close()
oMsg = Nothing
objOL = Nothing
The problem is that after it sends the first email, it gives me an COMException unhandled error, stating that the item has been moved or deleted. What is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您每次循环都需要一条新的 Outlook 消息:
I believe you need a new outlook message each time through loop: