Esempio n. 1
0
        /// <summary>
        /// Validates SMTP credentials
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <param name="server"></param>
        /// <param name="port"></param>
        /// <param name="enableSsl"></param>
        /// <returns></returns>
        public static bool ValidateCredentials(string login, string password, string server, int port, bool enableSsl)
        {
            SmtpConnectorBase connector;

            if (enableSsl)
            {
                connector = new SmtpConnectorWithSsl(server, port);
            }
            else
            {
                connector = new SmtpConnectorWithoutSsl(server, port);
            }

            if (!connector.CheckResponse(220))
            {
                return(false);
            }

            connector.SendData($"HELO {Dns.GetHostName()}{SmtpConnectorBase.EOF}");
            if (!connector.CheckResponse(250))
            {
                return(false);
            }

            connector.SendData($"AUTH LOGIN{SmtpConnectorBase.EOF}");
            if (!connector.CheckResponse(334))
            {
                return(false);
            }

            connector.SendData(Convert.ToBase64String(Encoding.UTF8.GetBytes($"{login}")) + SmtpConnectorBase.EOF);
            if (!connector.CheckResponse(334))
            {
                return(false);
            }

            connector.SendData(Convert.ToBase64String(Encoding.UTF8.GetBytes($"{password}")) + SmtpConnectorBase.EOF);
            if (!connector.CheckResponse(235))
            {
                return(false);
            }

            return(true);
        }
		/// <summary>Tests the settings.</summary>
		public static Task Test(this IContainMailConfiguration config)
		{
			var t = new Task(() =>
			{
				SmtpConnectorBase connector;
				if (config.MailServerEnableSsl)
					connector = new SmtpConnectorWithSsl(config.MailServer, config.MailServerPort);
				else
					connector = new SmtpConnectorWithoutSsl(config.MailServer, config.MailServerPort);
				try
				{
					var response = connector.CheckResponse();
					if (response != 220)
						throw GetException(response);

					connector.SendData($"HELO {Dns.GetHostName()}{SmtpConnectorBase.EOF}");
					response = connector.CheckResponse();
					if (response != 250)
						throw GetException(response);

					connector.SendData($"AUTH LOGIN{SmtpConnectorBase.EOF}");
					response = connector.CheckResponse();
					if (response != 334)
						throw GetException(response);

					connector.SendData(Convert.ToBase64String(Encoding.UTF8.GetBytes($"{config.MailServerUsername}")) + SmtpConnectorBase.EOF);
					response = connector.CheckResponse();
					if (response != 334)
						throw GetException(response);

					connector.SendData(Convert.ToBase64String(Encoding.UTF8.GetBytes($"{config.MailServerPassword}")) + SmtpConnectorBase.EOF);
					response = connector.CheckResponse();
					if (response != 235)
						throw GetException(response);
				}
				finally
				{
					((IDisposable) connector).Dispose();
				}
			}, TaskCreationOptions.LongRunning);
			t.Start(TaskScheduler.Default);
			return t;
		}