通过 System.Net.Mail C# 发送邮件时出现问题
这是我的邮件发送代码。 我收到“邮件标题中存在无效字符”错误。当我更改计算机名称时,使用了一些最短的名称。 问题解决了。 但在我的域中,整个计算机名称如“04500-ab04545.xxxdomain.gov.tr”,所以我需要找到另一个解决方案来解决这个问题。
因此,我在从 C# 代码发送邮件时无法给出静态计算机名称。
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑这可能是与编码相关的问题。
尝试使用
new MailAddress("[电子邮件受保护]", "blab blalb", Encoding.Default)
构造函数。或者尝试
Encoding.Unicode
。更新:
经过一番挖掘,此异常是从以下位置抛出的:
如果电子邮件地址中的 \xff 上方有任何字符,则会发生这种情况。 这不应该发生,因为名称应该已经被编码,但我猜还有其他事情变得有趣。
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:
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.
消息试图发送哪些标头?
您可以使用以下 MSDN 片段轻松转储:
What headers are the message trying to send with?
You can easily dump with this MSDN snippet:
当我将其修改为“网络”时,我收到此错误 --- 然后错误得到解决。 (我的理解是 - 如果指定了 PickupDirectory 选项,标头 -encoding utf-8 (base64) 会给出错误)
希望有帮助
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