locked
how to send automatic email on daily and weekly bases? RRS feed

  • Question

  • User-1680349116 posted

    Hi,

    I have a web site in asp.net(C#). And want to add an automatic email sending functionality in it, i.e. an email will be send daily and weekly(both on a particular time) to all the stock holders about the orders details placed by registered users.

    How can I do this in asp.net(C#)? any idea?

    Regards,

    dazy

    Saturday, February 13, 2010 1:41 PM

Answers

  • User1983487377 posted

    hi..

    for tht you can create a window service and install it at your server

    and in your window service ,, you can logically schedule the mails to send

    to create window service refer the Doc..http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx


    All the best


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 13, 2010 3:31 PM
  • User-952121411 posted

    Actually the original poster 'dazy' has not chimed in since the 1st post as to if this site is in a shared environment or not and on his own servers.  The inclination that this site was being hosted was brought up by someone else, and took us on another route.  If dazy could respond with some more information as to the infrastructure, it might help narrow down the suggestions.

    I do think that if it is possible, that using a Windows Service is optimal.  This can be done without a ton of work as shown below:

    Running a Periodic Process in .NET using a Windows Service:

    http://allen-conway-dotnet.blogspot.com/2009/12/running-periodic-process-in-net-using.html

    However if the site is hosted in a shared environment, then the solution in the last few posts will be the way to go.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2010 10:58 AM
  • User-389939489 posted

    > Solution suggested my LV are all for the web apps hosted in a shared enviroment, so will they not be useful in my case?

    Yes, they would: it is the other solutions that you cannot apply to a shared hosting, not the other way round.

    Anyway, given that you are on a dedicated server, I would suggest the windows service: as I have said, there is minimal difference in the actual coding involved. Sql mailing is of course an option too, in place of the windows service, though I find it a bit clumsy unless your overall application architecture makes it sensible to allocate such functionality to the database.

    Best luck,

    -LV

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2010 7:12 AM

All replies

  • User1983487377 posted

    hi..

    for tht you can create a window service and install it at your server

    and in your window service ,, you can logically schedule the mails to send

    to create window service refer the Doc..http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx


    All the best


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 13, 2010 3:31 PM
  • User-1199946673 posted

    if you don't have access to the server (for example in a shared hosting enviroment), a windows service isn't possible. In that case, take a look at:

    http://www.codeproject.com/KB/aspnet/ASPNETService.aspx
    http://www.mikesdotnetting.com/Article/129/Simple-task-Scheduling-using-Global.asax

    Saturday, February 13, 2010 7:06 PM
  • User1983487377 posted

    yes this is true ... and nice option..

    but it will not fire automatically

    someone will have to login or start the session thn only... so after every period you have to start service but login

    thn it is same as you give some page to do same task...


    please rectify me if i am wrong..

    thanks

    Sunday, February 14, 2010 6:13 AM
  • User-389939489 posted

    You are quite correct: a working approach is that you instanciate a Timer object from the Application_OnStart; of course, even better if you wrap the Timer and the scheduling logic into a separate, possibly static, class. If you then need something user-specific, you can still hook the Session events.

    You would start the whole thing from the Application_OnStart, so that, as long as the web app is running, your "scheduler" is guaranteed to be running too. Finally, you might want to dig into the settings for the application domain, in order to make sure that the site, once started, keeps running even when nobody accesses it for a while...

    HTH,

    -LV

    Sunday, February 14, 2010 6:47 AM
  • User1983487377 posted

    ok

    but if we see the original question

    he want to send mails from server after some period...

    i think that is not possible from application.. we have to have another service

    and if the hosting is not supporting then... should change logic...

    please refer Sql server reporting service which helps to send mail....

    may that help ...


    All the best


    Sunday, February 14, 2010 8:37 AM
  • User-389939489 posted

    > he want to send mails from server after some period...
    > i think that is not possible from application.. we have to have another service

    It is possible, along the lines of what I have described: it actually works absolutely well. And, it is your only option under a shared hosting.

    -LV

    Sunday, February 14, 2010 10:04 AM
  • User-952121411 posted

    Actually the original poster 'dazy' has not chimed in since the 1st post as to if this site is in a shared environment or not and on his own servers.  The inclination that this site was being hosted was brought up by someone else, and took us on another route.  If dazy could respond with some more information as to the infrastructure, it might help narrow down the suggestions.

    I do think that if it is possible, that using a Windows Service is optimal.  This can be done without a ton of work as shown below:

    Running a Periodic Process in .NET using a Windows Service:

    http://allen-conway-dotnet.blogspot.com/2009/12/running-periodic-process-in-net-using.html

    However if the site is hosted in a shared environment, then the solution in the last few posts will be the way to go.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 15, 2010 10:58 AM
  • User-1680349116 posted

    Thank you so much for your quick replies.

    http://www.mikesdotnetting.com/Article/129/Simple-task-Scheduling-using-Global.asax

    This solution looks useful for my scenario but my website is not so popular.

    And this solution is for those sites which are popular enought becuase it needs enough visitors that Session_Start() will be fired often to serve the purpose of trigger point.

    What do you people say about this?

    Regards,

    dazy.


    <input id="gwProxy" type="hidden"><!--Session data--><input onclick="jsCall();" id="jsProxy" type="hidden">

    Monday, February 15, 2010 11:36 AM
  • User-389939489 posted

    http://www.mikesdotnetting.com/Article/129/Simple-task-Scheduling-using-Global.asax

    This solution looks useful for my scenario but my website is not so popular.

    And this solution is for those sites which are popular enought becuase it needs enough visitors that Session_Start() will be fired often to serve the purpose of trigger point.

    What do you people say about this?

     

    As you can appreciate, that is not reliable, which is why you need to start a Timer from the Application_OnStart...

    @atconway:
    > I do think that if it is possible, that using a Windows Service is optimal.
    > This can be done without a ton of work

    I agree that a windows service is generally a more "natural" approach. OTOH, let's not forget that a major selling point of Asp.Net is that we finally have a *real* application, an application that is under many respects a system service too: there is just no point in being shy of using those features. Indeed there is no "ton of work" to be worried about either, as the amount of work involved is the same in both cases: what you would (ideally) do is have a Scheduler component built as a class library, and then link it to either the web app and its life-cycle, or a windows service, and its life-cycle, depending on need...

    HTH,

    -LV

    Monday, February 15, 2010 12:52 PM
  • User-1199946673 posted

    I think the orther solution I posted is slightly better.

    http://www.codeproject.com/KB/aspnet/ASPNETService.aspx

    If your site is not  visited on a regular base, why not use some (free) website monitoring services to visit your site every 30 minutes or so?

    http://host-tracker.com/
    http://www.siteuptime.com/

    Monday, February 15, 2010 2:41 PM
  • User-389939489 posted

    > I think the orther solution I posted is slightly better.

    That solution is just as inadequate as the preceeding: you use cache expiration not to use a Timer?

    > If your site is not  visited on a regular base, why not use some (free) website monitoring services

    Why not do it properly and just start a Timer from the Application_OnStart?

    Yes, I understand there are at least a hundred articles out there just worth sending to the trash bin: better sooner than later!

    -LV

    Monday, February 15, 2010 3:14 PM
  • User-419902300 posted

    Why not use the database server?

     Its quite simple to setup SMTP via the database server (SQL fully developed / tested functionality for this), SQL already has scheduled tasks/ jobs and as you are using a database it can pull the data from the same database instance.

    It took me about an hour to setup a similar model for an email campaign tool and it works a charm, is quicker because you cut out the unecessary use of a service application, any you can manage / test the schedued tasks / jobs through the SQL management tool.

    Approach:

    - Write a SQL Job that is scheduled to run daily or weekly or whenever you want it to reoccur.

    - Get the SQL Job to run a stored procedure, that generates a table of data.

    - Loop through the data and for each product/row/whatever, simply send out a 'dbmail'

    - Done!

    Note: DBMail supports HTML so you can decorate the email however you want it to look like.

     

    I really would advise looking into it as you may save yourself alot of coding / time.

    How to configure DBMail: http://www.databasejournal.com/features/mssql/article.php/3626056/Database-Mail-in-SQL-Server-2005.htm

    Example of how simple it can be to send an email: http://www.databasejournal.com/features/mssql/article.php/3626056/Database-Mail-in-SQL-Server-2005.htm

     

    Monday, February 15, 2010 3:26 PM
  • User-389939489 posted

    > is quicker because you cut out the unecessary use of a service application

    And isn't the database itself a service application?

    > you can manage / test the schedued tasks / jobs through the SQL management tool

    Sure, though that is usually not an option under a shared hosting, is it?

    There are tons of ways to do the same thing: only few of them are both correct and sensible.

    -LV

    Monday, February 15, 2010 3:54 PM
  • User-952121411 posted

    My typical concern with trying to make an ASP.NET application run indefinitely as a 'background process' is that it is not something it was intended or best suited to do.  The biggest issue I see here is the ASP.NET worker process recycling which can and does happen (unintentional circumstances only referred to here; typical recycles are handled by overlapping one another and should go smoothly), and could cause an unwanted effect on the stability and continual processing overall.  Also, what happens if the app instance shuts down?  Does somebody manually have to start it back up? With a Windows Service, the only time the service will stop once started is if the server crashed, and in that case no app of any type will persist until the server is rebooted.  Sure you can make an ASP.NET application morph into a long running background service, but realistically it is not the tool to do the job.  Why use a toothbrush to clean the floors when you have a mop?

    Also everyone needs to note that Timers are a tricky deal when polling, and if the proper type of timer is not used, you can get yourself into a world of mess or even a frozen application.  I know this from polling on tiny intervals (< .5 seconds with multiple timers) and it is important to use the proper type.  I still would recommend using 'ThreadPool.RegisterWaitForSingleObject' over timers regardless of the containing application (ASP.NET, Windows Service, etc.) when polling.

    However, this is all in concept and not necessarily in implementation for this specific question.  In the case where your site is being hosted for you, I defer to the other examples as I do not run into this problem.  I will note, I believe there are some options for managed server hosting where services can be installed, and another option is possibly looking into some of the free Cloud Computing options as well.

     

     

     

     

    Monday, February 15, 2010 4:14 PM
  • User-419902300 posted

    > is quicker because you cut out the unecessary use of a service application

    And isn't the database itself a service application?

    > you can manage / test the schedued tasks / jobs through the SQL management tool

    Sure, though that is usually not an option under a shared hosting, is it?

    There are tons of ways to do the same thing: only few of them are both correct and sensible.

    -LV

     

     

    The unneccessary refers to creating a bespoke service application in the example of using the dmail approach. Also as you noted, the database server has its own service in this context and as such would render another bespoke service redundant, otherwise you then have two service applications running the operation rather than one as an example when taking the dbmail approach.

    With the shared hosting comment, good to point this out. Sharing a database server with other people does limit what you can do in SQL server / DB product X, I guess your limited / it depends on your setup and needs.

     

    Monday, February 15, 2010 4:30 PM
  • User-389939489 posted

    @atconway:

    > The biggest issue I see here is the ASP.NET worker process recycling which can and does happen (unintentional circumstances only referred to here; typical recycles are handled by overlapping one another and should go smoothly), and could cause an unwanted effect on the stability and continual processing overall

    There is no issue and no unwanted effects at all: your worries sound like you don't know what the Asp.Net app life-cycle is. As for services that get started from the application_onstart, you are guaranted that they get restarted when the app recycles.

    > Also, what happens if the app instance shuts down? Does somebody manually have to start it back up?

    The service shuts down is the answer. What is the concern? What if your windows service shuts down? Is that no more a concern? The web app recycles: does your windows service? Your objections betray little familiarity with the Asp.Net life-cycle.

    > Sure you can make an ASP.NET application morph into a long running background service, but realistically it is not the tool to do the job.  Why use a toothbrush to clean the floors when you have a mop?

    Realistically, there are cases when you just have no other option: and the fact that this is not the tool for the job is your personal understanding (though your are not alone, of course).

    > Also everyone needs to note that Timers are a tricky deal when polling, and if the proper type of timer is not used, you can get yourself into a world of mess or even a frozen application.  I know this from polling on tiny intervals (< .5 seconds with multiple timers

    You are still making a general rule of your personal struggles.

    > In the case where your site is being hosted for you, I defer to the other examples as I do not run into this problem.

    Lucky you.

    Reality rather is: few developers are familiar with *event-driven* programming, despite it is basic to web programming, both server and client-side: and this includes most article writers, which is a shame in itself.

    -LV

    Monday, February 15, 2010 5:19 PM
  • User-1680349116 posted

    Thank you so much for all your replies and I'm sorry for my too late reply.

    First of all I want to mention that my site is hosted on a dedicated server.

    Second I'm new to asp.net so I have not much backgound knowledge. Since I have no idea about the pros and cons of some or all of the solutions you people have suggested here.

    Thank you once again for posting different solutions depending upon different scenarios and figuring out different possibilites and issues which can be arise with these solutions.

    But as I can't compare these solutions merits and demerits so now I become desperatly confused with all of your solutions.

    Hope you understand what I'm trying to say.

    Now can anybody suggest what will be the best solution in my case-

    • Solution suggested my LV are all for the web apps hosted in a shared enviroment, so will they not be useful in my case?
    • Windows Service
    • DB Server/SQL Schedulling Service
    • Or any other?

    Regards,

    dazy


    <input id="gwProxy" type="hidden"><!--Session data--><input onclick="jsCall();" id="jsProxy" type="hidden">

    Tuesday, February 16, 2010 7:00 AM
  • User-389939489 posted

    > Solution suggested my LV are all for the web apps hosted in a shared enviroment, so will they not be useful in my case?

    Yes, they would: it is the other solutions that you cannot apply to a shared hosting, not the other way round.

    Anyway, given that you are on a dedicated server, I would suggest the windows service: as I have said, there is minimal difference in the actual coding involved. Sql mailing is of course an option too, in place of the windows service, though I find it a bit clumsy unless your overall application architecture makes it sensible to allocate such functionality to the database.

    Best luck,

    -LV

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 16, 2010 7:12 AM
  • User-952121411 posted

    There is no issue and no unwanted effects at all: your worries sound like you don't know what the Asp.Net app life-cycle is.
     

    On the contrary, I think the same of you.  Offering that you should use an ASP.NET web app to run as a long running background task is ridiculous, and to me sounds like you have never even built a Windows Service or are familiar with services much at all.  Your insults and ignorance show you also think you wrote the book on software development and in my experience makes for the worst species of software engineer: the 'know it all developer'.  Which by the way, you are not.  The best engineers are ones that realize the multiple levels of experience in the field, and help grow and teach others through productive means and not trough belittling and insults.  I honestly have just about come to the conclusion that anything I read from you should be taken with a grain of salt, and seriously consider going 180 degrees in the opposite direction.

    And LV, if you honestly don't know the pitfalls of the different timers in .NET, you once again show your lack of experience.  But as you conceded in your last post,  a Windows Service (as I mentioned) is the better solution for this poster's question.

    Lastly, don't bother coming on these forums to belittle and insult others.  IMO we should be helping people with meaningful and decent dialog, not to flex brawn on what you think you know about software development; leave that to your own website to parade your ego (which hasn't been updated in 2 years by the way...nice).

    Tuesday, February 16, 2010 9:54 AM
  • User-389939489 posted

    @atconway:

    > Offering that you should use an ASP.NET web app to run as a long running background task is ridiculous

    Which is not what I have suggested, but feel free to keep going your way.

    > Lastly, don't bother coming on these forums to belittle and insult others.

    It is you chasing me, not the other way round. I have already invited you to some seriousness: now, I shall invite you to some (intellectual) honesty.

    To be clear: I couldn't care less to be discussing with you, it's a pure waste of time: you are just another of those guys around here playing for points and personal visibility, and flooding the web with misinformation in the meantime.

    Goodbye,

    -LV

    Wednesday, February 17, 2010 2:41 PM
  • User183360985 posted

    Hi,

    Please Help Me.The Code Is Below Code IS Executing Properly But I Am Unable To Receive The Mail.

    I Dont Know What I Am Missing.Please Suggest Me What To Do. 

     

     

     protected void btnSend_Click(object sender, EventArgs e)
        {
           // SmtpMail.Send(txtFrom.Text ,txtTo.Text,txtSub.Text,txtBody.Text);
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            message.To.Add("prashu09singh@gmail.com");
            message.To.Add("abhi.sheki2388@gmail.com");
            message.Body = txtBody.ToString();
            message.Subject = txtSub.Text;
            message.From = new System.Net.Mail.MailAddress("abhi_sheki23@Yahoo.co.in");

     

      System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");
     
      try{
          smtp.Send(message);
        Response.Write("Your Email has been sent sucessfully Thank You");
        smtp = null;
      }
      catch (Exception exc){
        Response.Write("Send failure: " + exc.ToString());
      }

    Wednesday, June 2, 2010 12:21 PM