简体   繁体   中英

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("xxxx@xxxx.domain"));
 msg.From = new MailAddress("xxxx@xxxx.domain","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);

I suspect this might be an Encoding related issue.

Try using the new MailAddress("xxxx@xxxx.domain","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.

I received this error when if this is modified to "network" --- then error got resolved. ( My understanding is - Incase of specified pickupdirectory option, the header -encoding utf-8 (base64) was giving error )

Hope it helps

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM