C#邮件发送
public class EmailHelper { public class MyEmail { private MailMessage mMailMessage; //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等) private SmtpClient mSmtpClient; //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等) private int mSenderPort; //发送邮件所用的端口号(htmp协议默认为25) private string mSenderServerHost; //发件箱的邮件服务器地址(IP形式或字符串形式均可) private string mSenderPassword; //发件箱的密码 private string mSenderUsername; //发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello) private bool mEnableSsl; //是否对邮件内容进行socket层加密传输 private bool mEnablePwdAuthentication; //是否对发件人邮箱进行密码验证 ///<summary> /// 构造函数 ///</summary> ///<param name="server">发件箱的邮件服务器地址</param> ///<param name="toMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param> ///<param name="fromMail">发件人地址</param> ///<param name="subject">邮件标题</param> ///<param name="emailBody">邮件内容(可以以html格式进行设计)</param> ///<param name="username">发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)</param> ///<param name="password">发件人邮箱密码</param> ///<param name="port">发送邮件所用的端口号(htmp协议默认为25)</param> ///<param name="sslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param> ///<param name="pwdCheckEnable">true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证</param> public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable) { try { mMailMessage = new MailMessage(); mMailMessage.To.Add(toMail); mMailMessage.From = new MailAddress(fromMail); mMailMessage.Subject = subject; mMailMessage.Body = emailBody; mMailMessage.IsBodyHtml = true; mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; mMailMessage.Priority = MailPriority.Normal; this.mSenderServerHost = server; this.mSenderUsername = username; this.mSenderPassword = password; this.mSenderPort = Convert.ToInt32(port); this.mEnableSsl = sslEnable; this.mEnablePwdAuthentication = pwdCheckEnable; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } ///<summary> /// 添加附件 ///</summary> ///<param name="attachmentsPath">附件的路径集合,以分号分隔</param> public void AddAttachments(string attachmentsPath) { try { string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义 Attachment data; ContentDisposition disposition; for (int i = 0; i < path.Length; i++) { data = new Attachment(path[i], MediaTypeNames.Application.Octet); disposition = data.ContentDisposition; disposition.CreationDate = File.GetCreationTime(path[i]); disposition.ModificationDate = File.GetLastWriteTime(path[i]); disposition.ReadDate = File.GetLastAccessTime(path[i]); mMailMessage.Attachments.Add(data); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } ///<summary> /// 邮件的发送 ///</summary> public void Send() { try { if (mMailMessage != null) { mSmtpClient = new SmtpClient(); //mSmtpClient.Host = "smtp." + mMailMessage.From.Host; mSmtpClient.Host = this.mSenderServerHost; mSmtpClient.Port = this.mSenderPort; mSmtpClient.UseDefaultCredentials = true; mSmtpClient.EnableSsl = this.mEnableSsl; if (this.mEnablePwdAuthentication) { System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); //NTLM: Secure Password Authentication in Microsoft Outlook Express mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM"); } else { mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); } mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; mSmtpClient.Send(mMailMessage); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } /// <summary> /// 发送邮件 /// </summary> /// <param name="send"></param> /// <param name="recieve"></param> /// <param name="subject"></param> /// <param name="mailbody"></param> /// <param name="host"></param> /// <param name="uname"></param> /// <param name="pwd"></param> /// <param name="strFileName"></param> public void SendMail(string send, string recieve, string subject, string mailbody, string host, string uname, string pwd, string strFileName) { //生成一个 使用SMTP发送邮件的客户端对象 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); //生成一个主机IP //client.Port = 25; //587, 465, 995 client.Host = host; //表示不以当前登录用户的默认凭据进行身份验证 client.UseDefaultCredentials = true; //包含用户名和密码 if (uname != "") { client.Credentials = new System.Net.NetworkCredential(uname, pwd); } //指定如何发送电子邮件。 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis; //通过本机SMTP服务器传送该邮件, //其实使用该项的话就可以随意设定“主机,发件者昵称, 密码”,因为你的IIS服务器已经设定好了。而且公司内部发邮件是不需要验证的。 System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add(recieve); message.From = new System.Net.Mail.MailAddress(send, uname, System.Text.Encoding.UTF8); message.Subject = subject; message.Body = mailbody; //定义邮件正文,主题的编码方式 message.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8"); message.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //获取或设置一个值,该值指示电子邮件正文是否为 HTML。 message.IsBodyHtml = false; //指定邮件优先级 message.Priority = System.Net.Mail.MailPriority.High; //添加附件 //System.Net.Mail.Attachment data = new Attachment(@"E:\9527\tubu\PA260445.JPG", System.Net.Mime.MediaTypeNames.Application.Octet); if (strFileName != "" && strFileName != null) { Attachment data = new Attachment(strFileName); message.Attachments.Add(data); } try { //发送 client.Send(message); Console.WriteLine("发送成功!"); } catch (System.Net.Mail.SmtpException ex) { Console.WriteLine("发送失败:" + ex.Message); } } } }
使用方法:
static void Main(string[] args) { try {//使用foxmail发件服务器 string senderServerHost = "mail.企业.com"; //目标邮箱 string toMailAddress = "XX@qq.com"; //发送邮箱 string fromMailAddress = "xx@企业.com"; //测试 string subjectInfo = "测试fox"; //内容 string bodyInfo = "正在测试的内容"; string mailUsername = "用户名"; //发送邮箱的授权码 //string mailPassword = "授权码"; //发送邮箱的授权码 string mailPassword = "密码"; //端口号 string mailPort = "25";
//文件全路径 string attachPath = "D:\\Logger\\202010\\20.log; D:\\Logger\\202010\\22.log;"; MyEmail email = new MyEmail(senderServerHost, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false); email.AddAttachments(attachPath); email.Send(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.ReadKey(); }
发件服务器查看POP3开启
感谢:https://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html