472,331 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

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

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
2 17118
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Barry Young | last post by:
I am using the following code to insert a row in an Oracle Database. strConnection = "Provider=OraOLEDB.Oracle;Data Source=MYDATABASE;User...
3
by: Krung Saengpole | last post by:
Hi, I used SQL Server 2000 Personal Edition. I created a stored procedure having input parameters as smallint,tinyint,char,varchar and...
3
by: Todd | last post by:
Our ASP.NET (C#) application accepts form entry and saves inputed data in XML. We are finding that users are sometimes cutting and pasting...
2
by: SHC | last post by:
Hi all, I ran the attached volcanoes.xml (with geology.dtd) in the Module of Access 2003. I got the following error: Microsoft Office Access...
0
by: SR | last post by:
I am using the new namespace in .NET 2.0 to connect to an SMTP server and send an email message. I am using a Windows Application environment. ...
3
by: ticketdirector | last post by:
Hi, In the event viewer on our production web server (win2k3), I am seeing that if a call to the System.Net.Mail.SmtpClient.Send() function fails...
2
by: SR | last post by:
I have 2 controls that seem to conflict with each other. On my Master page I have a Menu control and on my Content page Panel control. The...
0
by: howardr101 | last post by:
Hi, Have hunted around on the groups and can't find anything, hence. I've tried this against 2 mail servers (mailtraq and hmailserver) and it...
1
by: Andrew Poulos | last post by:
When I run a frame set locally I get the error A Runtime Error has occurred. Do you wish to Debug? Line: 1 Error: Invalid character Yes No
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.