原理:
例如A使用163邮箱发送邮件给B(qq邮箱)。首先A会把邮件通过SMTP(Simple Mail Transfer Protocol)协议传输到163的Smtp服务器上,163的Smtp服务器会根据B的邮箱账号,把邮件通过Smtp协议发给QQ邮箱的Smtp服务器。QQ的Smtp服务器接收到邮件消息后会将之存储在QQ邮箱的邮件存储设备上。当B登陆QQ邮箱后,如果有新邮件,POP3服务器就会从QQ邮箱的邮件存储设备上读出邮件并通过POP3/IMAP服务发送给邮件客户端的B。
原理图
使用的对象:
smtpClient类:允许应用程序使用简单邮件传输协议 (SMTP) 发送电子邮件。
MailAddress类:MailAddress类表示电子邮件的地址。该类结合SmtpClient类或MailMessage类使用,其实例存储了电子邮件的地址信息。
MailMessage 类:MailMessage类是.NET Framework中管理邮件的专用类,表示可以使用SmtpClient类发送的电子邮件。MailMessage 类的实例表示一个电子邮件的所有内容,并用SmtpClient类来传输到SMTP服务器。若要指定电子邮件的发件人、收件人和内容,可以使用 MailMessage类的关联属性。
NetworkCredential类:为基于密码的身份验证方案(如基本、简要、NTLM 和 Kerberos 身份验证)提供凭据。
C#邮件传输主要用到了
SmtpClient 和MailMessage 类。
SmtpClient类是客户端类,客户端的Send方法实现了发送邮件的功能。Send方法的参数是MailMessage对象。
MailMessage类是邮件信息类,通过From属性可以设置发送者。To属性设置接受者。CC抄送者。
Subject 标题;Body、内容。
注意:SmtpClient 的用户名和MailMessage 的发送者要保持一致。
要引用 System.Net.Mail
/// <summary> /// 发送邮件类 /// </summary> public class SmtpEmailHelper { #region Properties /// <summary> /// 发送邮箱全地址 /// </summary> public string SmtpUserName { get; set; } /// <summary> /// 发送邮箱显示名 /// </summary> public string SmtpDisplayName { get; set; } /// <summary> /// 发送邮箱密码 /// </summary> public string SmtpPassWord { get; set; } /// <summary> /// 邮箱服务器地址 /// </summary> public string SmtpServer { get; set; } /// <summary> /// 邮箱服务器端口 /// </summary> public int SmtpPort { get; set; } /// <summary> /// 是否ssl加密 /// </summary> public bool SmtpEnableSsl { get; set; } #endregion /// <summary> /// /// </summary> public SmtpEmailHelper() { } /// <summary> /// /// </summary> /// <param name="userName">发送邮箱全地址</param> /// <param name="passWord">发送邮箱密码</param> /// <param name="displayName">发送邮箱显示名</param> /// <param name="server">邮箱服务器地址</param> /// <param name="port">邮箱服务器端口</param> /// <param name="enableSsl">是否ssl加密</param> public SmtpEmailHelper(string userName, string passWord, string displayName, string server, int port, bool enableSsl = true) : this() { this.SmtpUserName = userName; this.SmtpPassWord = passWord; this.SmtpDisplayName = displayName; this.SmtpServer = server; this.SmtpPort = port; this.SmtpEnableSsl = enableSsl; } /// <summary> /// 发送邮件 /// </summary> /// <param name="subject">主题</param> /// <param name="body">内容消息正文</param> /// <param name="mailTo">收件人</param> /// <returns></returns> public bool Send(string subject, string body, string mailTo) { string[] arrMailTo = new string[] { mailTo }; string[] attachments = null; MailPriority priority = MailPriority.Normal; return Send(subject, body, arrMailTo, null, attachments, null, System.Text.Encoding.UTF8, priority, true); } /// <summary> /// 发送邮件 /// </summary> /// <param name="subject">主题</param> /// <param name="body">内容消息正文</param> /// <param name="mailTo">收件人</param> /// <param name="attachments">附件</param> /// <returns></returns> public bool Send(string subject, string body, string mailTo, string[] attachments) { string[] arrMailTo = new string[] { mailTo }; MailPriority priority = MailPriority.Normal; return Send(subject, body, arrMailTo, null, attachments, null, System.Text.Encoding.UTF8, priority, true); } /// <summary> /// 发送邮件 /// </summary> /// <param name="subject">主题</param> /// <param name="body">内容消息正文</param> /// <param name="mailTo">收件人</param> /// <param name="mailCC">抄送人</param> /// <param name="attachments">附件</param> /// <returns></returns> public bool Send(string subject, string body, string mailTo, string[] mailCC, string[] attachments) { string[] arrMailTo = new string[] { mailTo }; MailPriority priority = MailPriority.Normal; return Send(subject, body, arrMailTo, mailCC, attachments, null, System.Text.Encoding.UTF8, priority, true); } /// <summary> /// 发送邮件 /// </summary> /// <param name="subject">主题</param> /// <param name="body">内容消息正文</param> /// <param name="mailTo">收件人</param> /// <param name="attachments">附件</param> /// <param name="priority">邮件优先级</param> /// <returns></returns> public bool Send(string subject, string body, string[] mailTo, string[] attachments, MailPriority priority = MailPriority.Normal) { return Send(subject, body, mailTo,null, attachments, null, System.Text.Encoding.UTF8, priority, true); } /// <summary> /// 发送邮件 /// </summary> /// <param name="subject">主题</param> /// <param name="body">内容消息正文</param> /// <param name="mailTo">收件人</param> /// <param name="mailCC">抄送人</param> /// <param name="attachments">附件</param> /// <param name="bccs">密件抄送地址</param> /// <param name="bodyEncoding">编码</param> /// <param name="priority">邮件优先级</param> /// <param name="IsBodyHtml">是否是HTML邮件</param> /// <returns></returns> public bool Send(string subject, string body,string[] mailTo,string[] mailCC,string[] attachments, string[] bccs, System.Text.Encoding bodyEncoding, MailPriority priority = MailPriority.Normal, bool IsBodyHtml = true ) { //创建Email实体 System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(); mailMessage.From = new MailAddress(SmtpUserName, SmtpDisplayName,bodyEncoding); mailMessage.Subject = subject; mailMessage.SubjectEncoding = bodyEncoding;//邮件标题编码 mailMessage.Body = body; mailMessage.BodyEncoding = bodyEncoding;//邮件内容编码 mailMessage.IsBodyHtml = true;//是否是HTML邮件 mailMessage.Priority = priority;//邮件优先级 //插入附件 if (attachments != null) { foreach (var attachment in attachments) { if (!string.IsNullOrWhiteSpace(attachment) && System.IO.File.Exists(attachment)) { //建立邮件附件类的一个对象,语法格式为System.Net.Mail.Attachment(文件名,文件格式) var AttFile = new Attachment(attachment, System.Net.Mime.MediaTypeNames.Application.Octet); AttFile.Name = System.IO.Path.GetFileName(attachment); AttFile.NameEncoding = bodyEncoding; // MIME协议下的一个对象,用以设置附件的创建时间,修改时间以及读取时间 System.Net.Mime.ContentDisposition disposition = AttFile.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(attachment); disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachment); disposition.ReadDate = System.IO.File.GetLastAccessTime(attachment); mailMessage.Attachments.Add(AttFile); } } } //插入收件人地址,抄送地址和密件抄送地址 if (null != mailTo) { foreach (var to in mailTo.Where(m => !string.IsNullOrWhiteSpace(m))) { mailMessage.To.Add(new MailAddress(to)); } } if (null != mailCC) { foreach (var cc in mailCC.Where(m => !string.IsNullOrWhiteSpace(m))) { mailMessage.CC.Add(new MailAddress(cc)); } } if (null != bccs) { foreach (var bcc in bccs.Where(m => !string.IsNullOrWhiteSpace(m))) { mailMessage.Bcc.Add(new MailAddress(bcc)); } } //创建Smtp客户端 SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential(SmtpUserName, SmtpPassWord); //上述写你的邮箱和密码 client.Port = SmtpPort; //使用的端口 client.Host = SmtpServer; client.EnableSsl = SmtpEnableSsl; //经过ssl加密. object userState = mailMessage; try { //发送邮件 client.Send(mailMessage); //client.SendAsync(mailMessage); return true; } catch (Exception ex) { return false; } } }