Sending Emails on a regular basis

Last post 12-02-2008 3:30 PM by zhunter. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 11-18-2008 7:34 PM

    • ianthm
    • Top 25 Contributor
    • Joined on 03-25-2008
    • Wannabe Slacker
    • Points 1,071

    Sending Emails on a regular basis

    Hi,

    I have a requirement to send emails to users on a regular basis some 2 weeks after they register for service. I also need to know if they respond. I believe queue manager is part of the answer. Can you help me with some code or resources that shows how to do this task. Thanks

    Filed under:
  •  Advertisement

    3 MONTHS FREE & FREE SETUP on ASP.NET 3.5/2.0 Web Hosting! Windows 2008 & 2003 Servers Available, MS SQL 2008/2005, .NET 3.5 SP1, Entity Framework, LINQ, Silverlight 2.0, 30 Day Money Back Guarantee – Click Here!

     
  • 11-19-2008 7:34 AM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Wannabe Slacker
    • Points 15,810

    Re: Sending Emails on a regular basis

     http://www.eggheadcafe.com/community/aspnet/6/10047188/use-windows-service.aspx

    also copied the code here:

    Its easy just go thr these steps;    

    1.Open Visual Studio.net > Select New Project 

         2.Select Windows Service and give Name as 'GoodDay' and Click Ok.

     

         3.Right Click the Design and Select Properties and give the Name and ServiceName as 

            'GoodDay' and set AutoLog to 'True'

         4.Drag and drop the OleDbConnection and Select its properties and change its name to

            'conn' and provide the connection String(I provided the Database sample(Ms

             Access)) along with this code.

         5.Right Click the Design and select View Code and

            Select Project > Add Reference > Select System.Web >Press Ok

         6.Now add the following to your code         

              using

    System.Web.Mail;

              using System.Data.OleDb;

     

         7.Declare DateTime mdt=DateTime.Now;

     

         8.    In static void Main()

             Edit the following Code

            ServicesToRun = new System.ServiceProcess.ServiceBase[ { new GoodDay() };

         9. Edit the following code in onstart()

             DataSet ds = new DataSet();

     

    {

    DateTime dt=DateTime.Now;

    conn.Open();

    OleDbDataAdapter da=

    da.Fill(ds);

     

    {

     

    {

    mdt=DateTime.Now.AddDays(+1);

    String mailtxt="";

    MailMessage mm =

    mm.BodyFormat = MailFormat.Html;

    mm.To = dr["emp_email"].ToString();

    mm.From = "xyz@xyz.com";

    mm.Subject="Good Day";

    mailtxt = "<font face='verdana' color='#FF9900'><b>"+"Hi "+dr["emp_name"].ToString()+"," + "</b></font><br><br>";

    mailtxt=mailtxt+"<font face='verdana' color='#FF0000'><b>"+"Good Day." + "</b></font><br><br>";

    mailtxt=mailtxt+"<font face='verdana' color='#008080'><b>"+"May today be filled with sunshine and smile, laughter and love." + "</b></font><br><br>";

    mailtxt=mailtxt+"<font face='verdana' color='#0000FF'><b>Cheers!" + "<br><br>";

    mm.Body = mailtxt;

    SmtpMail.SmtpServer="localhost";

    SmtpMail.Send(mm);

    }

    }

    }

     

    10.Go to Design,right click and Select Add Installer ,you will notice two controls

         serviceProcessInstaller1,serviceInstaller1

     

    11.Select  serviceProcessInstaller1 and go to its properties and change the Account

          type to 'Local System'

     

    12. Select  serviceInstaller1 and go to its properties and change DisplayName and

          ServiceName to 'GoodDay' and make the StartType to 'Automatic'

     

    13.Build the Solution(Cntrl+Shift+B)

    protected override void OnStart(string[ args)new OleDbDataAdapter("select * from Empdata",conn);if(dt.ToShortDateString()==mdt.ToShortDateString())foreach(DataRow dr in ds.Tables[0].Rows)new MailMessage();

     

     

    14.Open the Visual Studio .Net Command Prompt and give the path of application

         for example C:\bin\Debug and then type InstallUtil GoodDay.exe
                    

         to uninstall,  InstallUtil    /u GoodDay.exe         

    15. After Installation ,right click My Computer and

           Select 'Manage'

           and Select 'Service and Applications'

           and in that Select 'Services'

    Select 'GoodDay' and start the Service.

     

  • 11-19-2008 4:32 PM In reply to

    • ianthm
    • Top 25 Contributor
    • Joined on 03-25-2008
    • Wannabe Slacker
    • Points 1,071

    Re: Sending Emails on a regular basis

    Thank you for this which I have looked through carefully. I do not have Windows Service on my version of Visual Studio and hence cannot use this service. Is there another way of addressing the need to send emails to users? Thanks

    Filed under:
  • 11-19-2008 4:47 PM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Wannabe Slacker
    • Points 15,810

    Re: Sending Emails on a regular basis

     what version do you have?

    this is my screenshot

  • 11-19-2008 5:30 PM In reply to

    • ianthm
    • Top 25 Contributor
    • Joined on 03-25-2008
    • Wannabe Slacker
    • Points 1,071

    Re: Sending Emails on a regular basis

    I cant seem to get a screen shot into the browser. I have access to two computers with vs

    The first has windows application, windows control library, device application, asp.net web services application, class library, console application, asp.net web applications.

    The second has windows application, windows control library, crystal reports application, excell workbook, class library, console application, device application, outlook addin.

    ...no windows service !

     

    Thanks

     

     

     

    Filed under:
  • 11-19-2008 5:39 PM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Wannabe Slacker
    • Points 15,810

    Re: Sending Emails on a regular basis

     I am sure there is many ways to deal with this.

    I do not often use windows service either. My dumb way is write an application, using the same logic, then use system's auto task scheduler ...

  • 11-19-2008 5:45 PM In reply to

    • ianthm
    • Top 25 Contributor
    • Joined on 03-25-2008
    • Wannabe Slacker
    • Points 1,071

    Re: Sending Emails on a regular basis

    Yes I think that is what I will do. Any ideas are appreciated but I will get started

    Filed under:
  • 11-20-2008 8:46 AM In reply to

    • xxxd
    • Top 10 Contributor
    • Joined on 12-18-2006
    • Wannabe Slacker
    • Points 15,810

    Re: Sending Emails on a regular basis

     now this comes down to a very simple email application. Let me know where you have problems. Otherwise, you can simply go to the web and grab a sample

  • 11-20-2008 8:57 AM In reply to

    • sonu
    • Top 10 Contributor
    • Joined on 05-22-2006
    • Montreal / Canada
    • Slacker
    • Points 10,367
    • MVP

    Re: Sending Emails on a regular basis

    I personally used a simple console application, that I scheduled with the windows task-scheduler. I guess a windows service is an eleganter solution though.

    [MVP since 2005] [MCAD]
    Webmaster of DotNetSlackers
    Question or Suggestion?
    Feel free to ask my any .NET question
    Our Posting FAQ
  • 12-02-2008 3:30 PM In reply to

    • zhunter
    • Top 500 Contributor
    • Joined on 12-02-2008
    • San Diego
    • Wannabe Slacker
    • Points 32

    Re: Sending Emails on a regular basis

     Another solution is to use SQL Server's Scheduler, and a Stored Procedure to send the email.  I use this same approach to send follow-up messages on forms that have been left "incomplete" for x number of days.  Since everything you need is probably in your database (User name, Email Address, Contents), this might be another good soltuion.

    Here is an example I have handy, using SQL Server 2000 and the free extended stored procedure (XPSMTP.DLL) or you can use the 2005+ build in (xp_sendmail).

    CREATE PROCEDURE  [dbo].[spCS_SendReminders]
    AS

    SET NOCOUNT ON

    DECLARE @vchReqUser  varchar(30),
     @vchReqUserEmail varchar(50),
     @vchReqStatus  varchar(50),
     @vchRobinCode  varchar(5),
     @vchDelToCode  varchar(2),
     @From    nvarchar(50),
     @To     nvarchar(50),
     @Subject   nvarchar(255),
     @Body    nvarchar(500)

    DECLARE SendReminders CURSOR FOR
     SELECT r.Name, r.Email, c.Status, c.RobinCode, c.DelToCode
     FROM CS_RobinDelToFormBase c
     INNER JOIN RohmPortal.dbo.RohmPortal_Users r
     ON c.RequestedBy = r.UserID
     WHERE c.Status In ('Rejected','InProgress');

    OPEN SendReminders

    FETCH SendReminders INTO @vchReqUser,
       @vchReqUserEmail,
       @vchReqStatus,
       @vchRobinCode,
       @vchDelToCode

    WHILE @@Fetch_Status = 0

       BEGIN

       DECLARE @vchSubject  varchar(500),
       @vchMessage  varchar(500)

     SELECT @vchSubject = 'You have a pending form open for ' + @vchRobinCode + '-' + @vchDelToCode + ' with a status of ' + @vchReqStatus + '.'
     SELECT @vchMessage = 'Please update your form before the end of the month, if you no longer want the form.... Please log in and choose "CANCEL" on the reivew screen!'

     EXEC master.dbo.xp_smtp_sendmail
      @FROM   = '<email@domain.com>',
      @TO    = @vchReqUserEmail,
      @server   = '<smtp.domain.com>',
      @subject  = @vchSubject,
      @message  = @vchMessage

     FETCH SendReminders INTO @vchReqUser,
        @vchReqUserEmail,
        @vchReqStatus,
        @vchRobinCode,
        @vchDelToCode           

       END

    CLOSE SendReminders

    DEALLOCATE SendReminders

    RETURN

     

    Zach Hunter
    http://www.zachhunter.net
Page 1 of 1 (10 items)