<>
发送邮件 如果发送人的邮箱用户名与邮箱密码都没有填写错误:如果报:参数或变量中有语法错误。 服务器响应为:mail from address must be same as authorization user错误,问题可能出在authorization user 没授权。如果是QQ邮箱,那么只要进入QQ邮箱,QQ邮箱:设置---账户 找到POP3/IMAP/SMTP服务 都开启。这时候发现可以发送邮件了。
如果报:SMTP 服务器要求安全连接或客户端未通过身份验证。 服务器响应为:Must issue a STARTTLS command first. 可能出现的问题是发送邮件的服务器端口(smtp.Port)配置错误。 例如:smtp.Port = 25
如果报:命令顺序不正确。 服务器响应为:Error: need EHLO and AUTH first ! 很有可能是发件人邮箱的用户名写错了;例如:(smtp.Credentials = new NetworkCredential("邮箱的用户名", "发件人邮箱密码");
如果报:参数或变量中有语法错误。 服务器响应为:mail from address must be same as authorization user 很有可能是发件人邮箱的密码错误了:例如:(smtp.Credentials = new NetworkCredential("邮箱的用户名", "发件人邮箱密码");
==============================================================================
<appSettings> <!--2015-06-26 Create--> <!-- Smtp 服务器 --> <add key="SmtpHost" value="smtp.qq.com" /> <!-- Smtp 服务器端口 --> <add key="SmtpPort" value="587" /> <!--发送者 Email 地址--> <add key="FromEmailAddress" value="wowo@qq.com" /> <!--发送者 Email 密码--> <add key="FormEmailPassword" value="123456789" /> <add key="ToEmailListString" value="123@qq.com;456@qq.com"/> <add key="CcEmailListString" value="CBA@qq.com"/> <add key="BccEmailListString" value="NBA@qq.com"/> </appSettings>
using CFPS_Redesign; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MVC发送邮件测试.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult SendEmail() { EmailHelper eh = new EmailHelper("邮件的主题", "你什么时候过来吃饭?"); if (eh.SendEmail()) { return View(); } else { return Content("on"); } } } }
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Mail; using System.Text; namespace CFPS_Redesign { /// <summary> /// Author : Bin /// Date : 2015-06-26 /// Description : 邮件发送辅助类 /// </summary> public class EmailHelper { #region [ 属性(发送Email相关) ] private string _SmtpHost = string.Empty; private int _SmtpPort = -1; private string _FromEmailAddress = string.Empty; private string _FormEmailPassword = string.Empty; private string _ToEmailListString = string.Empty; private string _CcEmailListString = string.Empty; private string _BccEmailListString = string.Empty; /// <summary> /// smtp 服务器 /// </summary> public string SmtpHost { get { if (string.IsNullOrEmpty(_SmtpHost)) { _SmtpHost = ConfigurationManager.AppSettings["SmtpHost"]; } return _SmtpHost; } } /// <summary> /// smtp 服务器端口 默认为25 /// </summary> public int SmtpPort { get { if (_SmtpPort == -1) { if (!int.TryParse(ConfigurationManager.AppSettings["SmtpPort"], out _SmtpPort)) { _SmtpPort = 25; } } return _SmtpPort; } } /// <summary> /// 发送者 Eamil 地址 /// </summary> public string FromEmailAddress { get { if (string.IsNullOrEmpty(_FromEmailAddress)) { _FromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"]; } return _FromEmailAddress; } } /// <summary> /// 发送者 Eamil 密码 /// </summary> public string FormEmailPassword { get { if (string.IsNullOrEmpty(_FormEmailPassword)) { _FormEmailPassword = ConfigurationManager.AppSettings["FormEmailPassword"]; } return _FormEmailPassword; } } #endregion #region [ 属性(邮件相关) ] /// <summary> /// 收件人 Email 列表,多个邮件地址之间用 半角逗号“,”或者分号“;”,或者竖线“|” 等符号分隔开 /// </summary> public string ToEmailListString { get { if (string.IsNullOrEmpty(_ToEmailListString)) { return ConfigurationManager.AppSettings["ToEmailListString"]; } return _ToEmailListString; } set { this._ToEmailListString = value; } } /// <summary> /// 邮件的抄送者,支持群发,多个邮件地址之间用 半角逗号“,”或者分号“;”,或者竖线“|” 等符号分隔开 /// </summary> public string CcEmailListString { get { if (string.IsNullOrEmpty(this._CcEmailListString)) { return ConfigurationManager.AppSettings["CcEmailListString"]; } return _CcEmailListString; } set { this._CcEmailListString = value; } } /// <summary> /// 邮件的密送者,支持群发,多个邮件地址之间用 半角逗号“,”或者分号“;”,或者竖线“|” 等符号分隔开 /// </summary> public string BccEmailListString { get { if (string.IsNullOrEmpty(_BccEmailListString)) { return ConfigurationManager.AppSettings["BccEmail"]; } return _BccEmailListString; } set { this._BccEmailListString = value; } } /// <summary> /// 邮件标题 /// </summary> public string Subject { get; set; } /// <summary> /// 邮件正文 /// </summary> public string EmailBody { get; set; } private bool _IsBodyHtml; /// <summary> /// 邮件正文是否为Html格式 /// </summary> public bool IsBodyHtml { get { return _IsBodyHtml; } set { _IsBodyHtml = value; } } /// <summary> /// 附件列表 /// </summary> public List<Attachment> AttachmentList { get; set; } #endregion #region [ 构造函数 ] /// <summary> /// 构造函数(EmailBody默认为html格式) /// </summary> /// <param name="subject">邮件标题</param> /// <param name="emailBody">邮件正文</param> public EmailHelper(string subject, string emailBody) { this.Subject = subject; this.IsBodyHtml = true; this.EmailBody = emailBody; } /// <summary> /// 构造函数 /// </summary> /// <param name="subject">邮件标题</param> /// <param name="isBodyHtml">邮件正文是否为Html格式</param> /// <param name="emailBody">邮件正文</param> public EmailHelper(string subject, bool isBodyHtml, string emailBody) { this.Subject = subject; this.IsBodyHtml = IsBodyHtml; this.EmailBody = emailBody; } /// <summary> /// 构造函数(EmailBody默认为html格式) /// </summary> /// <param name="subject">邮件标题</param> /// <param name="emailBody">邮件正文</param> /// <param name="attachmentList">附件列表</param> public EmailHelper(string subject, string emailBody, List<Attachment> attachmentList) { this.Subject = subject; this.EmailBody = emailBody; this.AttachmentList = attachmentList; this.IsBodyHtml = true; } /// <summary> /// 构造函数 /// </summary> /// <param name="subject">邮件标题</param> /// <param name="emailBody">邮件正文</param> /// <param name="isBodyEmail">邮件正文是否为Html格式</param> /// <param name="attachmentList">附件列表</param> public EmailHelper(string subject, string emailBody, bool isBodyEmail, List<Attachment> attachmentList) { this.Subject = subject; this.EmailBody = emailBody; this.AttachmentList = attachmentList; } /// <summary> /// 构造函数 (EmailBody默认为html格式) /// </summary> /// <param name="toList">收件人列表字符串</param> /// <param name="subject">邮件标题</param> /// <param name="emailBody">邮件正文</param> public EmailHelper(string toEmailListString, string subject, string emailBody) { this.ToEmailListString = toEmailListString; this.Subject = subject; this.EmailBody = emailBody; this.IsBodyHtml = true; } /// <summary> /// 构造函数 /// </summary> /// <param name="toList">收件人列表字符串</param> /// <param name="subject">邮件标题</param> /// <param name="isBodyHtml">邮件正文是否为Html格式</param> /// <param name="emailBody">邮件正文</param> public EmailHelper(string toEmailListString, string subject, bool isBodyHtml, string emailBody) { this.ToEmailListString = toEmailListString; this.Subject = subject; this.IsBodyHtml = isBodyHtml; this.EmailBody = emailBody; } /// <summary> /// 构造函数 /// </summary> /// <param name="toEmailListString">收件人列表字符串</param> /// <param name="ccEmailListString">抄送人列表字符串</param> /// <param name="subject">邮件标题</param> /// <param name="isBodyHtml">邮件正文是否为Html格式</param> /// <param name="emailBody">邮件正文</param> public EmailHelper(string toEmailListString, string ccEmailListString, string subject, bool isBodyHtml, string emailBody) { this.ToEmailListString = toEmailListString; this.CcEmailListString = ccEmailListString; this.Subject = subject; this.IsBodyHtml = isBodyHtml; this.EmailBody = emailBody; } /// <summary> /// 构造函数 /// </summary> /// <param name="toEmailListString">收件人列表字符串</param> /// <param name="ccEmailListString">抄送人列表字符串</param> /// <param name="subject">邮件标题</param> /// <param name="isBodyHtml">邮件正文是否为Html格式</param> /// <param name="emailBody">邮件正文</param> /// <param name="attachmentList">附件列表</param> public EmailHelper(string toEmailListString, string ccEmailListString, string subject, bool isBodyHtml, string emailBody, List<Attachment> attachmentList) { this.ToEmailListString = toEmailListString; this.CcEmailListString = ccEmailListString; this.Subject = subject; this.IsBodyHtml = isBodyHtml; this.EmailBody = emailBody; this.AttachmentList = attachmentList; } /// <summary> /// 构造函数 /// </summary> /// <param name="toEmailListString">收件人列表字符串</param> /// <param name="ccEmailListString">抄送人列表字符串</param> /// <param name="bccEmailListString">密送人列表字符串</param> /// <param name="subject">邮件标题</param> /// <param name="isBodyHtml">邮件正文是否为Html格式</param> /// <param name="emailBody">邮件正文</param> public EmailHelper(string toEmailListString, string ccEmailListString, string bccEmailListString, string subject, bool isBodyHtml, string emailBody) { this.ToEmailListString = toEmailListString; this.CcEmailListString = ccEmailListString; this.BccEmailListString = bccEmailListString; this.Subject = subject; this.IsBodyHtml = isBodyHtml; this.EmailBody = emailBody; } /// <summary> /// 构造函数 /// </summary> /// <param name="toEmailListString">收件人列表字符串</param> /// <param name="ccEmailListString">抄送人列表字符串</param> /// <param name="bccEmailListString">密送人列表字符串</param> /// <param name="subject">邮件标题</param> /// <param name="isBodyHtml">邮件正文是否为Html格式</param> /// <param name="emailBody">邮件正文</param> /// <param name="attachmentList">附件列表</param> public EmailHelper(string toEmailListString, string ccEmailListString, string bccEmailListString, string subject, bool isBodyHtml, string emailBody, List<Attachment> attachmentList) { this.ToEmailListString = toEmailListString; this.CcEmailListString = ccEmailListString; this.BccEmailListString = bccEmailListString; this.Subject = subject; this.IsBodyHtml = isBodyHtml; this.EmailBody = emailBody; this.AttachmentList = attachmentList; } #endregion #region [ 发送邮件 ] /// <summary> /// 发送邮件 /// </summary> /// <returns></returns> public bool SendEmail() { try { MailMessage mm = new MailMessage(); //实例化一个邮件类 mm.Priority = MailPriority.Normal; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可 mm.From = new MailAddress(this.FromEmailAddress, "管理员", Encoding.GetEncoding("GB2312")); //收件人 if (!string.IsNullOrEmpty(this.ToEmailListString)) { string[] toEmailArray = this.ToEmailListString.Split(new char[] { ';', ',', '|', ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (string toEmail in toEmailArray) { mm.To.Add(toEmail.Trim()); } } //抄送人 if (!string.IsNullOrEmpty(this.CcEmailListString)) { string[] CcEmailArray = this.CcEmailListString.Split(new char[] { ';', ',', '|', ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (string ccEmail in CcEmailArray) { mm.CC.Add(ccEmail.Trim()); } } //密送人 if (!string.IsNullOrEmpty(this.BccEmailListString)) { string[] BccEmailArray = this.BccEmailListString.Split(new char[] { ';', ',', '|', ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (string bccEmail in BccEmailArray) { mm.Bcc.Add(bccEmail.Trim()); } } mm.Subject = this.Subject; //邮件标题 mm.SubjectEncoding = Encoding.GetEncoding("GB2312"); //这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。 mm.IsBodyHtml = this.IsBodyHtml; //邮件正文是否是HTML格式 mm.BodyEncoding = Encoding.GetEncoding("GB2312"); //邮件正文的编码, 设置不正确, 接收者会收到乱码 mm.Body = this.EmailBody; //邮件正文 //邮件附件 if (this.AttachmentList != null && this.AttachmentList.Count > 0) { foreach (Attachment attachment in this.AttachmentList) { mm.Attachments.Add(attachment); } } SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network smtp.EnableSsl = false; //smtp服务器是否启用SSL加密 smtp.Host = this.SmtpHost; //指定 smtp 服务器地址 //smtp.Port = this.SmtpPort; //指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去 //smtp.UseDefaultCredentials = true; //如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了 smtp.Credentials = new NetworkCredential(this.FromEmailAddress, this.FormEmailPassword); //如果需要认证,则用这种方式 //发送邮件,如果不返回异常, 则大功告成了。 smtp.Send(mm); return true; } catch (Exception e) { return false; } } #endregion } }
原文地址:http://blog.csdn.net/fanbin168/article/details/46674125