c# - correct code for sending an email, asp.net -
i have form allows user send email on mailing list (linq table). i'm having trouble correct code , syntax linking smtp server.
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.web.profile; using system.web.security; using system.web.mail; using system.configuration; using system.web.configuration; using system.net.configuration; using system.net.mail; public partial class massemail : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void button1_click(object sender, eventargs e) { mailinglistclassdatacontext class = new mailinglistclassdatacontext(); var emaillist = emails in class.mailinglistmembers select emails.email; foreach (var subcriber in emaillist) { mailmessage objmail = new mailmessage(); objmail.from = "test@test.com"; objmail.to = subcriber; objmail.bodyformat = mailformat.html ; //the subject of message objmail.subject = "test email hope works" ; //he message text objmail.body = editor1.content; //need in area smtpclient client = new smtpclient(); smtpclient.send(objmail); } } }
using (var db = new mailinglistclassdatacontext()) { var client = new system.net.mail.smtpclient(); var recipients = e in db.mailinglistmembers select e.email; foreach (string recipient in recipients) { var message = new system.net.mail.mailmessage("sender@example.com", recipient); message.subject = "hello world!"; message.body = "<h1>foo bar</h1>"; message.isbodyhtml = true; client.send(message); } }
try setting configuration in web.config or machine.config. make sure you've specified correct address , port of smtp server.
<configuration> <system.net> <mailsettings> <smtp deliverymethod="network" from="me@example.com"> <network host="localhost" port="25" defaultcredentials="true" /> </smtp> </mailsettings> </system.net> </configuration>
Comments
Post a Comment