Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem while sending mail by System.Net.Mail C#

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);
like image 930
Ozlem Balli Avatar asked Feb 01 '26 00:02

Ozlem Balli


1 Answers

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.

like image 107
leppie Avatar answered Feb 03 '26 14:02

leppie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!