// this class sends email from an asp.net website. // edited by david stern, april 29, 2007. // for more information, visit http://www.davidstern.co.il // notice the added namespaces system.collections and system.net.mail. using using using using using using using using using using using
system; system.data; system.configuration; system.web; system.web.security; system.web.ui; system.web.ui.webcontrols; system.web.ui.webcontrols.webparts; system.web.ui.htmlcontrols; system.collections; system.net.mail;
public class sendingmail { mailmessage mlms = new mailmessage(); smtpclient smtp = new smtpclient(); // set the from email address, message subject and message body from the gui to the following fields: public string fromaddress; public string subjecttext; public string bodytext; // this arraylist contains multiple email addresses the email will be sent to. arraylist toaddresses = new arraylist(); // this method adds addresses to the above arraylist. call it from the gui. public void addaddress(string address) { toaddresses.add(address); } // this method sends the mail. public void send() { mlms.from = new mailaddress(fromaddress); foreach (string address in toaddresses) { mlms.to.add(address); } mlms.subject = subjecttext; mlms.isbodyhtml = true; mlms.body = bodytext;
} }
smtp.host = "localhost"; smtp.port = 25; smtp.deliverymethod = smtpdeliverymethod.pickupdirectoryfromiis; smtp.send(mlms);