System.Net.Mail.SmtpClient.Send() Error: An invalid character was found in the mail header.

  • Thread starter Thread starter clevrmnkey
  • Start date Start date
C

clevrmnkey

I've had nothing but trouble from the System.Net.Mail objects, but I
finally need to make them work, and I can't for the life of me see what
I'm doing wrong.

I pared back my mail transaction to the bare minimum:

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("<my
mail server IP>", 25);
smtp.Send("<one of my email addresses>", "<another of my email
addresses>", "Hello", "World");

And on the smtp.Send() command I get back a SmtpException {"Failure
sending mail."}, {An invalid character was found in the mail header.}.

An inspection of the MailMessage object (before I simplified that away)
showed that there were no items in the header before this exception. A
packet trace during the exception shows that the SMTP server responds
properly and the object immediately replys with a "QUIT".

I would greatly appreciate any insight that could be offered.
Thanks,
Andy
 
hmmmm. im not sure... the "to", "from" fields all end up in the header...
but ill post the code I use... hope it helps.

Adam - http://www.aejw.com/?page=contact


public class emailMsg{

#region propertys

private string ls_smtpServer="localhost"; public string
smtpServer{get{return(ls_smtpServer);}set{ls_smtpServer=value;}}
private string ls_Username=""; public string
smtpUserName{get{return(ls_Username);}set{ls_Username=value;}}
private string ls_Password=""; public string
smtpUserPassword{get{return(ls_Password);}set{ls_Password=value;}}
private bool lf_useThreading=false; public bool
useThreading{get{return(lf_useThreading);}set{lf_useThreading=value;}}

private string ls_toAddress=""; public string
to{get{return(ls_toAddress);}set{ls_toAddress=value;}}
private string ls_fromAddress=""; public string
from{get{return(ls_fromAddress);}set{ls_fromAddress=value;}}
private string ls_subjectText=""; public string
subject{get{return(ls_subjectText);}set{ls_subjectText=value;}}
private string ls_bodyText="test"; public string
body{get{return(ls_bodyText);}set{ls_bodyText=value;}}

public enum emailContentType{
text=0,
html=1,
}

private emailContentType lt_Content=emailContentType.text;
public emailContentType
bodyContent{get{return(lt_Content);}set{lt_Content=value;}}

#endregion

#region send mail functions

public void sendMail(){emailMsg.sendMail(this);}

static public void sendMail(emailMsg pMessage) {
//prep message
if(pMessage==null){
throw new Exception("'emailMessage' object is required");
}
if(pMessage.useThreading){
//set thread info
if(lo_EmailThreads==null) lo_EmailThreads=new
System.Collections.ArrayList();
int iSel=lo_EmailThreads.Add(pMessage);
//start thread
System.Threading.Thread oThread = new System.Threading.Thread(new
System.Threading.ThreadStart(z_ThreadSend));
oThread.Name=iSel.ToString();
oThread.Start();
}else{
//start non thread
z_SendMail(pMessage);
}
}


#endregion

#region private functions

private static System.Collections.ArrayList lo_EmailThreads = null;
private static void z_ThreadSend(){
int i=Convert.ToInt32(System.Threading.Thread.CurrentThread.Name);
emailMsg oMessage = (emailMsg)lo_EmailThreads;
lo_EmailThreads.Remove(i);
z_SendMail(oMessage);
}

private static void z_SendMail(emailMsg pMessage){

System.Net.Mail.SmtpClient client = new
System.Net.Mail.SmtpClient();
client.Host = pMessage.smtpServer;
client.Send(pMessage.z_getMessage());

System.Threading.Thread.Sleep(10);
}

internal System.Net.Mail.MailMessage z_getMessage(){

System.Net.Mail.MailAddress to = new
System.Net.Mail.MailAddress(this.to);
System.Net.Mail.MailAddress from = new
System.Net.Mail.MailAddress(this.from);
System.Net.Mail.MailMessage oMailMessage = new
System.Net.Mail.MailMessage(from, to);
oMailMessage.Subject = this.subject;
oMailMessage.Body = this.body;
oMailMessage.IsBodyHtml = (lt_Content == emailContentType.html);
oMailMessage.Priority = System.Net.Mail.MailPriority.Normal;

return(oMailMessage);
}


#endregion

}
 
Adam:

Thanks for posting your .NET emailMsg class.
I plugged that in, and got exactly the same error:

[MyClass].emailMsg mail = new [MyClass].emailMsg();
mail.smtpServer = "[SMTP Server IP]";
mail.to = "[MyPrimaryEmailAddress]";
mail.from = "[MySecondaryEmailAddress]";
mail.subject = "Hello";
mail.body = "World";
mail.sendMail();

Here's a little more information about the Exception:

SmtpException {"Failure sending mail."}
InnerException {"An invalid character was found in the mail header."}
StackTrace " at System.Net.BufferBuilder.Append(String value,
Int32 offset, Int32 count)\r\n at
System.Net.Mail.EHelloCommand.PrepareCommand(SmtpConnection conn,
String domain)\r\n at
System.Net.Mail.SmtpConnection.GetConnection(String host, Int32
port)\r\n at System.Net.Mail.SmtpTransport.GetConnection(String host,
Int32 port)\r\n at System.Net.Mail.SmtpClient.GetConnection()\r\n
at System.Net.Mail.SmtpClient.Send(MailMessage message)"
TargetSite {Void Append(System.String, Int32, Int32)}

I thought briefly that this was caused by the PGP service attempting to
encrypt all SMTP mail content leaving my laptop, but that didn't turn
out to be the case.

Ideas?

Many Thanks,
Andy


aejw.com said:
hmmmm. im not sure... the "to", "from" fields all end up in the header...
but ill post the code I use... hope it helps.

Adam - http://www.aejw.com/?page=contact


public class emailMsg{

#region propertys

private string ls_smtpServer="localhost"; public string
smtpServer{get{return(ls_smtpServer);}set{ls_smtpServer=value;}}
private string ls_Username=""; public string
smtpUserName{get{return(ls_Username);}set{ls_Username=value;}}
private string ls_Password=""; public string
smtpUserPassword{get{return(ls_Password);}set{ls_Password=value;}}
private bool lf_useThreading=false; public bool
useThreading{get{return(lf_useThreading);}set{lf_useThreading=value;}}

private string ls_toAddress=""; public string
to{get{return(ls_toAddress);}set{ls_toAddress=value;}}
private string ls_fromAddress=""; public string
from{get{return(ls_fromAddress);}set{ls_fromAddress=value;}}
private string ls_subjectText=""; public string
subject{get{return(ls_subjectText);}set{ls_subjectText=value;}}
private string ls_bodyText="test"; public string
body{get{return(ls_bodyText);}set{ls_bodyText=value;}}

public enum emailContentType{
text=0,
html=1,
}

private emailContentType lt_Content=emailContentType.text;
public emailContentType
bodyContent{get{return(lt_Content);}set{lt_Content=value;}}

#endregion

#region send mail functions

public void sendMail(){emailMsg.sendMail(this);}

static public void sendMail(emailMsg pMessage) {
//prep message
if(pMessage==null){
throw new Exception("'emailMessage' object is required");
}
if(pMessage.useThreading){
//set thread info
if(lo_EmailThreads==null) lo_EmailThreads=new
System.Collections.ArrayList();
int iSel=lo_EmailThreads.Add(pMessage);
//start thread
System.Threading.Thread oThread = new System.Threading.Thread(new
System.Threading.ThreadStart(z_ThreadSend));
oThread.Name=iSel.ToString();
oThread.Start();
}else{
//start non thread
z_SendMail(pMessage);
}
}


#endregion

#region private functions

private static System.Collections.ArrayList lo_EmailThreads = null;
private static void z_ThreadSend(){
int i=Convert.ToInt32(System.Threading.Thread.CurrentThread.Name);
emailMsg oMessage = (emailMsg)lo_EmailThreads;
lo_EmailThreads.Remove(i);
z_SendMail(oMessage);
}

private static void z_SendMail(emailMsg pMessage){

System.Net.Mail.SmtpClient client = new
System.Net.Mail.SmtpClient();
client.Host = pMessage.smtpServer;
client.Send(pMessage.z_getMessage());

System.Threading.Thread.Sleep(10);
}

internal System.Net.Mail.MailMessage z_getMessage(){

System.Net.Mail.MailAddress to = new
System.Net.Mail.MailAddress(this.to);
System.Net.Mail.MailAddress from = new
System.Net.Mail.MailAddress(this.from);
System.Net.Mail.MailMessage oMailMessage = new
System.Net.Mail.MailMessage(from, to);
oMailMessage.Subject = this.subject;
oMailMessage.Body = this.body;
oMailMessage.IsBodyHtml = (lt_Content == emailContentType.html);
oMailMessage.Priority = System.Net.Mail.MailPriority.Normal;

return(oMailMessage);
}


#endregion

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top