0 votes

I am using VB.net.
when i want to send an email with

Dim result As ISendMessageResult
result = smtp.SendMessage(email)

and for example because of a wrong emailadress, sending fails.
The returnvalue is NOTHING instead of a regular value.

and because of NOTHING, i can't handle any Errors because i don't get any
Values back.

How can i detect what has been the reason why sending failt.

Please help

Here is my extracted code:

Dim lngBytesSent As Long, ResponseText As String 
', I As Integer, wert As String, response As SmtpResponse

builder.LoadHtml("MSG-Filename")
builder.Subject = "Test"
builder.From.Add(New MailBox("Sender@domain.de", "Sender@domain.de"))
builder.[To].Add(New MailBox("rec1@domain.de", "rec1@domain.de"))
builder.[To].Add(New MailBox("rec2@domain.de", "rec2@domainxxx.de"))
Dim email As IMail = builder.Create()

Using smtp As New Smtp()
    Try
        smtp.AllowPartialSending = True
        smtp.Connect(mSMTPServerName, mSMTPServerPort)    
        ' or ConnectSSL for SSL
        If mSMTPAccountName = "" Then
        Else
            smtp.UseBestLogin(mSMTPAccountName, mSMTPAuthPWD)
        End If
    Catch ex As Exception
        smtp.Close()
        Exit Sub
    End Try

    Dim result As ISendMessageResult
    Try
        result = smtp.SendMessage(email) 
        'with a wrong reciepient an error is thrown

        If result.Status = 0 Then
            MsgBox("result.Status = 0")
        Else
            If result.Status = 1 Then      
                'Partial success status: message was succesfully 
                'sent to some, but not to all receipients
                If result.FromRejected Then
                    MsgBox("result.FromRejected")
                Else
                    If result.RejectedRecipients.Count <> 0 Then
                        MsgBox("result.RejectedRecipients.Count <> 0")
                    Else
                        SendStatus = 4  'Socket closed
                        ResponseText = Get_ResponseText(result.AllResponses)
                    End If
                End If
            Else   'keine email ging raus, Failure status: message sending failed

            End If
        End If
    Catch e As SmtpResponseException
        'error handling never reaches this section WHY !!!!
        MsgBox(e.Response)  '
        If IsNothing(result) Then
            MsgBox("result.Status = Nothing")
        Else
            If result.Status = 2 Then   
            'keine email ging raus, Failure status: message sending failed
                MsgBox("result.Status = 2")
            Else
                MsgBox("result.Status = 1")
            End If
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
        If IsNothing(result) Then
            MsgBox("result.Status = Nothing")
        Else
            If result.Status = 2 Then   
            'keine email ging raus, Failure status: message sending failed
                MsgBox("result.Status = 2")
            Else
                MsgBox("result.Status = 1")
            End If
        End If
    End Try
    smtp.Close()
End Using

with a wrong receipient i always end up in the 'Catch ex As Exception' section. and result is NOTHING.

Thank you for your immediate response.

by (200 points)
edited by
Could you please show your code?
First: consider using enums, such as SendMessageStatus.Success, SendMessageStatus.Failure instead of integers.

1 Answer

0 votes

Unless you encounter serious SMTP protocol error - in which case a SmtpResponseException or ServerException is thrown, return value of Smtp.SendMessage method is always non-null

[Edit]
After looking at your code:

  1. If you are ending in 'Catch ex As Exception' block, it most likely mean that your server disconnects when it encounters incorrect recipient - this violates SMTP protocol obviously, but some servers act in such way. The expcetion is a ServerException in such case.

    You can turn on logging to get more info:
    https://www.limilabs.com/blog/logging-in-mail-dll

  2. In 'Catch ex As Exception' block, 'result' variable can not be set - this is how .NET and VB.NET works.

  3. You can examine ex.Data["SendMessageResult"] entry to get ISendMessageResult in such case. This entry is filled only if a problem happened after Mail.dll tried to send a message (e.g. during RSET command)

by (301k points)
edited by
...