Exemplo n.º 1
0
        public static SmtpClient GetMailServer(MailServerTypes MailServerType)
        {
            string strMailServer = String.Empty;
            int    intPort       = 0;
            string strUserName   = String.Empty;
            string strPassword   = String.Empty;

            if (MailServerType == MailServerTypes.Bulk && !StringFunctions.IsNullOrWhiteSpace(MailServerBulkEmail))
            {
                //Use separate mail server for bulk email
                strMailServer = MailServerBulkEmail;
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerBulkEmail_Port))
                {
                    intPort = int.Parse(MailServerBulkEmail_Port);
                }
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerBulkEmail_UserName))
                {
                    strUserName = MailServerBulkEmail_UserName;
                }
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerBulkEmail_Password))
                {
                    strPassword = MailServerBulkEmail_Password;
                }
            }
            else if (MailServerType == MailServerTypes.Debug && !StringFunctions.IsNullOrWhiteSpace(MailServerDebug))
            {
                //Use separate mail server for debugging emails and error reporting
                strMailServer = MailServerDebug;
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerDebug_Port))
                {
                    intPort = int.Parse(MailServerDebug_Port);
                }
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerDebug_UserName))
                {
                    strUserName = MailServerDebug_UserName;
                }
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerDebug_Password))
                {
                    strPassword = MailServerDebug_Password;
                }
            }
            else
            {
                if (Environment.Current.AmILive())
                {
                    strMailServer = MailServerLive;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_Port))
                    {
                        intPort = int.Parse(MailServerLive_Port);
                    }
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_UserName))
                    {
                        strUserName = MailServerLive_UserName;
                    }
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_Password))
                    {
                        strPassword = MailServerLive_Password;
                    }
                }
                else if (Environment.Current.AmIStage())
                {
                    strMailServer = MailServerStage;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerStage_Port))
                    {
                        intPort = int.Parse(MailServerStage_Port);
                    }
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerStage_UserName))
                    {
                        strUserName = MailServerStage_UserName;
                    }
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerStage_Password))
                    {
                        strPassword = MailServerStage_Password;
                    }
                }
                else if (Environment.Current.AmIQA())
                {
                    strMailServer = MailServerQA;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerQA_Port))
                    {
                        intPort = int.Parse(MailServerQA_Port);
                    }
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerQA_UserName))
                    {
                        strUserName = MailServerQA_UserName;
                    }
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerQA_Password))
                    {
                        strPassword = MailServerQA_Password;
                    }
                }
                else
                {
                    strMailServer = MailServerDev;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerDev_Port))
                    {
                        intPort = int.Parse(MailServerDev_Port);
                    }
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerDev_UserName))
                    {
                        strUserName = MailServerDev_UserName;
                    }
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerDev_Password))
                    {
                        strPassword = MailServerDev_Password;
                    }
                }
            }

            if (StringFunctions.IsNullOrWhiteSpace(strMailServer))
            {
                strMailServer = MailServerLive;
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_Port))
                {
                    intPort = int.Parse(MailServerLive_Port);
                }
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_UserName))
                {
                    strUserName = MailServerLive_UserName;
                }
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_Password))
                {
                    strPassword = MailServerLive_Password;
                }
            }

            if (StringFunctions.IsNullOrWhiteSpace(strMailServer))
            {
                throw new Exception("Null Mail Server");
            }

            System.Net.Mail.SmtpClient objMailServer = new System.Net.Mail.SmtpClient();
            objMailServer.Host = strMailServer;
            if (intPort > 0)
            {
                objMailServer.Port = intPort;
            }
            //if (objMailServer.Port == 587) //This appears to be a bad assumption to make
            //    objMailServer.EnableSsl = true;

            if (!StringFunctions.IsNullOrWhiteSpace(strUserName))
            {
                NetworkCredential objCredentials = new NetworkCredential(strUserName, strPassword);
                objMailServer.UseDefaultCredentials = false;
                objMailServer.Credentials           = objCredentials;
            }

            return(objMailServer);
        }
Exemplo n.º 2
0
        public static SmtpClient GetMailServer(MailServerTypes MailServerType)
        {
            string strMailServer = String.Empty;
            int intPort = 0;
            string strUserName = String.Empty;
            string strPassword = String.Empty;

            if (MailServerType == MailServerTypes.Bulk && !StringFunctions.IsNullOrWhiteSpace(MailServerBulkEmail))
            {
                //Use separate mail server for bulk email
                strMailServer = MailServerBulkEmail;
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerBulkEmail_Port))
                    intPort = int.Parse(MailServerBulkEmail_Port);
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerBulkEmail_UserName))
                    strUserName = MailServerBulkEmail_UserName;
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerBulkEmail_Password))
                    strPassword = MailServerBulkEmail_Password;
            }
            else if (MailServerType == MailServerTypes.Debug && !StringFunctions.IsNullOrWhiteSpace(MailServerDebug))
            {
                //Use separate mail server for debugging emails and error reporting
                strMailServer = MailServerDebug;
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerDebug_Port))
                    intPort = int.Parse(MailServerDebug_Port);
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerDebug_UserName))
                    strUserName = MailServerDebug_UserName;
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerDebug_Password))
                    strPassword = MailServerDebug_Password;
            }
            else
            {
                if (Environment.Current.AmILive())
                {
                    strMailServer = MailServerLive;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_Port))
                        intPort = int.Parse(MailServerLive_Port);
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_UserName))
                        strUserName = MailServerLive_UserName;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_Password))
                        strPassword = MailServerLive_Password;
                }
                else if (Environment.Current.AmIStage())
                {
                    strMailServer = MailServerStage;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerStage_Port))
                        intPort = int.Parse(MailServerStage_Port);
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerStage_UserName))
                        strUserName = MailServerStage_UserName;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerStage_Password))
                        strPassword = MailServerStage_Password;
                }
                else if (Environment.Current.AmIQA())
                {
                    strMailServer = MailServerQA;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerQA_Port))
                        intPort = int.Parse(MailServerQA_Port);
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerQA_UserName))
                        strUserName = MailServerQA_UserName;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerQA_Password))
                        strPassword = MailServerQA_Password;
                }
                else
                {
                    strMailServer = MailServerDev;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerDev_Port))
                        intPort = int.Parse(MailServerDev_Port);
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerDev_UserName))
                        strUserName = MailServerDev_UserName;
                    if (!StringFunctions.IsNullOrWhiteSpace(MailServerDev_Password))
                        strPassword = MailServerDev_Password;
                }
            }

            if (StringFunctions.IsNullOrWhiteSpace(strMailServer))
            {
                strMailServer = MailServerLive;
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_Port))
                    intPort = int.Parse(MailServerLive_Port);
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_UserName))
                    strUserName = MailServerLive_UserName;
                if (!StringFunctions.IsNullOrWhiteSpace(MailServerLive_Password))
                    strPassword = MailServerLive_Password;
            }

            if (StringFunctions.IsNullOrWhiteSpace(strMailServer))
                throw new Exception("Null Mail Server");

            System.Net.Mail.SmtpClient objMailServer = new System.Net.Mail.SmtpClient();
            objMailServer.Host = strMailServer;
            if (intPort > 0)
                objMailServer.Port = intPort;
            //if (objMailServer.Port == 587) //This appears to be a bad assumption to make
            //    objMailServer.EnableSsl = true;

            if (!StringFunctions.IsNullOrWhiteSpace(strUserName))
            {
                NetworkCredential objCredentials = new NetworkCredential(strUserName, strPassword);
                objMailServer.UseDefaultCredentials = false;
                objMailServer.Credentials = objCredentials;
            }

            return objMailServer;
        }