在邮件标头中找到无效的字符

zhangxuyu1118 2008-03-27 02:27:56
using System.Net.Mail;试了很多SMTP发送邮件的代码,结果相同。

代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;

namespace 测试
{ //smtp类
class Class1
{
static bool mailSent = false;
public static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
String token = (string)e.UserState;

if (e.Cancelled)
{
Console.WriteLine("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
}
else
{
Console.WriteLine("Message sent.");
}
mailSent = true;
}
public static void Main(string[] args)
{
string svr = "mail.lierda.com";
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient(svr, 25);

// Specify the e-mail sender.
// Create a mailing address that includes a UTF8 character
// in the display name.
MailAddress from = new MailAddress("zxyu@lierda.com");
// "yuxiaozhong" + (char)0xD8 + "4821698",System.Text.Encoding.UTF8);
// Set destinations for the e-mail message.
MailAddress to = new MailAddress("zxyu@lierda.com");
// Specify the message content.
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent by an application. ";
// Include some non-ASCII characters in body and subject.
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
message.Body += Environment.NewLine + someArrows;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "test message 1" + someArrows;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// Set the method that is called back when the send operation ends.
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// The userState can be any object that allows your callback
// method to identify this send operation.
// For this example, the userToken is a string constant.
string userState = "test message1";
client.Credentials = new System.Net.NetworkCredential(from.Address, "11181118", "lierda.com");//
client.SendAsync(message, userState);
Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");

string answer = Console.ReadLine();

// If the user canceled the send, and mail hasn't been sent yet,
// then cancel the pending operation.
//client.SendAsyncCancel();
if (answer.StartsWith("c") && mailSent == false)
{
client.SendAsyncCancel();
}
// Clean up.
message.Dispose();
Console.WriteLine("Goodbye.");
}
}
}
...全文
3794 36 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
yingmu115 2011-11-29
  • 打赏
  • 举报
回复
到底什么原因啊。。。。。。我的竟然是有时候发送成功,有时候发送失败,不会还要靠人品吧。......
gyj5240 2011-10-18
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 dreamtimedt 的回复:]
发送邮件的服务器的“计算机名”一定不能有中文!

万恶的M$
[/Quote]

还是这位仁兄是对的,我的也是让改名(自己的名字),就不行了,一直发不了邮件。。

看来很多地方还是不要用中文名字
johnwanzhi 2011-08-31
  • 打赏
  • 举报
回复
一般这种问题都是编码惹的祸。还有一种情况就是邮件格式不正确。
超神肉肉 2011-08-11
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 iuhxq 的回复:]
我是用jmail实现的。

我对jmail偏爱。呵呵,用微软的类也可以实现。可能会稍微麻烦点。

利用asp.net+Jmail发送Email
[/Quote]

请问这个方法传入的参数是什么啊?
bhtc_hou 2011-05-26
  • 打赏
  • 举报
回复
为了看答案
dreamtimedt 2011-03-03
  • 打赏
  • 举报
回复
发送邮件的服务器的“计算机名”一定不能有中文!

万恶的M$
wangoqoq 2010-12-27
  • 打赏
  • 举报
回复
果真是好贴!!!!!!!!!!!!!!!!
xlong1900 2010-08-14
  • 打赏
  • 举报
回复
fff
指间、魔法师 2009-07-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 itfutao 的回复:]
你现在什么问题?
最近刚刚为公司开发了一个邮件收发系统
用的system.net.mail做的.

[/Quote]

system.net.mail 会出现:
在邮件标头中找到无效的字符。
mygoal 2008-12-18
  • 打赏
  • 举报
回复
bug 来的 http://support.microsoft.com/kb/947323/zh-cn
niitnanfeng 2008-12-08
  • 打赏
  • 举报
回复
mark
qiusongze 2008-09-26
  • 打赏
  • 举报
回复
大家怎么解决的啊,可否告诉我啊,我的邮箱qiusz@bankcomm.com
看了好多说明,这里说的最靠谱,”在邮件标头中找到无效的字符“,怎么算无效的字符,我没用过特别的字符啊。郁闷了好久了,谢谢啊
stevenvan 2008-05-07
  • 打赏
  • 举报
回复
还有如 this.Subject中有特殊符号也会报这样的异常

18:02
[]
等等
stevenvan 2008-05-07
  • 打赏
  • 举报
回复
呵,我和楼主遇到的问题一样,原因是如此
http://makoma.blog.hexun.com/16453328_d.html
M$ 工程师为了避开重复作编码的动作, 每次呼叫 ToEncodedString( ) 及 ToString( ) 时都会去看看 fullAddress 这个 private field 是否有值? 有的话代表之前已经作过编码了, 就直接捡现成. 问题出在第一次呼叫时, 编码的动作在 ToString( ) 及 ToEncodedString( ) 各写了一次 (果然没有做好 refactoring ... 哈哈), 结果 ToString( ) 的这份 code implementation 是错的, 跟本没编码 ...
在我的程序中原来是这样的:
this.Body = mailbody
this.Subject = rule.RuleName
加入.toString()后,问题解决!
this.Body = mailbody.ToString();
this.Subject = rule.RuleName.ToString();
主要问题是this.Subject出了问题
hanye_2005 2008-04-22
  • 打赏
  • 举报
回复
郁闷,真的无解啊?!
我的情况和楼主一模一样的啊!
曾经好用的程序怎么不行?
zhangxuyu1118 2008-04-21
  • 打赏
  • 举报
回复
非常感谢小灰(www.svnhost.cn)!

可爱的小灰 小美女 已经若干次无私地帮助我了,这次全分献上。

另外虽没给分,但还是感谢楼上各位!
iuhxq 2008-04-18
  • 打赏
  • 举报
回复
我是用jmail实现的。

我对jmail偏爱。呵呵,用微软的类也可以实现。可能会稍微麻烦点。

利用asp.net+Jmail发送Email
zhangxuyu1118 2008-04-16
  • 打赏
  • 举报
回复
竟然无解。C#程序员难道真的没有厚实基础的吗
zhangxuyu1118 2008-04-09
  • 打赏
  • 举报
回复
16楼的wtk13:
vc的SMTP程序和FOXMAIL可以正常发送,可以确定系统API没问题。
如果是编码的问题或者是Framework的问题,那么学习。NET的必要性真的要打折扣了;
对于这样的问题,MS有比较权威的解释吗,哪怕说你的操作系统是盗版的,所以。。。。
pinhailinfeng 2008-04-08
  • 打赏
  • 举报
回复
不太了解顶下
加载更多回复(15)

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧