c# - how to send email automatically -
i trying send email automatically using timer. given below code have used send email. not responding. while using same code under button click event, working perfectly. help me find proper solution. give thanks you.
code:
namespace alertmail { public partial class form1 : form { public form1() { initializecomponent(); } private void timer1_tick(object sender, eventargs e) { mailmessage logininfo = new mailmessage(); string em = "toaddress@gmail.com"; logininfo.to.add(em.tostring()); logininfo.from = new mailaddress("fromaddress@gmail.com"); logininfo.subject = "alert information"; logininfo.body = "hai"; logininfo.isbodyhtml = true; smtpclient smtp = new smtpclient(); smtp.host = "smtp.gmail.com"; smtp.port = 587; smtp.enablessl = true; smtp.credentials = new system.net.networkcredential("fromaddress@gmail.com", "password"); smtp.send(logininfo); label6.text = "alert send email..!!"; } } }
in many web application need send schedule(automatic) emails , schedule them. like:
sends emails on regular basis send message @ daily, weekly, monthly or yearly intervals.for this, used windows services or windows application.
as know web server iis continuously running, can add together timer in application , timer can manage these activities
//inside global.ascx void application_start(object sender, eventargs e) { // code runs on application startup system.timers.timer mytimer = new system.timers.timer(); // set interval 5 seconds (5000 milliseconds). mytimer.interval = 5000; mytimer.autoreset = true; mytimer.elapsed += new elapsedeventhandler(mytimer_elapsed); mytimer.enabled = true; } public void mytimer_elapsed(object source, system.timers.elapsedeventargs e) { // utilize mailer code clsschedulemail objschedulemail = new clsschedulemail(); objschedulemail.sendschedulemail(); } // within class public void sendschedulemail() { // write send mail service code here. }
c# email webforms
No comments:
Post a Comment