Saturday, August 11, 2007

Sending Mail in ASP.NET 2.0

MailMessage message = new MailMessage();

message.From = new MailAddress("sender@foo.bar.com");

message.To.Add(new MailAddress("recipient1@foo.bar.com"));

message.To.Add(new MailAddress("recipient2@foo.bar.com"));

message.To.Add(new MailAddress("recipient3@foo.bar.com"));

message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));

message.Subject = "This is my subject";

message.Body = "This is the content";

SmtpClient client = new SmtpClient();

client.Send(message);

System.Net.Mail reads SMTP configuration data out of the standard .NET configuration system (so for ASP.NET applications you’d configure this in your application’s web.config file). Here is an example of how to configure it:

<system.net>

<mailSettings>

<smtp from="test@foo.com">

<network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />

smtp>

mailSettings>

system.net>

Hope this helps,


Ganesh

No comments: