Helo command rejected: need fully-qualified hostname
When sending email from C#, if you ever get the error “Helo command rejected: need fully-qualified hostname”, you may end up scratching your head, because there is no easy way to change what gets sent in the HELO command from the SMTPClient class.
You, can however, use reflection to override what the SmtpClient class sends. here I have hard-coded “www.smtpjs.com” – because this code is specifically for this project.
You need to subclass SMTPClient as SMTPClientEx, and override the private properties localHostName and clientDomain as follows;
using System.Net.Mail;
using System.Reflection;/// <inheritdoc />
/// <summary>
/// Subclass of Smtpclient, with overridden HELO
/// </summary>
public class SmtpClientEx : SmtpClient
{public SmtpClientEx()
{
Initialize();
}private void Initialize()
{
const string strHelo = “www.smtpjs.com”;
var tSmtpClient = typeof(SmtpClient);
var localHostName = tSmtpClient.GetField(“localHostName”, BindingFlags.Instance | BindingFlags.NonPublic);
if (localHostName != null) localHostName.SetValue(this,strHelo);
var clientName = tSmtpClient.GetField(“clientDomain”, BindingFlags.Instance | BindingFlags.NonPublic);
if (clientName != null) clientName.SetValue(this, strHelo);
}
}