Reputation: 11617
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?
Upvotes: 2
Views: 111
Reputation: 103388
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.
Upvotes: 1
Reputation: 50728
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?
Upvotes: 1