コード例 #1
0
ファイル: MailHelper.cs プロジェクト: jiasw/TaskManager
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="Receiver">邮件接收人</param>
        /// <param name="Subject">邮件主题</param>
        /// <param name="content">邮件内容</param>
        /// <returns>发送状态</returns>
        public static SMSCode SendMessage(string Receiver, string Subject,string content)
        {

            if (string.IsNullOrEmpty(Receiver) || string.IsNullOrEmpty(Subject)
                || string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("SendMessage参数空异常!");
            }
            if (Config == null)
            {
                Config = JsonConvert.DeserializeObject<MailConfig>(MailInfo);
                if (string.IsNullOrEmpty(Config.SmtpHost) || string.IsNullOrEmpty(Config.SendMail) ||
                    string.IsNullOrEmpty(Config.UserName) || string.IsNullOrEmpty(Config.Password))
                {
                    throw new ArgumentNullException("配置文件节点MailInfo信息错误,相关信息为空!");
                }
            }
            if (client == null)
            {
                try
                {
                    client = new System.Net.Mail.SmtpClient();
                    client.Host = Config.SmtpHost;
                    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = true;
                    client.Credentials = new System.Net.NetworkCredential(Config.UserName, Config.Password);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            try
            {
                System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
                Message.SubjectEncoding = System.Text.Encoding.UTF8;
                Message.BodyEncoding = System.Text.Encoding.UTF8;
                Message.Priority = System.Net.Mail.MailPriority.High;

                Message.From = new System.Net.Mail.MailAddress(Config.SendMail, Config.DisplayName);
                //添加邮件接收人地址
                string[] receivers = Receiver.Split(new char[] { ',' });
                Array.ForEach(receivers.ToArray(), ToMail => { Message.To.Add(ToMail); });

                Message.Subject = Subject;
                Message.Body = content;

                Message.IsBodyHtml = true;
                client.Send(Message);
                return SMSCode.Success;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("邮件发送失败", ex);
                return SMSCode.Exception;
            }
        }
コード例 #2
0
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="Receiver">邮件接收人</param>
        /// <param name="Subject">邮件主题</param>
        /// <param name="content">邮件内容</param>
        /// <returns>发送状态</returns>
        public static SMSCode SendMessage(string Receiver, string Subject, string content)
        {
            if (string.IsNullOrEmpty(Receiver) || string.IsNullOrEmpty(Subject) ||
                string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("SendMessage参数空异常!");
            }
            if (Config == null)
            {
                Config = JsonConvert.DeserializeObject <MailConfig>(MailInfo);
                if (string.IsNullOrEmpty(Config.SmtpHost) || string.IsNullOrEmpty(Config.SendMail) ||
                    string.IsNullOrEmpty(Config.UserName) || string.IsNullOrEmpty(Config.Password))
                {
                    throw new ArgumentNullException("配置文件节点MailInfo信息错误,相关信息为空!");
                }
            }
            if (client == null)
            {
                try
                {
                    client                       = new System.Net.Mail.SmtpClient();
                    client.Host                  = Config.SmtpHost;
                    client.DeliveryMethod        = System.Net.Mail.SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = true;
                    client.Credentials           = new System.Net.NetworkCredential(Config.UserName, Config.Password);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            try
            {
                System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
                Message.SubjectEncoding = System.Text.Encoding.UTF8;
                Message.BodyEncoding    = System.Text.Encoding.UTF8;
                Message.Priority        = System.Net.Mail.MailPriority.High;

                Message.From = new System.Net.Mail.MailAddress(Config.SendMail, Config.DisplayName);
                //添加邮件接收人地址
                string[] receivers = Receiver.Split(new char[] { ',' });
                Array.ForEach(receivers.ToArray(), ToMail => { Message.To.Add(ToMail); });

                Message.Subject = Subject;
                Message.Body    = content;

                Message.IsBodyHtml = true;
                client.Send(Message);
                return(SMSCode.Success);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("邮件发送失败", ex);
                return(SMSCode.Exception);
            }
        }