Skip to content

SmtpClient doesn’t send FQDN when HELO domain is required

2007 May 10
tags:
by Philippe Delodder

I was working on a simple
mail system and each time when I tryed to send a Mail it didn’t go
through. I think thats ought, but after a bit searching on the net and
looking in the logs i found out why it didn’t go throught no valid FQDN
for HELO domain. So it seems there was a problem with the smtpclient of
.net framework 2.0 and i have fixed it. The fix is more of a workaround
to solve it, by using reflections.

You can download it here or you can view it below

Imports System.Reflection

Public Class RFCSmtpClientInherits System.Net.Mail.SmtpClient

Private Shared ReadOnly localHostName As FieldInfo = GetLocalHostNameField()

Public Sub New(ByRef host As String, ByVal port As Integer)MyBase.New(host, port)Initialize()End Sub

Public Sub New(ByRef host As String)MyBase.New(host)Initialize()End Sub

Public Sub New()MyBase.New()Initialize()End Sub

Public Sub Initialize()Dim h As String = getFQDN()If h <> "" ThenHELODomain = hEnd IfEnd Sub

Public Property HELODomain() As StringGetIf localHostName Is Nothing ThenReturn NothingElseReturn localHostName.GetValue(Me)End IfEnd GetSet(ByVal value As String)If String.IsNullOrEmpty(value) ThenThrow New ArgumentNullException("value")ElselocalHostName.SetValue(Me, value)End IfEnd SetEnd Property

Private Shared Function GetLocalHostNameField() As FieldInfoDim flags As BindingFlags = (BindingFlags.Instance Or BindingFlags.NonPublic)Return GetType(System.Net.Mail.SmtpClient).GetField("localHostName", flags)End Function

Public Function getFQDN() As StringReturn System.Net.Dns.GetHostEntry("").HostNameEnd Function

End Class
3 Responses leave one →
  1. January 14, 2009

    Interesting article, i have bookmarked your blog for future referrence, thanks

  2. teeteekaakaa permalink
    May 23, 2010

    Hey, I’ve run into the same issue…mail going to junk folder of users and on looking at the log files found that SmtpClient is using netbios name of my computer in EHLO, instead of domain name (myserver.com).

    Did the code above fix your junk mail issue once and for all?? do you have updated code for this class?

    • May 23, 2010

      No I have not updated the class. If you have an updated version I can post it here.

      Indeed this code did solve the issue for me.

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

Bad Behavior has blocked 174 access attempts in the last 7 days.