开发者

How do I tell my application to send an email?

How would I tell my application to send an email to a subscriber? How wo开发者_高级运维uld I send it every week starting from the time the appication is launched.


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.


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);
                }
            }


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜