使用System.Net.Mail 发送邮件总是提示“验证失败”,从网上也没找到确切的答案,大概是因为公司使用了代理网关,程序不能直接与邮件服务器建立连接。使用System.Web.Mail没有问题。下面是两种发送邮件的代码。 转自:http://www.cnblogs.com/zhouyu629/archive/2009/12/22/1629933.html System.Net.Mail 发送邮件 static string sendMail(string to, string title, string content) { string strHost = "xxx.xxx.xxx.x"; //STMP服务器地址 string strAccount = "post@5liao.cc"; //SMTP服务帐号 string strPwd = "1122333"; //SMTP服务密码 string strFrom = "post@5liao.cc"; //发送方邮件地址 SmtpClient _smtpClient = new SmtpClient(); _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 _smtpClient.Host = strHost; ;//指定SMTP服务器 _smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//用户名和密码 MailMessage _mailMessage = new MailMessage(strFrom, to); _mailMessage.Subject = title;//主题 _mailMessage.Body = content;//内容 _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码 _mailMessage.IsBodyHtml = true;//设置为HTML格式 _mailMessage.Priority = MailPriority.High;//优先级 try { _smtpClient.Send(_mailMessage); return "发送成功"; } catch (Exception ex) { return ex.Message; } } System.Web.Mail 发送邮件(winform或console程序需要对System.Web进行引用) static string sendMail(string to,string title,string content) { System.Web.Mail.MailMessage _mailMessage = new System.Web.Mail.MailMessage(); _mailMessage.Body = content; _mailMessage.From = "post@5liao.cc"; _mailMessage.Subject = title; _mailMessage.To = to; _mailMessage.BodyEncoding = System.Text.Encoding.UTF8; _mailMessage.BodyFormat = MailFormat.Html; _mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1); _mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "post@5liao.cc"); _mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "******"); System.Web.Mail.SmtpMail.SmtpServer = "XXx.xxx.xxx.x"; try { System.Web.Mail.SmtpMail.Send(_mailMessage); return "发送成功"; } catch(Exception ex) { return ex.Message; } } 另一篇有附件的用法,转自:http://www.cnblogs.com/aminta/archive/2008/11/08/1281357.html 主要是用到了System.Web.Mail命名空间,用到了此空间的三个类,分别是: ●MailMessage类,用于构造电子邮件 ●MailAttachment类,用于构造电子邮件附件 ●SmtpMail类,用于发送电子邮件及其附件 1、MailMessage类构造电子邮件 此类主要有以下属性和方法 ★From 发件人的地址 ★To 以分号分隔的收件人的地址列表 ★Cc 以分号隔开的抄送的收件人的邮件地址列表 ★Subject 电子邮件的主题 ★Body 电子邮件的正文 ★BodyFormat 电子邮件的正文内容类型,由MailFormat枚举值指定,MailFormat.Text或MailFormat.Html ★Attachments 电子邮件附件集合 ★Priority 电子邮件的优先级,由MailPriority枚举值指定,可以是MailPriority.Low ,MailPriority.Normal或MailPriority.High三者之一 2、Attachment用来构造电子邮件附件.用此类构造了电子邮件附件然后添加到MailMessage对象的Attachments集合即可 3、使用SmtpMail类发送电子邮件,可以通过系统本身的SMTP邮件服务或者其它SMTP服务器来发送,发送电子邮件首先需要设置SmtpMail类的SmtpServer属性,然后使用Send方法发送就可以了 下面做个简单的邮件发送例子: using System.Web.Mail; /// <summary> /// 给业务面试人发送面试通知邮件 /// </summary> /// <returns>true/false</returns> //发送邮件 bool sendmailbusi() { System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); mail.From = "aminta@sohu.com"; mail.To = "aminta@sohu.com"; mail.Subject = "测试邮件"; mail.Body = "这是一封测试邮件"; mail.BodyFormat = System.Web.Mail.MailFormat.Html; mail.BodyEncoding = System.Text.Encoding.UTF8; //邮件内容编码 //构造添加附件(可以发多个附件给多个收件人) System.Web.Mail.MailAttachment mailAttach_1 = new System.Web.Mail.MailAttachment(@"E:上岗考核表-入职.doc"); System.Web.Mail.MailAttachment mailAttach_2 = new System.Web.Mail.MailAttachment(@"E:填表说明.doc"); System.Web.Mail.MailAttachment mailAttach_3 = new System.Web.Mail.MailAttachment(@"E:上岗考核表-转正.doc"); mail.Attachments.Add (mailAttach_1); mail.Attachments.Add(mailAttach_2); mail.Attachments.Add(mailAttach_3); System.Web.Mail.SmtpMail.SmtpServer = "smtp.sohu.com"; // 发送邮件服务器端口 //验证 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //是否需要验证,一般是要的 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "aminta"); //自己邮箱的用户名 mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123456"); //自己邮箱的密码 try { System.Web.Mail.SmtpMail.Send(mail); return true; } catch { return false; } } // 05的应该是: 例如: //发送邮件(含附件) bool sendmail(string mailto,string mailfrom,string content,string loginname,string psd) { System.NET.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); mail.From = new System.Net.Mail.MailAddress(mailfrom.Trim()); mail.To.Add(mailto.Trim()); mail.Subject = "xx公司面试通知函"; mail.IsBodyHtml = true; mail.BodyEncoding = System.Text.Encoding.UTF8; //附件 string strFilePath = @"E:logo.jpg"; System.Net.Mail.Attachment attachment1 = new System.Net.Mail.Attachment(strFilePath);//添加附件 attachment1.Name = System.IO.Path.GetFileName(strFilePath); attachment1.NameEncoding = System.Text.Encoding.GetEncoding("gb2312"); attachment1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; attachment1.ContentDisposition.Inline = true; attachment1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline; string cid = attachment1.ContentId;//关键性的地方,这里得到一个id数值 mail.Attachments.Add(attachment1); //邮件正文 mail.Body ="<table width='100%'><tr><td><img src ='cid:"+cid+"'/></td></tr>" +content.Trim(); System.Net.Mail.SmtpClient server = new System.Net.Mail.SmtpClient("mail.xxxx.xxx"); //之前一不留神把公司域名挂上了,在头儿的提醒下赶紧改了,不然惨了,^0^ server.Credentials = new System.Net.NetworkCredential(loginname.Trim(), psd.Trim()); //用户名和密码 try { server.Send(mail); return true; } catch { return false; } }