5

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"
Muleskinner
  • 14,150
  • 19
  • 58
  • 79
  • Seems obvious that this is an encoding problem. At least work through [this Q+A](http://stackoverflow.com/questions/20604551/mandrill-reject-reason-invalid-sender). If that doesn't help then be *specific* about how you know the address got rejected. – Hans Passant May 22 '17 at 08:39
  • Thank you for your comment, I looked through your link but not really connected to our problem - please see my updated question for more details. – Muleskinner May 22 '17 at 10:55
  • There is a setting on `SmtpClient` called `DeliveryFormat`. What happens if you set this to "International"? `SmtpClient.DeliveryFormat = SmtpDeliveryFormat.International` – TheHvidsten May 22 '17 at 11:10
  • Thank you - unfortunately DeliveryFormat was introduced in .NET 4.5 framework whereas we use 4.0 – Muleskinner May 22 '17 at 11:13
  • Will you be able to at least test with .NET 4.5? From what I've been able to read the SmtpClient in previous versions does not support all cases of international characters. If you still encounter this issue in .NET 4.5 as well we could at least have excluded one potential cause. – TheHvidsten May 22 '17 at 11:17
  • Unfortunately i cannot as we still use VS2010 – Muleskinner May 22 '17 at 11:22
  • I agree with you, the Wireshark trace is flawless. This is a server side issue if you believe the character range you are sending are within their accepted values. Note: for us new to Mandrill, we can't even see it's requirements. Just because the encoding is correct doesn't mean all the characters are allowed. – JWP May 24 '17 at 23:18
  • SmtpClient was always faulty, try MailKit https://github.com/jstedfast/MailKit – Akash Kava May 25 '17 at 18:40
  • Thanks to you, I realized that I could have the same problem with `System.Net.Mail` with Mandrill as you. It is not an option to use a different library in the short, so I will try to find a workaround and I will share it here if I succeed. – Kul-Tigin May 28 '17 at 22:31
  • @Kul-Tigin the only workaround Ive found so far is to use iso-8859-1 encoding (`System.Text.Encoding.GetEncoding("iso-8859-1")`). This seems to work for most of our customers languages but of course fails in languages with more excotic characters (japanese fx.). – Muleskinner May 29 '17 at 09:26

1 Answers1

3

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.

Muleskinner
  • 14,150
  • 19
  • 58
  • 79