简体   繁体   English

System.Net.Mail C#发送邮件时出现问题

[英]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. 但在我的域名中,整个计算机名称如“04500-ab04545.xxxdomain.gov.tr”,所以我需要为此问题找到另一种解决方案。

So I cant give a static computer name while sending mail from c# code. 所以我不能在从c#代码发送邮件时提供静态计算机名称。

 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. 尝试使用new MailAddress("xxxx@xxxx.domain","blab blalb", Encoding.Default)构造函数。

Else try Encoding.Unicode . 否则尝试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. 如果您在电子邮件地址中有\\ xff以上的任何字符,则会发生这种情况。 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 ) (我的理解是 - 注明指定的pickupdirectory选项,标题-encoding utf-8(base64)给出了错误)

Hope it helps 希望能帮助到你

What headers are the message trying to send with? 尝试发送邮件的标题是什么?

You can easily dump with this MSDN snippet: 您可以使用此MSDN代码段轻松转储:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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