By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,905 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,905 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Dec 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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_smtpS erver=value;}}
private string ls_Username=""; public string
smtpUserName{get{return(ls_Username);}set{ls_Usern ame=value;}}
private string ls_Password=""; public string
smtpUserPassword{get{return(ls_Password);}set{ls_P assword=value;}}
private bool lf_useThreading=false; public bool
useThreading{get{return(lf_useThreading);}set{lf_u seThreading=value;}}

private string ls_toAddress=""; public string
to{get{return(ls_toAddress);}set{ls_toAddress=valu e;}}
private string ls_fromAddress=""; public string
from{get{return(ls_fromAddress);}set{ls_fromAddres s=value;}}
private string ls_subjectText=""; public string
subject{get{return(ls_subjectText);}set{ls_subject Text=value;}}
private string ls_bodyText="test"; public string
body{get{return(ls_bodyText);}set{ls_bodyText=valu e;}}

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.CurrentT hread.Name);
emailMsg oMessage = (emailMsg)lo_EmailThreads[i];
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

}

<cl********@gmail.comwrote in message
news:11**********************@48g2000cwx.googlegro ups.com...
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
Dec 22 '06 #2

P: n/a
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(SmtpC onnection conn,
String domain)\r\n at
System.Net.Mail.SmtpConnection.GetConnection(Strin g 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 wrote:
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_smtpS erver=value;}}
private string ls_Username=""; public string
smtpUserName{get{return(ls_Username);}set{ls_Usern ame=value;}}
private string ls_Password=""; public string
smtpUserPassword{get{return(ls_Password);}set{ls_P assword=value;}}
private bool lf_useThreading=false; public bool
useThreading{get{return(lf_useThreading);}set{lf_u seThreading=value;}}

private string ls_toAddress=""; public string
to{get{return(ls_toAddress);}set{ls_toAddress=valu e;}}
private string ls_fromAddress=""; public string
from{get{return(ls_fromAddress);}set{ls_fromAddres s=value;}}
private string ls_subjectText=""; public string
subject{get{return(ls_subjectText);}set{ls_subject Text=value;}}
private string ls_bodyText="test"; public string
body{get{return(ls_bodyText);}set{ls_bodyText=valu e;}}

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.CurrentT hread.Name);
emailMsg oMessage = (emailMsg)lo_EmailThreads[i];
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

}
Dec 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.