使用 VB.Net 通过 Outlook 发送电子邮件时出现 COMException 未处理错误

发布于 2024-10-26 07:45:12 字数 843 浏览 0 评论 0原文

我有以下代码来向人们发送电子邮件,其详细信息是从数据库中检索的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-11-02 07:45:13

我相信您每次循环都需要一条新的 Outlook 消息:

Dim oMsg As Outlook._MailItem 
Dim objOL As Outlook.Application 
objOL = New Outlook.Application() 

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 = objOL.CreateItem(Outlook.OlItemType.olMailItem) 

    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() 

    oMsg = Nothing 
End While 
con.Close() 
objOL = Nothing 

I believe you need a new outlook message each time through loop:

Dim oMsg As Outlook._MailItem 
Dim objOL As Outlook.Application 
objOL = New Outlook.Application() 

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 = objOL.CreateItem(Outlook.OlItemType.olMailItem) 

    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() 

    oMsg = Nothing 
End While 
con.Close() 
objOL = Nothing 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文