You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

发布后使用Microsoft.Office.Interop.Outlook.Application报错求助

发送邮件时发布版报错(调试模式正常)求助

我在尝试发送邮件时遇到错误(错误截图)。程序在调试模式下运行正常,但发布后执行就会出现该错误。我已经尝试过修改注册表和恢复Office,但问题仍未解决,希望得到帮助。

按钮点击事件代码

Private Sub Btn_Mail_Click(sender As Object, e As EventArgs) Handles Btn_Mail.Click
    Dim itemChecked As Object
    Dim mailto As String
    If CheckedListBox1.CheckedItems.Count > 0 Then
        For Each itemChecked In CheckedListBox1.CheckedItems
            mailto = mailto & itemChecked.item("mail").ToString & ","
        Next
        sendEmailViaOutlook("abc@abc.it", mailto, "XXXX", "Xxxx")
    Else
        MsgBox("No Mail selected")
    End If
End Sub

模块中的发送邮件代码

Imports outlook = Microsoft.Office.Interop.Outlook

Public Sub sendEmailViaOutlook(ByVal sFromAddress As String, ByVal sToAddress As String, ByVal sSubject As String, ByVal sBody As String)

    Try

        Dim app As outlook.Application = New outlook.Application

        Dim newMail As outlook.MailItem = app.CreateItem(outlook.OlItemType.olMailItem)

        If Not String.IsNullOrWhiteSpace(sToAddress) Then
            Dim arrAddTos As String() = sToAddress.Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)

            For Each strAddr As String In arrAddTos
                If Not String.IsNullOrWhiteSpace(strAddr) AndAlso strAddr.IndexOf("@"c) <> -1 Then
                    newMail.Recipients.Add(strAddr.Trim())
                Else
                    Throw New Exception("Bad to-address: " & sToAddress)
                End If
            Next
        Else
            Throw New Exception("Must specify to-address")
        End If


        If Not newMail.Recipients.ResolveAll() Then
            Throw New Exception("Failed to resolve all recipients: " & sToAddress & ";")
        End If


        If Not String.IsNullOrWhiteSpace(sSubject) Then newMail.Subject = sSubject
        If Not String.IsNullOrWhiteSpace(sBody) Then newMail.Body = sBody
        Dim accounts As outlook.Accounts = app.Session.Accounts
        Dim acc As outlook.Account = Nothing

        For Each account As outlook.Account In accounts

            If account.SmtpAddress.Equals(sFromAddress, StringComparison.CurrentCultureIgnoreCase) Then
                acc = account
                Exit For
            End If
        Next

        If acc IsNot Nothing Then
            newMail.SendUsingAccount = acc
            newMail.Send()
            MsgBox("Email Inviata")
        Else
            Throw New Exception("Account does not exist in Outlook: " & sFromAddress)
        End If

    Catch ex As Exception
        MsgBox("ERROR: Failed to send mail: " & ex.Message)
    End Try
End Sub

内容的提问来源于stack exchange,提问作者GuidoM.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 06:27:23