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