Esempio n. 1
0
        public static bool SendEmailSCM(string sToMail, string sCcMail, string sBccMail, string sSubject, string sMessage, bool bIsHtml)
        {
            bool        bResult   = false;
            DLLConfig   dllconfig = new DLLConfig();
            MailMessage Msg       = new MailMessage();

            string[] sTo = (DLLConfig.DebugMode ? dllconfig.AdminMailList.Split(';') : sToMail.Split(';'));
            for (int i = 0; i < sTo.Length; i++)
            {
                if (!string.IsNullOrEmpty(sTo[i]))
                {
                    Msg.To.Add(new MailAddress(sTo[i]));
                }
            }
            string[] sCC = sCcMail.Split(';');
            for (int i = 0; i < sCC.Length; i++)
            {
                if (!string.IsNullOrEmpty(sCC[i]))
                {
                    Msg.CC.Add(new MailAddress(sCC[i]));
                }
            }
            string[] sBcc = sBccMail.Split(';');
            for (int i = 0; i < sBcc.Length; i++)
            {
                if (!string.IsNullOrEmpty(sBcc[i]))
                {
                    Msg.Bcc.Add(new MailAddress(sBcc[i]));
                }
            }
            #region 通知底圖
            //底圖檔案
            LinkedResource lrHead = new LinkedResource(@"D:\Schedule\SyncERP\Notify-Head.jpg");
            lrHead.ContentId   = "Notify-Head";
            lrHead.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);
            LinkedResource lrFooter = new LinkedResource(@"D:\Schedule\SyncERP\Notify-Footer.jpg");
            lrFooter.ContentId   = "Notify-Footer";
            lrFooter.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);
            //郵件格式
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(sMessage, null, "text/html");
            htmlView.LinkedResources.Add(lrHead);
            htmlView.LinkedResources.Add(lrFooter);
            Msg.AlternateViews.Add(htmlView);
            #endregion

            Msg.From         = new MailAddress(dllconfig.MailSender);
            Msg.Subject      = sSubject;//(dllconfig.DebugMode ? "[SCM測試]" : "[SCM]")
            Msg.Body         = (DLLConfig.DebugMode ? "Original:" + sToMail : "") + Environment.NewLine + Environment.NewLine + sMessage;
            Msg.IsBodyHtml   = bIsHtml;
            Msg.BodyEncoding = Encoding.UTF8;
            SmtpClient client = new SmtpClient(dllconfig.SMTPServer);
            try
            {
                client.Credentials = new System.Net.NetworkCredential(dllconfig.MailSender, dllconfig.MailPassword);
                client.Send(Msg);
                bResult = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace);
                CommonFunction cf = new CommonFunction();
                cf.SetLogFile(@"C:\inetpub\wwwroot\SCMWeb.log", ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace);
                throw new Exception(ex.Message);
            }
            return(bResult);
        }
Esempio n. 2
0
        /// <summary>
        /// 將系統的錯誤訊息發送給管理員
        /// </summary>
        /// <param name="GetError"></param>
        public static void SendMailToAdmin(System.Exception GetError)
        {
            try
            {
                //發送最底層的的問題訊息
                Exception InnerError = GetError.InnerException;
                while (InnerError != null)
                {
                    GetError   = InnerError;
                    InnerError = GetError.InnerException;
                }

                //設定郵件資料
                MailMessage msg = new MailMessage();
                msg.BodyEncoding = Encoding.Unicode;
                msg.IsBodyHtml   = true;
                //msg.SubjectEncoding = Encoding.Unicode;

                DLLConfig dllconfig = new DLLConfig();
                //寄件者
                msg.From = new MailAddress(dllconfig.MailSender);
                //收件者
                string   strSystemMaintainerMailList = dllconfig.AdminMailList;
                string[] strPartMail = strSystemMaintainerMailList.Split(';');
                for (int iIndex = 0; iIndex < strPartMail.Length; iIndex++)
                {
                    msg.To.Add(strPartMail[iIndex]);
                }

                //標題
                msg.Subject = (DLLConfig.DebugMode ? "[SCM測試]" : "[SCM]") + dllconfig.MailSubject;

                //郵件內文
                StringBuilder sbBody = new StringBuilder();
                sbBody.Append("Message:");
                sbBody.Append(GetError.Message).Append("<br />");
                if (string.IsNullOrEmpty(GetError.Source) == false)
                {
                    sbBody.Append("Source:");
                    sbBody.Append(GetError.Source).Append("<br />");
                }
                if (string.IsNullOrEmpty(GetError.StackTrace) == false)
                {
                    sbBody.Append("StackTrace:");
                    sbBody.Append(GetError.StackTrace.Replace("\r\n", "<br />")).Append("<br />");
                }
                sbBody.Append("User:"******"<br />");
                }
                else
                {
                    sbBody.Append(Environment.UserName).Append("<br />");
                }
                sbBody.Append("Host:");
                sbBody.Append(System.Net.Dns.GetHostName()).Append("<br />");    // HttpRequest.UserHostAddress
                msg.Body = sbBody.ToString();

                //發送郵件
                SmtpClient smtp = new SmtpClient(dllconfig.SMTPServer);
                smtp.Send(msg);
            }
            catch
            {
                //Do Nothing
            }
        }