使用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; } }