using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; using System.Net.Mail; namespace WMFormHandler { /// /// This is a simple mailform handler /// created to be used with "Instituut HIP" (Studio IMMO) /// 21 march 2006 /// /// I updated it to function as a .NET 2.0 application. This is one of the oldest subsystems in WMIT history /// it still works every now and then for usage with Medcon Europe and Instituut HIP. I added some extra toAddress checks for /// medcon form usage. 17th of May 2008. /// public partial class mailhandler : System.Web.UI.Page{ protected string messageBody = ""; protected string fromAddress = ""; protected string toAddress = ""; protected string bccAddress = ""; protected string subject = ""; protected string body = ""; protected bool proceed = true; protected void Page_Load(object sender, System.EventArgs e){ #region Check if any of the form values where set if(Request.Form.AllKeys.Length == 0){ Response.Write("Please use \"Post\" as method to pass form values to this handler"); return; } #endregion #region Check if handler is not abused by other ISP's / Hackers or SPAM robots if(!(Request.UrlReferrer.Host.ToLower().IndexOf("medconeurope.com")>=0) && !(Request.UrlReferrer.Host.ToLower().IndexOf("pacecvhealth.org") >= 0) && !(Request.UrlReferrer.Host.ToLower().IndexOf("medconeurope.eu") >= 0) && !(Request.UrlReferrer.Host.ToLower().IndexOf("medconinternational.com") >= 0) && !(Request.UrlReferrer.Host.ToLower().IndexOf("cakeworks.nl") >= 0) && !(Request.UrlReferrer.Host.ToLower().IndexOf("bizju.nl") >= 0) && !(Request.UrlReferrer.Host.ToLower().IndexOf("micothon.com") >= 0)) { if(Request.UrlReferrer.Host != Request.Url.Host){ Response.Write("I'm sorry, I am not allowed to process your request. Please contact WMIT: support [at] wmit [dot] nl"); return; } } #endregion #region Now try to fetch the manditory values: (fromAddress and toAddress) if(Request.Form["fromAddress"]!=null){ fromAddress = Request.Form["fromAddress"]; }else{ proceed = false; } if(Request.Form["toAddress"] != null){ toAddress = Request.Form["toAddress"]; // Little check on the to address to prevent possible attacks if (!toAddress.ToLower().EndsWith("bizju.nl") && !toAddress.ToLower().EndsWith("medconinternational.com") && !toAddress.ToLower().EndsWith("medconeurope.com") && !toAddress.ToLower().EndsWith("wmit.nl") && !toAddress.ToLower().EndsWith("cakeworks.nl") && !toAddress.ToLower().EndsWith("micothon.com")) { // illegal address Response.Write("Sorry, I am not allowed to send messages to " + toAddress + ". Please contact WMIT: helpdesk [at] wmit [dot] nl"); proceed = false; } }else{ proceed = false; } #endregion #region Now fetch additional form values if(Request.Form["bccAddress"]!=null){ bccAddress = Request.Form["bccAddress"]; } if(Request.Form["subject"]!=null){ subject = Request.Form["subject"]; } #endregion #region Cancel invalid requests here if(!proceed){ if(toAddress.Equals("")){ Response.Write("
Please use the \"toAddress\" hidden field to fill out the destination for the generated e-mail message"); } if(fromAddress.Equals("")){ Response.Write("
Please use the \"fromAddress\" hidden field to fill out the sender of the generated e-mail message"); } return; } #endregion addToMessage("Bericht ontvangen via "+Request.UrlReferrer.Host+ " op "+DateTime.Now.ToString()+"."); addToMessage(" "); // Now process all user defined fields foreach(string key in Request.Form.AllKeys){ if(!(key.Equals("toAddress") || key.Equals("fromAddress") || key.Equals("subject") || key.Equals("bccAddress") || key.Equals("okUrl") || key.Equals("errorUrl") || key.Equals("autoreply"))){ string formValue = Request.Form[key]; if(formValue.Length > 0){ addToMessage(key+System.Environment.NewLine+Request.Form[key]+System.Environment.NewLine); } } } addToMessage("Informatie van computer van afzender: IP: "+Request.UserHostAddress+" / "+Request.UserAgent); if(subject.Equals("")){ subject = "Bericht via website: "+Request.UrlReferrer.Host; } // Response.Write(messageBody); if(sendMail(toAddress,toAddress,fromAddress,fromAddress,bccAddress,subject,messageBody)){ sendAutoReply(); if(Request.Form["okUrl"]!=null){ Response.Redirect(Request.Form["okUrl"]); }else{ Response.Write("Your message was sent succesfully."); } }else{ if(Request.Form["errorUrl"]!=null){ Response.Redirect(Request.Form["errorUrl"]); }else{ Response.Write("Error while sending message."); } } } protected void sendAutoReply(){ if(Request.Form["autoreply"]!=null){ if(Request.Form["E-mail"]!=null){ // Do some basic check to prevent very stupid errors: string replyAddress = Request.Form["E-mail"]; if(replyAddress.IndexOf("@")==-1 || replyAddress.IndexOf(".")==-1 || replyAddress.Length<=5){ // Certainly an invalid e-mail address. return; } string path = Server.MapPath("./"+Request.Form["autoreply"]+".txt"); if(File.Exists(path)){ string body = ""; try{ body = System.IO.File.ReadAllText(path); string fromAddress = Request.Form["fromAddress"]; string subject = Request.Form["subject"]; sendMail(replyAddress,replyAddress,fromAddress,fromAddress,"",subject,body); }catch(Exception error){ System.Diagnostics.Trace.WriteLine("Sorry, auto reply caused exception. "+error.Message); return; } } } } } protected void addToMessage(string line){ messageBody += line + System.Environment.NewLine; } /// /// Generates an e-mail object and makes a generous attempt to send it over the net /// /// /// /// /// /// public bool sendMail(string toAddress, string toText, string fromAddress, string fromText, string bcc, string subject, string body) { try { MailMessage mail = new MailMessage(); mail.BodyEncoding = System.Text.Encoding.UTF8; mail.To.Add(new MailAddress(toAddress, toText)); if (bcc != null) { if (bcc.Length > 0) { mail.Bcc.Add(new MailAddress(bcc)); } } mail.Subject = subject; mail.Body = body; mail.IsBodyHtml = false; mail.From = new MailAddress(fromAddress, fromText); SmtpClient client = new SmtpClient("localhost"); client.Send(mail); return true; } catch (Exception error) { Response.Write("
");
                Response.Write("Error: " + error.ToString());
                Response.Write("

"); return false; } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { } #endregion } }