C# .NET 发送邮件 代码
2015-09-28 16:01
489 查看
using Common.Log; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Mail; using System.Net.Security; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography.X509Certificates; namespace Common.Tools { public class SendEmail { /// <summary> /// 发送邮件 /// </summary> /// <param name="MessageTo">收件人</param> /// <param name="MessageSubject">邮件主题</param> /// <param name="MessageBody">邮件正文</param> /// <returns></returns> public static bool Send(string MessageTo, string MessageSubject, string MessageBody) { //此处注册只为防止个别邮箱证书验证失败问题 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); MailAddress MessageFrom = new MailAddress("***@qq.com"); MailMessage message = new MailMessage(); message.From = MessageFrom; message.To.Add(MessageTo); //收件人邮箱地址可以是多个以实现群发 message.Subject = MessageSubject; message.Body = MessageBody; message.IsBodyHtml = true; //是否为html格式 message.Priority = MailPriority.High; //发送邮件的优先等级 SmtpClient sc = new SmtpClient(); sc.Host = "smtp.qq.com"; //指定发送邮件的服务器地址或IP sc.Port = 587; //一个是143,一个25 //sc.Port = 587; //指定发送邮件端口 sc.UseDefaultCredentials = true; sc.EnableSsl = true; sc.Credentials = new System.Net.NetworkCredential("***@qq.com", "密码"); //指定登录服务器的用户名和密码 try { sc.Send(message); //发送邮件 } catch (Exception ex) { WriteLog.WriteErrorLog(ex.ToString()); return false; } return true; } private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; //throw new NotImplementedException(); } } }
相关文章推荐
- C#实现二叉树遍历算法
- C# form 传参数的几个方法
- C#中form窗体之间的传递几个方法学习
- C#与闭包(closure)学习笔记
- C#与重构(入门)
- 基于C#实现简单离线注册码生成与验证
- C#委托(delegate、Action、Func、predicate)
- c# 使用protobuf
- C#开发中遇到问题常问题
- 控制台I/O
- C#中的MessageBox的show的各种重载方法使用
- C#编写的windows服务安装后启动提示“服务启动后又停止了”
- windbg调试C#代码(一)
- 编译生成.NET DLL并调用
- C#操作IIS方法集合
- c#创建ISS站点
- WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
- C# 操作word总结(一)——建立文档和添加页眉页脚
- C#学习之事件
- C# EPL USB 指令打印