示例#1
0
        public static List <SmtpAttempt> SendTestEmails(HttpRequestMessage request, string to = "*****@*****.**")
        {
            var host = request.RequestUri.Host;

            host = "mail." + host;

            var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

            var credentials = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);

            var ports = new[] { 25, 587 };

            var ssl = new[] { true };

            var capacity = ports.Length * ssl.Length;

            var returnVar      = new List <SmtpAttempt>(capacity);
            var taskList       = new List <Task>(capacity);
            var smtpClientList = new List <SmtpClient>(capacity);

            foreach (var p in ports)
            {
                foreach (var s in ssl)
                {
                    var a = new SmtpAttempt
                    {
                        Host = host,
                        Port = p,
                        SSL  = s
                    };

                    SmtpClient mailer = new SmtpClient(host, p);

                    mailer.UseDefaultCredentials = false;
                    mailer.Credentials           = credentials;
                    mailer.EnableSsl             = s;
                    try
                    {
                        returnVar.Add(a);
                        mailer.Send(smtpSection.From, to, "test email", "testing server settings\r\n" + a.ToString());
                        //var sm = mailer.SendMailAsync(smtpSection.From, to, "test email", "testing server settings\r\n" + a.ToString());
                        //taskList.Add(sm);
                        smtpClientList.Add(mailer);
                    }
                    catch (Exception e)
                    {
                        a.ExceptionMsg = e.Message;
                        LogErrorManually(e);
                        mailer.Dispose();
                    }
                }
            }
            try
            {
                // wait for all of them to complete
                //await Task.WhenAll(taskList);
            }
            catch (Exception)
            {
                for (int i = 0; i < taskList.Count; i++)
                {
                    if (taskList[i].Exception != null)
                    {
                        var sa = returnVar[i];
                        sa.ExceptionMsg = taskList[i].Exception.Message;
                        LogErrorManually(taskList[i].Exception);
                    }
                }
            }
            smtpClientList.ForEach(s => s.Dispose());
            return(returnVar);
        }
示例#2
0
        public static List<SmtpAttempt> SendTestEmails(HttpRequestMessage request, string to = "*****@*****.**")
        {
            var host = request.RequestUri.Host;

            host = "mail." + host;

            var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

            var credentials = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);

            var ports = new[] { 25, 587 };

            var ssl = new[] { true };

            var capacity =  ports.Length * ssl.Length;

            var returnVar = new List<SmtpAttempt>(capacity);
            var taskList = new List<Task>(capacity);
            var smtpClientList = new List<SmtpClient>(capacity);

            foreach (var p in ports)
            {
                foreach (var s in ssl)
                {
                    var a = new SmtpAttempt
                    {
                        Host = host,
                        Port = p,
                        SSL = s
                    };

                    SmtpClient mailer = new SmtpClient(host, p);

                    mailer.UseDefaultCredentials = false;
                    mailer.Credentials = credentials;
                    mailer.EnableSsl = s;
                    try
                    {
                        returnVar.Add(a);
                        mailer.Send(smtpSection.From, to, "test email", "testing server settings\r\n" + a.ToString());
                        //var sm = mailer.SendMailAsync(smtpSection.From, to, "test email", "testing server settings\r\n" + a.ToString());
                        //taskList.Add(sm);
                        smtpClientList.Add(mailer);
                    }
                    catch(Exception e)
                    {
                        a.ExceptionMsg = e.Message;
                        LogErrorManually(e);
                        mailer.Dispose();
                    }
                }
            }
            try
            {
                // wait for all of them to complete
                //await Task.WhenAll(taskList); 
            }
            catch (Exception)
            {
                for (int i = 0; i< taskList.Count;i++)
                {
                    if (taskList[i].Exception != null)
                    {
                        var sa = returnVar[i];
                        sa.ExceptionMsg = taskList[i].Exception.Message;
                        LogErrorManually(taskList[i].Exception);
                    }
                }
            }
            smtpClientList.ForEach(s => s.Dispose());
            return returnVar;
        }