views:

887

answers:

3

This is my mail sending code. I was getting "There is Invalid character in Mail Header" error.When i changed my Computer Name some shortest name. The problem solved. But in my domain whole computer names like "04500-ab04545.xxxdomain.gov.tr" so I need to find another solution for this problem.

So I cant give a static computer name while sending mail from c# code.

 MailMessage msg = new MailMessage();
 msg.Body = "axxxxxx";
 msg.To.Add(new MailAddress("[email protected]"));
 msg.From = new MailAddress("[email protected]","blab blalb");
 msg.Subject = "Subject xxx";
 SmtpClient server = new SmtpClient("xxxxxxxx",25);
 server.Credentials = new NetworkCredential("xxxxx", "xxxxxxx");
 SmtpClient server = new SmtpClient("mail.adalet.gov.tr",25);
 server.Credentials = new NetworkCredential("xxx", "xxx");
 server.Send(msg);
A: 

What headers are the message trying to send with?

You can easily dump with this MSDN snippet:

string[] keys = message.Headers.AllKeys;
        Console.WriteLine("Headers");
        foreach (string s in keys)
        {
            Console.WriteLine("{0}:", s);
            Console.WriteLine("    {0}", message.Headers[s]);
+4  A: 

I suspect this might be an Encoding related issue.

Try using the new MailAddress("[email protected]","blab blalb", Encoding.Default) constructor.

Else try Encoding.Unicode.

Update:

After some digging, this exception is thrown from:

void System.Net.BufferBuilder.Append(string,int,int);

This will happen if you have any characters above \xff in the email address. This is not suppose to happen, as the name should be encoded already, but something else is going funny I guess.

leppie
thanx man u r cool ;)
Ozlem Balli
Seems even Microsoft fails the Turkish test sometimes :)
leppie
A: 
MailMessage mail = new MailMessage("xxxx@domain", "Subject goes here",
"Please put the body of your email message here.");
SmtpClient client = new SmtpClient();

Then in your try block:

client.Send(mail);

And this in your web.config:

<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="mail.adalet.gov.tr" defaultCredentials="true" />
</smtp>
</mailSettings>   
</system.net>
GregD