Exemplo n.º 1
0
 /// <summary>
 /// 发送邮件
 /// </summary>
 /// <param name="MailTo">接收人用户名,单封邮件</param>
 /// <param name="MailSubject">邮件主题</param>
 /// <param name="MailBody">邮件内容</param>
 /// <param name="IsHtml">邮件正文是否为HTML格式</param>
 /// <param name="_MailServer"></param>
 /// <returns></returns>
 public static bool SendOK(string MailTo, string MailSubject, string MailBody, bool IsHtml, JumboTCMS.Entity.MailServer _MailServer)
 {
     if (_MailServer == null)
         return false;
     if (_MailServer.FromAddresss.Count == 0)
         return false;
     bool _SendOK = false;
     for (int i = 0; i < _MailServer.FromAddresss.Count; i++)
     {
         JumboTCMS.Utils.Mail.MailMessage message = new JumboTCMS.Utils.Mail.MailMessage();
         message.MaxRecipientNum = 80;//最大收件人
         message.From = System.Configuration.ConfigurationManager.AppSettings["JumboTCMS:WebmasterEmail"];
         message.FromName = _MailServer.FromNames[i].ToString();
         string[] _mail = MailTo.Split(',');
         for (int j = 0; j < _mail.Length; j++)
         {
             message.AddRecipients(_mail[j]);
         }
         message.Subject = MailSubject;
         if (IsHtml)
             message.BodyFormat = JumboTCMS.Utils.Mail.MailFormat.HTML;
         else
             message.BodyFormat = JumboTCMS.Utils.Mail.MailFormat.Text;
         message.Priority = JumboTCMS.Utils.Mail.MailPriority.Normal;
         message.Body = MailBody;
         JumboTCMS.Utils.Mail.SmtpClient smtp = new JumboTCMS.Utils.Mail.SmtpClient(_MailServer.SmtpHosts[i].ToString(), Convert.ToInt16(_MailServer.SmtpPorts[i].ToString()));
         if (smtp.Send(message, _MailServer.FromAddresss[i].ToString(), _MailServer.FromPwds[i].ToString()))
         {
             string strXmlFile = HttpContext.Current.Server.MapPath("~/_data/config/jcms(emailserver).config");
             JumboTCMS.DBUtility.XmlControl XmlTool = new JumboTCMS.DBUtility.XmlControl(strXmlFile);
             XmlTool.Update("Mails/Mail[FromAddress=\"" + _MailServer.FromAddresss[i].ToString() + "\"]/Used", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
             XmlTool.Save();
             XmlTool.Dispose();
             _SendOK = true;
             SaveSucLog(MailTo, _MailServer.FromAddresss[i].ToString(), _MailServer.FromNames[i].ToString(), _MailServer.SmtpHosts[i].ToString());
             break;//跳出循环
         }
         else
         {
             SaveErrLog(MailTo, _MailServer.FromAddresss[i].ToString(), _MailServer.FromNames[i].ToString(), _MailServer.SmtpHosts[i].ToString(), smtp.ErrMsg + "\r\n当前共有:" + _MailServer.FromAddresss.Count + "个发件人在队列中.");
             continue;
         }
         //try
         //{
         //    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
         //    message.From = new System.Net.Mail.MailAddress(_MailServer.FromAddresss[i].ToString(), _MailServer.FromNames[i].ToString(), Encoding.GetEncoding("gb2312"));
         //    string[] _mail = MailTo.Split(',');
         //    for (int j = 0; j < _mail.Length; j++)
         //    {
         //        message.To.Add(_mail[j]);
         //    }
         //    message.Subject = MailSubject;
         //    message.Body = MailBody;
         //    message.IsBodyHtml = IsHtml;
         //    message.Priority = System.Net.Mail.MailPriority.Normal;
         //    message.BodyEncoding = Encoding.GetEncoding("gb2312");
         //    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(_MailServer.SmtpHosts[i].ToString());
         //    smtp.Credentials = new System.Net.NetworkCredential(_MailServer.FromAddresss[i].ToString(), _MailServer.FromPwds[i].ToString());
         //    smtp.Send(message);
         //    string strXmlFile = HttpContext.Current.Server.MapPath("~/_data/config/jcms(emailserver).config");
         //    JumboTCMS.DBUtility.XmlControl XmlTool = new JumboTCMS.DBUtility.XmlControl(strXmlFile);
         //    XmlTool.Update("Mails/Mail[FromAddress=\"" + _MailServer.FromAddresss[i].ToString() + "\"]/Used", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
         //    XmlTool.Save();
         //    XmlTool.Dispose();
         //    _SendOK = true;
         //    break;//跳出循环
         //}
         //catch (Exception)
         //{ }
     }
     return _SendOK;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="MailTo">接收人用户名,单封邮件</param>
        /// <param name="MailSubject">邮件主题</param>
        /// <param name="MailBody">邮件内容</param>
        /// <param name="IsHtml">邮件正文是否为HTML格式</param>
        /// <param name="MailFrom">发件人邮箱地址</param>
        /// <param name="MailFromName">发件人署名</param>
        /// <param name="MailPwd">发件人邮箱密码</param>
        /// <param name="MailSmtpHost">发件人邮箱Host,如"smtp.sina.com"</param>
        public static bool SendOK(string MailTo, string MailSubject, string MailBody, bool IsHtml, string MailFrom, string MailFromName, string MailPwd, string MailSmtpHost, int MailSmtpPort)
        {
            JumboTCMS.Utils.Mail.MailMessage message = new JumboTCMS.Utils.Mail.MailMessage();
            message.MaxRecipientNum = 80;//最大收件人
            message.From = System.Configuration.ConfigurationManager.AppSettings["JumboTCMS:WebmasterEmail"];
            message.FromName = MailFromName;
            string[] _mail = MailTo.Split(',');
            for (int j = 0; j < _mail.Length; j++)
            {
                message.AddRecipients(_mail[j]);
            }
            message.Subject = MailSubject;
            if (IsHtml)
                message.BodyFormat = JumboTCMS.Utils.Mail.MailFormat.HTML;
            else
                message.BodyFormat = JumboTCMS.Utils.Mail.MailFormat.Text;
            message.Priority = JumboTCMS.Utils.Mail.MailPriority.Normal;
            message.Body = MailBody;
            JumboTCMS.Utils.Mail.SmtpClient smtp = new JumboTCMS.Utils.Mail.SmtpClient(MailSmtpHost, MailSmtpPort);
            if (smtp.Send(message, MailFrom, MailPwd))
                return true;
            else
            {
                SaveErrLog(MailTo, MailFrom, MailFromName, MailSmtpHost, smtp.ErrMsg);
                return false;
            }

            //try
            //{
            //    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            //    message.From = new System.Net.Mail.MailAddress(MailFrom, MailFromName, Encoding.GetEncoding("gb2312"));
            //    string[] _mail = MailTo.Split(',');
            //    for (int j = 0; j < _mail.Length; j++)
            //    {
            //        message.To.Add(_mail[j]);
            //    }
            //    message.Subject = MailSubject;
            //    message.Body = MailBody;
            //    message.IsBodyHtml = IsHtml;
            //    message.Priority = System.Net.Mail.MailPriority.Normal;
            //    message.BodyEncoding = Encoding.GetEncoding("gb2312");
            //    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(MailSmtpHost);
            //    smtp.Credentials = new System.Net.NetworkCredential(MailFrom, MailPwd);
            //    smtp.Send(message);
            //}
            //catch (Exception)
            //{
            //    return false;
            //}
            //return true;
        }