Пример #1
0
            private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
            {
                // Get the unique identifier for this asynchronous operation.
                string token = (string)e.UserState;

                //string token = e.UserState.ToString();


                if (e.Cancelled)
                {
                    // Console.WriteLine( "[{0}] Send canceled.", token );
                }
                if (e.Error != null)
                {
                    // Console.WriteLine( "[{0}] {1}", token, e.Error.ToString() );
                    Global.LogError(HttpContext.Current, EnumLogCategories.EMAIL, "Error sending email: " + token + " " + e.Error);
                }
                else
                {
                    // Console.WriteLine( "Message sent." );
                    Global.LogDebug(HttpContext.Current, EnumLogCategories.EMAIL, "Email Send " + token);
                }
                // mailSent = true;
            }
Пример #2
0
            // http://stackoverflow.com/questions/7276375/what-are-best-practices-for-using-smtpclient-sendasync-and-dispose-under-net-4/7276819#7276819

            /// <summary>
            /// Call using:
            /// using System.Threading.Tasks;
            /// var t = Task.Run( () => Global.Utils.Emails.SendEmailAsync( "*****@*****.**", asunto, "enviado desde helpdesk", false ) );
            /// t.Wait();
            /// </summary>
            /// <param name="toEmailAddress"></param>
            /// <param name="emailSubject"></param>
            /// <param name="emailMessage"></param>
            /// <param name="isBodyHtml"></param>
            /// <returns></returns>
            public static async Task SendEmailAsync(string[] to, string from, string[] CC, string[] BCC, string emailSubject, string emailMessage, bool isBodyHtml)
            {
                var message = new MailMessage();

                //message.To.Add( toEmailAddress );

                // from?
                message.From = new MailAddress(from);

                // to?
                if (to != null)
                {
                    foreach (string s in to)
                    {
                        if (s != null)
                        {
                            message.To.Add(s);
                        }
                    }
                }

                // CC?
                if (CC != null)
                {
                    foreach (string s in CC)
                    {
                        if (s != null)
                        {
                            message.To.Add(s);
                        }
                    }
                }

                // BCC?
                if (BCC != null)
                {
                    foreach (string s in BCC)
                    {
                        if (s != null)
                        {
                            message.To.Add(s);
                        }
                    }
                }


                message.Subject    = emailSubject;
                message.Body       = emailMessage;
                message.IsBodyHtml = isBodyHtml;

                message.From = new MailAddress(Configuration.Mail.GetMailServerLogin());


                //Proper Authentication Details need to be passed when sending email from gmail
                NetworkCredential mailAuthentication = new NetworkCredential(Configuration.Mail.GetMailServerLogin(), Configuration.Mail.GetMailServerPassword());

                using (var smtpClient = new SmtpClient())
                {
                    // server
                    smtpClient.Host                  = Configuration.Mail.GetMailServer();
                    smtpClient.Port                  = Configuration.Mail.GetMailServerPort();
                    smtpClient.EnableSsl             = Configuration.Mail.GetMailServerIsEnableSSL();
                    smtpClient.UseDefaultCredentials = false;
                    smtpClient.Credentials           = mailAuthentication;


                    if (Global.Configuration.Development.GetIsEnabledDeveloperMode())
                    {
                        smtpClient.Timeout = 5000;
                    }
                    else
                    {
                        smtpClient.Timeout = 180000;  //An Int32 that specifies the time-out value in milliseconds. The default value is 100,000 (100 seconds).
                    }
                    // Set the method that is called back when the send operation ends.
                    // smtpClient.SendCompleted += new SendCompletedEventHandler( SendCompletedCallback );

                    // The userState can be any object that allows your callback
                    // method to identify this send operation.
                    // For this example, the userToken is a string constant.
                    string userState = emailSubject.Replace(" ", "") + "_" + DateTime.Now.Ticks.ToString();


                    // send
                    try
                    {
                        await smtpClient.SendMailAsync(message);

                        // smtpClient.Send( message ); // only works with this...
                        // smtpClient.SendAsync( message, userState );
                    }
                    catch (Exception e)
                    {
                        Global.LogError(HttpContext.Current, EnumLogCategories.EMAIL, e.Message + Environment.NewLine + e.InnerException);
                    }
                }
            }
Пример #3
0
        private void Application_Error( object sender, EventArgs e )
        {
            //https://msdn.microsoft.com/en-us/library/24395wz3.aspx

            HttpServerUtility server = HttpContext.Current.Server;
            Exception exception = server.GetLastError();
            string currentPageRequest = HttpContext.Current.Request.FilePath;


            if (Configuration.Development.GetIsEnabledDebugDeveloperModeShowGlobalPageError())
            {

                if (exception.GetType() == typeof( HttpException ))
                {
                    Server.Transfer( "~/ErrorPageHttp.aspx" );
                }
                else
                {
                    Response.Write( "<h2>Global Page Error</h2>\n" );
                    Response.Write(
                        "<p>" + exception.Message + "</p>\n" );
                    Response.Write( "Return to the <a href='/Default.aspx'>" +
                        "Default Page</a>\n" );

                    if (Context.Request.IsLocal)
                    {
                        Response.Write( "<p>" + exception.Source + "</p>\n" );
                        Response.Write( "<p>" + exception.InnerException + "</p>\n" );
                        Response.Write( "<p>" + exception.StackTrace + "</p>\n" );
                    }
                }

                if (! _previousPageError.Equals( currentPageRequest ))
                {

                    ExceptionUtility.LogException( exception, currentPageRequest );
                    ExceptionUtility.NotifySystemOps( exception );
                }
            }

            // if not in DEV MODE, write error to .txt
            if (!Configuration.Development.GetIsEnabledDeveloperMode())
            {


                // devexpress callback error
                // Use HttpContext.Current to get a Web request processing helper

                // is http?
                if (exception is HttpUnhandledException)
                {



                    HttpException ex = (HttpException)Server.GetLastError();
                    //Exception innerexception = exception.InnerException;

                    // Log an exception
                    // TODO, send email to admin.
                    if (!_previousPageError.Equals( currentPageRequest ))
                    {

                        ExceptionUtility.LogException( exception, currentPageRequest );
                        ExceptionUtility.NotifySystemOps( exception );


                        // log to logger
                        if (HttpContext.Current != null && HttpContext.Current.Request != null)
                            Global.LogError( Context, Global.EnumLogCategories.GENERAL,
                            "HTTP " + ex.GetHttpCode() + ": " + Request.RawUrl.ToString(),
                            ex );
                    }

                    // options to show info to user:
                    // 1. show a blank page:
                    //Server.ClearError();
                    // 2. redirect to a page
                    // Response.Redirect("~/Errors/ErrorPageHttp.aspx");
                    // 3. do nothing, go to customErrors configuration in web.config and/or show asp.net error
                }
                else
                {
                    // other NO http errors
                    ExceptionUtility.LogException( exception, currentPageRequest );
                    ExceptionUtility.NotifySystemOps( exception );

                    // log to logger
                    if (HttpContext.Current != null && HttpContext.Current.Request != null)
                        Global.LogError( Context, Global.EnumLogCategories.GENERAL,
                        "Error " + exception.HResult + ": " + Request.RawUrl.ToString(),
                        exception );

                }

            }
            else
            {
                ExceptionUtility.LogException( exception, currentPageRequest );
                ExceptionUtility.NotifySystemOps( exception );

                // log to logger, no works in callback mode
                /*
                if (HttpContext.Current != null && HttpContext.Current.Request != null)
                    Global.LogError( Context, Global.EnumLogCategories.GENERAL,
                        "Error " + exception.HResult + ": " + Request.RawUrl.ToString(),
                        exception );
                        */

                ExceptionUtility.LogException( exception, currentPageRequest );
                ExceptionUtility.NotifySystemOps( exception );



            }

            

            //_previousException = exception;
            _previousPageError = currentPageRequest;


        }