2

Sometimes I need to run something on my website. For instance, right now I want to send a mail out from the support account to someone.

To do this, I plan to create a temp webpage, then put some code in the page_load event, then delete the webpage:

protected void Page_Load(object sender, EventArgs e)
{
   String notificationMessage = "Email body.";
   Mailer.SendMail("[email protected]", "Email header", notificationMessage);
}

This is a bit silly. However, it is fairly convinient for accessing stuff I've set in web.config, like sql servers and mailing modules.

Is there a better way to do this?

3
  • The only other way I could see doing this is storing values inside of a database and loading these dynamically. Commented Nov 1, 2011 at 15:57
  • 2
    maybe something with a GenericHandler ? At least you don't carry the overhead of the Page. Commented Nov 1, 2011 at 16:04
  • I agree with @Bazzz, a generic HttpHandler would be a good approach. Commented Nov 1, 2011 at 16:32

2 Answers 2

1

As @Bazzz and @neontapir have stated, I would use a Generic Handler file for this with the code:

public class Handler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
             String notificationMessage = "Email body.";
             Mailer.SendMail("[email protected]", "Email header", notificationMessage);
    }
}

This way your not rendering any HTML, which will make the process quicker, and you will only have the one .ashx file compared to a .aspx and a .aspx.cs

In the long run if you find yourself making one offs like this, you should consider building it into the application.

Sign up to request clarification or add additional context in comments.

Comments

1

If you use System.Net.Mail.Mailmessage class (more at http://www.systemnetmail.com/faq/2.1.aspx), you can set the IsBodyHtml to true, and provide the HTML directly to the message, without the need of worrying about creating/deleting a web page... You can't post an ASP.NET page via email and expect to interact with it.

What exactly are you trying to do?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.