C# .NET 发送邮件 代码

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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值