Example #1
0
        private static string GetMessage(AbstractFunction senderFunction, AbstractMailrelayReply reply, string targetUrl)
        {
            string parameters = senderFunction.ToGet();
            string error      = reply.error;

            return($"{senderFunction.GetType().Name}{Environment.NewLine}	Url:{targetUrl}{Environment.NewLine}	Parameters:{parameters}{Environment.NewLine}	Error:{error}");
        }
Example #2
0
        public AbstractMailrelayReply Send(AbstractFunction functionToSend)
        {
            functionToSend.apiKey = mailrelayApiKey;
            string target = mailrelayUrl + functionToSend.ToGet();

            Dictionary <string, string> values = functionToSend.GetValues();

            string reply;

            using (HttpClient client = new HttpClient())
            {
                FormUrlEncodedContent content = new FormUrlEncodedContent(values);

                if (DateTime.Now < lastSend + sendInterval)
                {
                    Thread.Sleep(sendInterval);
                }
                lastSend = DateTime.Now;

                try
                {
                    Task <HttpResponseMessage> response = client.PostAsync(mailrelayUrl, content);
                    Task <string> responseString        = response.Result.Content.ReadAsStringAsync();
                    reply = responseString.Result;
                }
                catch (Exception exception)
                {
                    while (exception.InnerException != null)
                    {
                        exception = exception.InnerException;
                    }
                    throw new MailrelayConnectionException(functionToSend, mailrelayUrl, exception);
                }
            }

            AbstractMailrelayReply mailrelayReply = functionToSend.GetMailrelayReply(reply);

            if (mailrelayReply.status == 0)
            {
                throw new MailrelayConnectionException(functionToSend, mailrelayReply, mailrelayUrl);
            }

            return(mailrelayReply);
        }
Example #3
0
        public AbstractMailrelayReply SendAsGet(AbstractFunction functionToSend)
        {
            functionToSend.apiKey = mailrelayApiKey;
            string target = mailrelayUrl + functionToSend.ToGet();

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(target);

            httpWebRequest.ContentType = "text/xml; encoding='utf-8'";
            httpWebRequest.Method      = "GET";

            HttpWebResponse httpResponse;

            try
            {
                httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            }
            catch (Exception exception)
            {
                throw new MailrelayConnectionException(functionToSend, mailrelayUrl, exception);
            }

            string reply;

            using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                reply = streamReader.ReadToEnd();
            }

            AbstractMailrelayReply mailrelayReply = functionToSend.GetMailrelayReply(reply);

            if (mailrelayReply.status == 0)
            {
                throw new MailrelayConnectionException(functionToSend, mailrelayReply, mailrelayUrl);
            }

            return(mailrelayReply);
        }
Example #4
0
 public MailrelayConnectionException(AbstractFunction senderFunction, AbstractMailrelayReply reply, string targetUrl)
     : base(GetMessage(senderFunction, reply, targetUrl))
 {
 }
Example #5
0
        private static string GetMessage(AbstractFunction senderFunction, string targetUrl)
        {
            string parameters = senderFunction.ToGet();

            return($"{senderFunction.GetType().Name}{Environment.NewLine}	Url:{targetUrl}{Environment.NewLine}	Parameters:{parameters}{Environment.NewLine}");
        }
Example #6
0
 public MailrelayConnectionException(AbstractFunction senderFunction, string targetUrl, Exception inner)
     : base($"Error:	{inner.Message}" + GetMessage(senderFunction, targetUrl), inner)
 {
 }