日期:2014-05-18  浏览次数:20603 次

SmtpClient类发送邮件时提示语法错误
SmtpClient   mailClient   =   new   SmtpClient( "mail.hisys.com.cn ");
mailClient.DeliveryMethod   =   SmtpDeliveryMethod.Network;
NetworkCredential   SMTPUserInfo   =   new   NetworkCredential( "xxxx ",   "xxx ");
mailClient.Credentials   =   SMTPUserInfo;
mailClient.Send(mailMessage);//mailMessage已经正确定义
用这段程序发送邮件时,对方服务器返回 "语法错误,无法识别命令 ",什么原因?
我用smtp.163.com这个服务器试了是可以的,为什么?

------解决方案--------------------
up
------解决方案--------------------
现在163的好像不支持SMTP,换一个别的邮箱试试,21CN的可以,我用过.
------解决方案--------------------
/// <summary>
/// 发送邮件
/// </summary>
/// <param name= "strSmtpServer "> smtp地址 </param>
/// <param name= "UserName "> 用户名 </param>
/// <param name= "Password "> 密码 </param>
/// <param name= "strFrom "> 发信人地址 </param>
/// <param name= "strto "> 收信人地址 </param>
/// <param name= "strSubject "> 邮件标题 </param>
/// <param name= "strBody "> 邮件正文 </param>
public static void SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
{
//生成一个 使用SMTP发送邮件的客户端对象
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);

//表示以当前登录用户的默认凭据进行身份验证
client.UseDefaultCredentials = true;



//包含用户名和密码
client.Credentials = new System.Net.NetworkCredential(UserName, Password);

//指定如何发送电子邮件。
//Network 电子邮件通过网络发送到 SMTP 服务器。
//PickupDirectoryFromIis 将电子邮件复制到挑选目录,然后通过本地 Internet 信息服务 (IIS) 传送。
//SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。

client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

//建立邮件对象
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject,strBody);

//定义邮件正文,主题的编码方式
message.BodyEncoding = System.Text.Encoding.GetEncoding( "gb2312 ");
message.SubjectEncoding = System.Text.Encoding.GetEncoding( "gb2312 ");

//获取或设置一个值,该值指示电子邮件正文是否为 HTML。
message.IsBodyHtml = false;

//指定邮件优先级

message.Priority = System.Net.Mail.MailPriority.Normal;

//添加附件
//System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@ "f:/baihe.txt ");
if (strFileName != " " && strFileName != null)
{
Attachment data = new Attachment(strFileName);
message.Attachments.Add(data);
}


//发件人身份验证,否则163 发不了
client.Credentials = new System.Net.NetworkCredential(strFrom, Password);

//发送
client.Send(message);
}
}