tags:

views:

142

answers:

3

How would I tell my application to send an email to a subscriber? How would I send it every week starting from the time the appication is launched.

+8  A: 

Use the classes in the System.Net.Mail namespace to send emails from your app. Specifically, the MailMessage class serves this purpose.

To send emails periodically, you can use a timer (use System.Timers.Timer, for example) or you could use the built-in Windows task scheduler, which has rich functionality and runs as a service, so that you don't need to keep an interactive session open on your machine. I can give you a more detailed answer if you provide more details about the type of app you're developing.

CesarGon
How would I set a timer so that every week it does whatever.
Mohit Deshpande
As I said, you can use the built-in Windows scheduler or program the functionality yourself. Each approach has its own pros and cons. Using the Windows task scheduler can save you a lot of work: you only need to provide an executable to launch (which would send the email) and program the scheduler to run every week. On my Windows Vista, the task scheduler is under Control Panel, Administrative Tools, Task Scheduler; that may vary depending on the Windows version that you use.Programming the functionality yourself may give you more flexibility, but it's more work.
CesarGon
You might want to check out Quartz.NET. It's a scheduling framework/app. http://quartznet.sourceforge.net/
Walter
A: 

I haven't created anything directly like this myself but I've seen solutions that use a Scheduled Task on the Server which are set to run on a certain date/time a small script that carries out what is required. This assumes you have your own server though...

Alex
A: 

I use this method to use gmail, and it works well for me.

var fromAddress = new MailAddress("From");
                var toAddress = new MailAddress("To");
                string fromPassword = textBox4.Text;
                const string subject = "Test";
                const string body = "Test Finished";

                var smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new System.Net.NetworkCredential(fromAddress.Address, fromPassword)
                };
                using (var message = new MailMessage(fromAddress, toAddress)
                {
                    Subject = subject,
                    Body = body
                })
                {
                    Attachment attachf = new Attachment("C:\\file.txt");
                    message.Attachments.Add(attachf);
                    smtp.Send(message);
                }
            }
Mark W