0

I have this error when I try to send a mail Error

when I'm in debug mode the code works fine

when I try the program after publishing it gives me this error

    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

EDIT: add part of code which I entered in the Module

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

I tried with various registry changes and restoring the office but it doesn't work

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
GuidoM.
  • 27
  • 4

1 Answers1

1

Automating Outlook from a web application or service is not really a good idea. If you deal with Office365 or Exchange accounts you may consider using the Graph API or EWS, see Send email messages by using EWS in Exchange for more information.

Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45