473,416 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 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 17440
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 Id=SYSTEM;Password=******" Dim strMessage As String ...
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 smalldatetime. When I executed it by Query Analyzer, it's...
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 special characters (from MS Word) into these forms....
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 You have error Invalid Character in content model....
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. When I call the Send function, the message is not...
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 / throws an exception, it is popping up an...
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 formatting is great, all fine and dandy, until I hover...
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 occus with both. The problems seems to be that...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.