Thursday, December 2, 2010

SmtpClient sends email from Windows Server 2008

SmtpClient can be used to send email from Asp.net application. However the following configuration is required to make it work.
1. Enable SMTP server service from Server manager page by clicking on "Add Feature" link
2. From IIS 6.0 Manger, right click "SMTP Virtal Server #1", and select "Property". Select "Access" tab, clicking "Relay...", and select "All exception the list below".
3. Restart IIS service

Sample code:

MailMessage mail = new MailMessage(m_strEmail, "jonathan_li2000@yahoo.ca");
mail.Subject = "Test subject";
SmtpClient mailClient = new SmtpClient("127.0.0.1");
mailClient.Send(mail);

Notice that once the server gets restarted, by default the SMTP Virtual server needs to be manually started. To automatically start the SMTP service, set the service to start automatically (Start -> Administrative Tools -> Services).

Good luck!

No comments:

Post a Comment