/// <summary> /// SendMail 的摘要说明 /// </summary> public class SendMail { public SendMail() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 发送邮件 /// </summary> /// <returns></returns> public static bool SendEmail(string EmailPath, string EmailName, string EmailTitle, string EmailContent) { SmtpClient client = new SmtpClient(); //获取或设置用于验证发件人身份的凭据。 client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["EmailUser"], ConfigurationManager.AppSettings["EmailPwd"]); //经过ssl(安全套接层)加密,163邮箱SSL协议端口号为465/994,关闭SSL时端口为25, //qq邮箱SSL协议端口号为465或587,关闭SSL时端口同样为25,不过用SSL加密后发送邮件都失败,具体原因不知 //client.EnableSsl = true; client.Port = Convert.ToInt32(ConfigurationManager.AppSettings["EmailPort"]); //端口号 client.Host = ConfigurationManager.AppSettings["EmailHost"]; //获取或设置用于 SMTP 事务的主机的名称或 IP 地址 try { client.Send(InitMailMessage(EmailPath, EmailName, EmailTitle, EmailContent)); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } } /// <summary> /// 发送邮件(自定义发送配置) /// </summary> /// <param name="reEmailPath">收件人邮箱地址</param> /// <param name="reEmailName">收件人名称</param> /// <param name="sendEmailTitle">发件标题</param> /// <param name="sendEmailContent">发件内容</param> /// <param name="sendEmailUser">发件人账户</param> /// <param name="sendEmailPwd">发件人密码</param> /// <param name="sendEmailName">发件人名称</param> /// <param name="sendEmailPort">端口</param> /// <param name="sendEmailHost">发送SMTP地址</param> /// <returns></returns> public static bool SendEmail(string reEmailPath, string reEmailName, string sendEmailTitle, string sendEmailContent, string sendEmailUser, string sendEmailPwd, string sendEmailName, string sendEmailPort, string sendEmailHost) { SmtpClient client = new SmtpClient(); //设置为发送认证消息 client.UseDefaultCredentials = true; //string Email = EmailUser.Split('@')[0]; //获取或设置用于验证发件人身份的凭据。 client.Credentials = new System.Net.NetworkCredential(sendEmailUser, sendEmailPwd); //经过ssl(安全套接层)加密,163邮箱SSL协议端口号为465/994,关闭SSL时端口为25, //qq邮箱SSL协议端口号为465或587,关闭SSL时端口同样为25,不过用SSL加密后发送邮件都失败,具体原因不知 //client.EnableSsl = true; client.Port = Convert.ToInt32(sendEmailPort); //端口号 client.Host = sendEmailHost; //获取或设置用于 SMTP 事务的主机的名称或 IP 地址 try { client.Send(InitMailMessage(reEmailPath, reEmailName, sendEmailTitle, sendEmailContent, sendEmailUser, sendEmailName)); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } } /// <summary> /// 初始化信件相关信息 /// </summary> /// <param name="reEmailPath">发件人</param> /// <param name="reEmailName">发件人姓名</param> /// <param name="sendEmailTitle">发送标题</param> /// <param name="sendEmailContent">发送内容</param> /// <returns></returns> public static MailMessage InitMailMessage(string reEmailPath, string reEmailName, string sendEmailTitle, string sendEmailContent) { MailMessage mail = new MailMessage(); mail.From = new MailAddress(ConfigurationManager.AppSettings["EmailUser"], ConfigurationManager.AppSettings["EmailName"]); //发件人 if (reEmailName != "") { MailAddress mailAdd = new MailAddress(reEmailPath, reEmailName); //收件人 mail.To.Add(mailAdd); //收件人 } else { mail.To.Add(reEmailPath); //收件人 } mail.Subject = sendEmailTitle; //主题 mail.Body = sendEmailContent; //内容 //邮件主题和正文的编码格式 mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = true; //邮件正文允许html编码 mail.Priority = MailPriority.Normal; //优先级 //密送——就是将信密秘抄送给收件人以外的人,所有收件人看不到密件抄送的地址 //mail.Bcc.Add("823904362@qq.com"); //抄送——就是将信抄送给收件人以外的人,所有的收件人可以在抄送地址处看到此信还抄送给谁 //mail.CC.Add("823904362@qq.com"); //mail.Attachments.Add(new Attachment("D:\\1.doc")); //添加附件 return mail; } /// <summary> /// 发送邮件 /// </summary> /// <returns></returns> public static bool SendEmail(string EmailPath, string EmailName, string EmailTitle, string EmailContent, string attPath) { SmtpClient client = new SmtpClient(); //获取或设置用于验证发件人身份的凭据。 client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["EmailUser"], ConfigurationManager.AppSettings["EmailPwd"]); //经过ssl(安全套接层)加密,163邮箱SSL协议端口号为465/994,关闭SSL时端口为25, //qq邮箱SSL协议端口号为465或587,关闭SSL时端口同样为25,不过用SSL加密后发送邮件都失败,具体原因不知 //client.EnableSsl = true; client.Port = Convert.ToInt32(ConfigurationManager.AppSettings["EmailPort"]); //端口号 client.Host = ConfigurationManager.AppSettings["EmailHost"]; //获取或设置用于 SMTP 事务的主机的名称或 IP 地址 try { client.Send(InitMailMessage(EmailPath, EmailName, EmailTitle, EmailContent, attPath)); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } } /// <summary> /// 初始化信件相关信息 /// </summary> /// <param name="address">发送地址</param> /// <param name="title">标题</param> /// <param name="content">内容</param> /// <returns></returns> public static MailMessage InitMailMessage(string address, string name, string title, string content, string attPath) { MailMessage mail = new MailMessage(); mail.From = new MailAddress(ConfigurationManager.AppSettings["EmailUser"], ConfigurationManager.AppSettings["EmailName"]); //发件人 if (name != "") { MailAddress mailAdd = new MailAddress(address, name); //收件人 mail.To.Add(mailAdd); //收件人 } else { mail.To.Add(address); //收件人 } mail.Subject = title; //主题 mail.Body = content; //内容 //邮件主题和正文的编码格式 mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = true; //邮件正文允许html编码 mail.Priority = MailPriority.Normal; //优先级 //密送——就是将信密秘抄送给收件人以外的人,所有收件人看不到密件抄送的地址 //mail.Bcc.Add("823904362@qq.com"); //抄送——就是将信抄送给收件人以外的人,所有的收件人可以在抄送地址处看到此信还抄送给谁 //mail.CC.Add("823904362@qq.com"); Attachment attach = new Attachment(attPath, "application/x-zip-compressed"); mail.Attachments.Add(attach); //mail.Attachments.Add(new Attachment("D:\\1.doc")); //添加附件 return mail; } /// <summary> /// 发送邮件(群发) /// </summary> /// <returns></returns> public static bool SendEmail(string[] EmailPath, string[] EmailName, string EmailTitle, string EmailContent) { SmtpClient client = new SmtpClient(); //获取或设置用于验证发件人身份的凭据。 client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["EmailUser"], ConfigurationManager.AppSettings["EmailPwd"]); //经过ssl(安全套接层)加密,163邮箱SSL协议端口号为465/994,关闭SSL时端口为25, //qq邮箱SSL协议端口号为465或587,关闭SSL时端口同样为25,不过用SSL加密后发送邮件都失败,具体原因不知 //client.EnableSsl = true; client.Port = Convert.ToInt32(ConfigurationManager.AppSettings["EmailPort"]); //端口号 client.Host = ConfigurationManager.AppSettings["EmailHost"]; //获取或设置用于 SMTP 事务的主机的名称或 IP 地址 try { client.Send(InitMailMessage(EmailPath, EmailName, EmailTitle, EmailContent)); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } } /// <summary> /// 初始化信件相关信息(群发) /// </summary> /// <param name="address">发送地址</param> /// <param name="title">标题</param> /// <param name="content">内容</param> /// <returns></returns> public static MailMessage InitMailMessage(string[] address, string[] name, string title, string content) { MailMessage mail = new MailMessage(); mail.From = new MailAddress(ConfigurationManager.AppSettings["EmailUser"], ConfigurationManager.AppSettings["EmailName"]); //发件人 if (name != null && name.Length > 0) { if (address != null && address.Length > 0) { for (int i = 0; i < address.Length; i++) { if (address != null && address[i] != null && address[i] != "") { string nameStr = name != null && name[i] != null ? name[i].ToString() : ""; MailAddress mailAdd = new MailAddress(address[i], nameStr); //收件人 mail.To.Add(mailAdd); //收件人 } } } } else { for (int i = 0; i < address.Length; i++) { if (address[i] != null && address[i] != "") { mail.To.Add(address[i]); //收件人 } } } mail.Subject = title; //主题 mail.Body = content; //内容 //邮件主题和正文的编码格式 mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = true; //邮件正文允许html编码 mail.Priority = MailPriority.Normal; //优先级 //密送——就是将信密秘抄送给收件人以外的人,所有收件人看不到密件抄送的地址 //mail.Bcc.Add("823904362@qq.com"); //抄送——就是将信抄送给收件人以外的人,所有的收件人可以在抄送地址处看到此信还抄送给谁 //mail.CC.Add("823904362@qq.com"); //mail.Attachments.Add(new Attachment("D:\\1.doc")); //添加附件 return mail; } /// <summary> /// 初始化信件相关信息 /// </summary> /// <param name="reEmailPath">收件人</param> /// <param name="reEmailName">收件人名称</param> /// <param name="sendEmailTitle">发送标题</param> /// <param name="sendEmailContent">发送内容</param> /// <param name="sendEmailUser">发件人</param> /// <param name="sendEmailName">发件人名称</param> /// <returns></returns> public static MailMessage InitMailMessage(string reEmailPath, string reEmailName, string sendEmailTitle, string sendEmailContent, string sendEmailUser, string sendEmailName) { MailMessage mail = new MailMessage(); mail.From = new MailAddress(sendEmailUser, sendEmailName); //发件人 if (reEmailPath != "") { MailAddress mailAdd = new MailAddress(reEmailPath, reEmailName); //收件人 mail.To.Add(mailAdd); //收件人 } else { mail.To.Add(reEmailPath); //收件人 } mail.Subject = sendEmailTitle; //主题 mail.Body = sendEmailContent; //内容 //邮件主题和正文的编码格式 mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = true; //邮件正文允许html编码 mail.Priority = MailPriority.Normal; //优先级 //密送——就是将信密秘抄送给收件人以外的人,所有收件人看不到密件抄送的地址 //mail.Bcc.Add("823904362@qq.com"); //抄送——就是将信抄送给收件人以外的人,所有的收件人可以在抄送地址处看到此信还抄送给谁 //mail.CC.Add("823904362@qq.com"); //mail.Attachments.Add(new Attachment("D:\\1.doc")); //添加附件 return mail; } }
16
2015
04
C#、Asp.Net 发送邮件工具类
发布:郑德才博客 | 分类:学习之路 | 评论:0 | 浏览:
相关文章:
C#、Asp.Net 对比两个实体信息前后是否有所改变(结构相同和不相同) (2016-9-2 20:24:29)
C#、Asp.Net 将一个实体对象转换为另一个实体对象(结构可以不一样) (2016-9-2 19:14:5)
Asp.Net 常用时间计算 (2016-3-16 23:29:20)
【C#、Asp.Net 工具类大全】图片通用操作类 (2015-12-23 13:27:30)
【C#、Asp.Net 工具类大全】Request请求工具类 (2015-12-23 13:15:56)
【C#、Asp.Net 工具类大全】弹出提示操作类 (2015-12-23 13:6:1)
【C#、Asp.Net 工具类大全】正则匹配工具类 (2015-7-26 9:36:56)
【C#、Asp.Net 工具类大全】Js常用操作类 (2015-7-15 14:23:33)
【C#、Asp.Net 工具类大全】Html常用帮助类 (2015-7-11 23:42:53)
C#字符串编码帮助类 (2015-7-11 23:39:38)
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。