With .NET 2.0 writing code which uses the IIS webserver to send mail has become even more powerful. The System.Net.Mail namespace has some rich classes.
private static void SendMailWithIIS(string subject, string body, string to)
{
MailMessage message = new MailMessage();
message.From = new MailAddress("Me@Spammer.net");
message.To.Add(to);
message.Subject = subject;
message.Body = body;
message.BodyEncoding = System.Text.Encoding.ASCII;
message.IsBodyHtml = true;
message.Priority = MailPriority.Normal;
SmtpClient smtp = new SmtpClient("Localhost");
smtp.Send(message);
}
The .NET docs even contain a sample to send mail asynchronous. The hard part lies not in the code but in the configuration of localhost. It considers itself being used as a mail relay and by default it does not allow anyone to do that. In your code you'll get the error message : Mailbox unavailable. The server response was: 5.7.1 Unable to relay for
To fix it you have to configure relay restrictions in the IIS admin.
Here I've set localhost, aka 127.0.0.1, as the only one allowed to relay mail. And now my .NET code can spam everybody. Use with care !