当以下代码中的mailFromName包含“æ”,“ø”或“å”时,我们正面临通过Mandrill SMTP发送邮件时出现问题的无效问题。当我们使用另一个SMTP时,邮件发送得非常好。
Mail.ReplyToList.Add(New MailAddress(mailFromAddress, mailFromName, System.Text.Encoding.UTF8))
我们使用.NET smtpClient类。
Dim SmtpClient As New System.Net.Mail.SmtpClient
SmtpClient.Send(Mail)
SmtpClient.Dispose()
更新
正如评论中所建议的那样,我同意这看起来像编码问题,但无法看到我能为自己做些什么。
它似乎只连接到包含æ,ø和å而不是Mail.To
的RepleyToList,我以类似的方式定义它:
Dim mTo As New MailAddress(mailToAddress, mailToName, System.Text.Encoding.UTF8)
Mail.To.Add(mTo)
mailToName
可以包含æ,ø和å,并且发送完全没问题。
很难证明某些事情没有发生,但我确信邮件不会发送,我确信它已连接到replyToList中的sendername。
我怎么知道?
我在发送之前记录邮件。包含相关字符的邮件会被记录,但在https://mandrillapp.com/activity中永远不会显示,并且永远不会到达收件人。我删除了æ,ø或å - 所有这些都在Mandrill概述中可见,一切正常。
想看看我的完整代码吗?
请注意,发送失败的邮件不会产生异常。
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
更新II
我现在使用WireShark跟踪SMTP通信。请参阅屏幕截图,其中显示了未交付但未显示在Mandrill活动列表中的邮件的通信 - 即使我们获得SMTP响应正常并排队ID。
我有三个例子:
送达:
mailFromName = "xxx xxxxxxxxx xxxx - xxxxxxxxxx xxxxxxx xxxxxxxxx"
未交付:
mailFromName = "xxx xxxxxxxxx xxxx - xxxxxxxxxx xxxxxxx xxxxxxæxx"
送达:
mailFromName = "æxx xxxxxxxxx xxxx - xxxxxxxxxx xxxxxxx xxxxxxxxx"
答案 0 :(得分:3)
我相信我已经找到了解释。似乎smtpclient在约72个字符的主题和回复显示名称后添加一个新行。问题是这似乎是在字符串编码后发生的。因此,在某些情况下,在编码字符的中间添加一个新行字符,其中afaik是非法的。
我不确定为什么Mandrill SMTP服务器在没有返回错误的情况下失败,而其他SMTP服务器处理这种情况就好了。
既然我明白了发生了什么,我必须想办法解决它 - 最好的建议得到赏金。