473,598 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Net.Mail .SmtpClient.Sen d() 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("<m y
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 17468
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_smtpS erver);}set{ls_ smtpServer=valu e;}}
private string ls_Username=""; public string
smtpUserName{ge t{return(ls_Use rname);}set{ls_ Username=value; }}
private string ls_Password=""; public string
smtpUserPasswor d{get{return(ls _Password);}set {ls_Password=va lue;}}
private bool lf_useThreading =false; public bool
useThreading{ge t{return(lf_use Threading);}set {lf_useThreadin g=value;}}

private string ls_toAddress="" ; public string
to{get{return(l s_toAddress);}s et{ls_toAddress =value;}}
private string ls_fromAddress= ""; public string
from{get{return (ls_fromAddress );}set{ls_fromA ddress=value;}}
private string ls_subjectText= ""; public string
subject{get{ret urn(ls_subjectT ext);}set{ls_su bjectText=value ;}}
private string ls_bodyText="te st"; public string
body{get{return (ls_bodyText);} set{ls_bodyText =value;}}

public enum emailContentTyp e{
text=0,
html=1,
}

private emailContentTyp e lt_Content=emai lContentType.te xt;
public emailContentTyp e
bodyContent{get {return(lt_Cont ent);}set{lt_Co ntent=value;}}

#endregion

#region send mail functions

public void sendMail(){emai lMsg.sendMail(t his);}

static public void sendMail(emailM sg pMessage) {
//prep message
if(pMessage==nu ll){
throw new Exception("'ema ilMessage' object is required");
}
if(pMessage.use Threading){
//set thread info
if(lo_EmailThre ads==null) lo_EmailThreads =new
System.Collecti ons.ArrayList() ;
int iSel=lo_EmailTh reads.Add(pMess age);
//start thread
System.Threadin g.Thread oThread = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(z _ThreadSend));
oThread.Name=iS el.ToString();
oThread.Start() ;
}else{
//start non thread
z_SendMail(pMes sage);
}
}
#endregion

#region private functions

private static System.Collecti ons.ArrayList lo_EmailThreads = null;
private static void z_ThreadSend(){
int i=Convert.ToInt 32(System.Threa ding.Thread.Cur rentThread.Name );
emailMsg oMessage = (emailMsg)lo_Em ailThreads[i];
lo_EmailThreads .Remove(i);
z_SendMail(oMes sage);
}

private static void z_SendMail(emai lMsg pMessage){

System.Net.Mail .SmtpClient client = new
System.Net.Mail .SmtpClient();
client.Host = pMessage.smtpSe rver;
client.Send(pMe ssage.z_getMess age());

System.Threadin g.Thread.Sleep( 10);
}

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

System.Net.Mail .MailAddress to = new
System.Net.Mail .MailAddress(th is.to);
System.Net.Mail .MailAddress from = new
System.Net.Mail .MailAddress(th is.from);
System.Net.Mail .MailMessage oMailMessage = new
System.Net.Mail .MailMessage(fr om, to);
oMailMessage.Su bject = this.subject;
oMailMessage.Bo dy = this.body;
oMailMessage.Is BodyHtml = (lt_Content == emailContentTyp e.html);
oMailMessage.Pr iority = System.Net.Mail .MailPriority.N ormal;

return(oMailMes sage);
}
#endregion

}

<cl********@gma il.comwrote in message
news:11******** **************@ 48g2000cwx.goog legroups.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("<m y
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 = "[MyPrimaryEmailA ddress]";
mail.from = "[MySecondaryEmai lAddress]";
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.Buff erBuilder.Appen d(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(S tring host,
Int32 port)\r\n at System.Net.Mail .SmtpClient.Get Connection()\r\ n
at System.Net.Mail .SmtpClient.Sen d(MailMessage message)"
TargetSite {Void Append(System.S tring, 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_smtpS erver);}set{ls_ smtpServer=valu e;}}
private string ls_Username=""; public string
smtpUserName{ge t{return(ls_Use rname);}set{ls_ Username=value; }}
private string ls_Password=""; public string
smtpUserPasswor d{get{return(ls _Password);}set {ls_Password=va lue;}}
private bool lf_useThreading =false; public bool
useThreading{ge t{return(lf_use Threading);}set {lf_useThreadin g=value;}}

private string ls_toAddress="" ; public string
to{get{return(l s_toAddress);}s et{ls_toAddress =value;}}
private string ls_fromAddress= ""; public string
from{get{return (ls_fromAddress );}set{ls_fromA ddress=value;}}
private string ls_subjectText= ""; public string
subject{get{ret urn(ls_subjectT ext);}set{ls_su bjectText=value ;}}
private string ls_bodyText="te st"; public string
body{get{return (ls_bodyText);} set{ls_bodyText =value;}}

public enum emailContentTyp e{
text=0,
html=1,
}

private emailContentTyp e lt_Content=emai lContentType.te xt;
public emailContentTyp e
bodyContent{get {return(lt_Cont ent);}set{lt_Co ntent=value;}}

#endregion

#region send mail functions

public void sendMail(){emai lMsg.sendMail(t his);}

static public void sendMail(emailM sg pMessage) {
//prep message
if(pMessage==nu ll){
throw new Exception("'ema ilMessage' object is required");
}
if(pMessage.use Threading){
//set thread info
if(lo_EmailThre ads==null) lo_EmailThreads =new
System.Collecti ons.ArrayList() ;
int iSel=lo_EmailTh reads.Add(pMess age);
//start thread
System.Threadin g.Thread oThread = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(z _ThreadSend));
oThread.Name=iS el.ToString();
oThread.Start() ;
}else{
//start non thread
z_SendMail(pMes sage);
}
}
#endregion

#region private functions

private static System.Collecti ons.ArrayList lo_EmailThreads = null;
private static void z_ThreadSend(){
int i=Convert.ToInt 32(System.Threa ding.Thread.Cur rentThread.Name );
emailMsg oMessage = (emailMsg)lo_Em ailThreads[i];
lo_EmailThreads .Remove(i);
z_SendMail(oMes sage);
}

private static void z_SendMail(emai lMsg pMessage){

System.Net.Mail .SmtpClient client = new
System.Net.Mail .SmtpClient();
client.Host = pMessage.smtpSe rver;
client.Send(pMe ssage.z_getMess age());

System.Threadin g.Thread.Sleep( 10);
}

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

System.Net.Mail .MailAddress to = new
System.Net.Mail .MailAddress(th is.to);
System.Net.Mail .MailAddress from = new
System.Net.Mail .MailAddress(th is.from);
System.Net.Mail .MailMessage oMailMessage = new
System.Net.Mail .MailMessage(fr om, to);
oMailMessage.Su bject = this.subject;
oMailMessage.Bo dy = this.body;
oMailMessage.Is BodyHtml = (lt_Content == emailContentTyp e.html);
oMailMessage.Pr iority = System.Net.Mail .MailPriority.N ormal;

return(oMailMes sage);
}
#endregion

}
Dec 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12631
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 Dim objConnection As New OleDb.OleDbConnection(strConnection) Dim objCommand As OleDb.OleDbCommand
3
31019
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 ok. But when I executed it by ASP code that used ADODB.command, it showed error: Invalid character value for cast specification . Though SQL Server debugger, it showed: Invalid character value for cast
3
22309
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. The data is saved successfuly, but when the XML is later read the following error is encountered depending on the invalid character found: This is a sample:
2
2074
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. |OK| Please help and advise me what is wrong in my source code and how to correct
0
1173
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 sent immediately. It is only sent when I close the application or try to send another message. I have tried disabling my AntiVirus package and it still happens or should I say, doesn't happen. The following is my code, quite simple and from my...
3
2256
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 assertion failure message box. But since this is a remote server, there is no logged in user, so the assertion failure message box is just sitting there waiting forever for someone to come along and hit "OK" button. This is causing problems on the...
2
1983
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 over one of the Static menu items and the menu displays. It moves the entire panel down, the top basically becomes the bottom of the panel...all the information is still intact, but I have a big blank space where my content used to be.... Any...
0
2395
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 when the SMTPClient attaches to the server and the server sends it greeting message, if anything other than +OK is received by the client it throws an exception and falls over.
1
3068
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
7981
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7894
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8284
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8262
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3894
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1500
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.