Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET sending using SmtpClient with Mandrill SMTP fails

We are facing a difficult problem sending mail via Mandrill SMTP silently fails when mailFromName in the below code contains "æ", "ø" or "å". The mail is send perfectly well when we use another SMTP.

Mail.ReplyToList.Add(New MailAddress(mailFromAddress, mailFromName, System.Text.Encoding.UTF8))

We use the .NET smtpClient class.

Dim SmtpClient As New System.Net.Mail.SmtpClient
SmtpClient.Send(Mail)
SmtpClient.Dispose()

UPDATE

As suggested in comments, I agree this looks like an encoding problem but can't see what I can do about it on our side.

It seems to be only connected to the RepleyToList containing æ,ø and å and not Mail.To, which I define in similar fashion like this:

Dim mTo As New MailAddress(mailToAddress, mailToName, System.Text.Encoding.UTF8)
Mail.To.Add(mTo)

mailToName can contain æ, ø and å and is send perfectly okay.

Difficult to prove that something does not happen, but I am sure mails are not send and I am sure it is connected to the sendername in replyToList.

How I know?

I log the mails just before sending. Mails containing the characters in question are logged but never visible in https://mandrillapp.com/activity and never reach the recipient. I remove the æ, ø or å - all is visible in Mandrill Overview and all okay.

Wanna see my full code?

Here it is, note that sending mails which fails does not produce exceptions.

  Public Sub sendMail(ByVal mailFromAddress As String, ByVal mailFromName As String, _
                      ByVal mailToAddress As String, ByVal mailToName As String, ByVal mailCcAddress As String, ByVal mailBCcAddress As String, _
                      ByVal mailPriority As Net.Mail.MailPriority, ByVal IsBodyHtml As Boolean, ByVal mailSubject As String, _
                      ByVal bodyPlain As String, ByVal bodyHTML As String)
    Const maxtry As Integer = 3
    Dim tries As Integer = 0
    Dim failed As Boolean = False

    ' mailFromName = mailFromName.Replace("æ", "ae")

    Do
      tries += 1

      Try

        failed = False
        Dim Mail As New MailMessage


        If mailFromAddress <> String.Empty Then
          'Mail.ReplyTo = New MailAddress(mailFromAddress, mailFromName, System.Text.Encoding.UTF8)
          Mail.ReplyToList.Add(New MailAddress(mailFromAddress, mailFromName, System.Text.Encoding.UTF8))
        Else
          'Mail.ReplyTo = New MailAddress(Me.MailAddressFrom, Me.MailAddressDisplayName, System.Text.Encoding.UTF8)
          Mail.ReplyToList.Add(New MailAddress(Me.MailAddressFrom, Me.MailAddressDisplayName, System.Text.Encoding.UTF8))
        End If
        Mail.From = New MailAddress(Me.MailAddressFrom, Me.MailAddressDisplayName, System.Text.Encoding.UTF8)
        Mail.Sender = New MailAddress(Me.MailAddressFrom, Me.MailAddressDisplayName, System.Text.Encoding.UTF8)

        If mailToAddress <> String.Empty Then
          Dim mTo As New MailAddress(mailToAddress, mailToName, System.Text.Encoding.UTF8) 'UTF8
          Mail.To.Add(mTo)
        End If
        If mailCcAddress <> String.Empty Then
          Dim mCc As New MailAddress(mailCcAddress)
          Mail.CC.Add(mCc)
        End If
        If mailBCcAddress <> String.Empty Then
          Dim mBCc As New MailAddress(mailBCcAddress)
          Mail.Bcc.Add(mBCc)
        End If

        Mail.Priority = mailPriority

        Mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure

        Mail.IsBodyHtml = IsBodyHtml 
        Mail.HeadersEncoding = Encoding.GetEncoding("utf-8")

        Mail.Subject = mailSubject
        Mail.SubjectEncoding = Encoding.GetEncoding("utf-8") ' = System.Text.Encoding.Default 'UTF8 'Default ' Test on all possible encodings

        If IsBodyHtml Then
          Mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(bodyHTML, System.Text.Encoding.Default, "text/html"))
        End If
        Mail.BodyEncoding = Encoding.GetEncoding("utf-8")
        Mail.Body = bodyPlain

        ' Add to log
        addToMailLog(mailToAddress, mailCcAddress, mailSubject, bodyPlain)

        ' Send
        Dim SmtpClient As New System.Net.Mail.SmtpClient
        'SmtpClient.ServicePoint.MaxIdleTime = 1
        SmtpClient.Send(Mail)
        Me._status = EMailStatus.EOkay
        SmtpClient.Dispose()

      Catch ex As Exception
        Dim debugInfo As New CDebug("sendMail", "Try: " & tries.ToString & ", UserId: " & Me.UserId, ex.ToString, Me.UserId, String.Empty)
        Me._status = EMailStatus.EMailSendError
        failed = True
      End Try

    Loop Until (failed = False Or tries >= maxtry)

  End Sub

UPDATE II

I have now tracked the SMTP communication using WireShark. See screenshot which shows communication for a mail which was not delivered and does not show up in the Mandrill activity list - even though we get SMTP response OK and queued id.

smtp capture

I have three examples:

DELIVERED:

mailFromName = "xxx xxxxxxxxx xxxx - xxxxxxxxxx xxxxxxx xxxxxxxxx"

NOT DELIVERED:

mailFromName = "xxx xxxxxxxxx xxxx - xxxxxxxxxx xxxxxxx xxxxxxæxx"

DELIVERED:

mailFromName = "æxx xxxxxxxxx xxxx - xxxxxxxxxx xxxxxxx xxxxxxxxx"
like image 619
Muleskinner Avatar asked Oct 18 '22 12:10

Muleskinner


1 Answers

I believe I have found the explanation to this. It seems that the smtpclient add a new line after approx 72 characters of the subject and reply-to display name. The problem is that this seems to happen after the string has been encoded. Therefore, in some cases, a new line character is added in the middle of the encoded character which afaik is illegal.

I am not sure why Mandrill SMTP server fail without returning error while other SMTP servers handles the situation just fine.

Now that I understand what is happening I have to figure a way to fix it - best suggestion gets the bounty.

like image 167
Muleskinner Avatar answered Oct 21 '22 04:10

Muleskinner